溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

如何使用Android實(shí)現(xiàn)漸變啟動(dòng)頁(yè)和帶有指示器的引導(dǎo)頁(yè)

發(fā)布時(shí)間:2021-09-27 11:17:29 來(lái)源:億速云 閱讀:131 作者:小新 欄目:編程語(yǔ)言

小編給大家分享一下如何使用Android實(shí)現(xiàn)漸變啟動(dòng)頁(yè)和帶有指示器的引導(dǎo)頁(yè),希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!

實(shí)現(xiàn)步驟:

1.首先我們做個(gè)有漸變動(dòng)畫(huà)的啟動(dòng)頁(yè)面SplashActivity

在onCreate里設(shè)置核心方法setAlphaAnimation()

public void setAlphaAnimation(){   //生成AlphaAnimation的對(duì)象   AlphaAnimation animation= new AlphaAnimation(this);   //設(shè)置動(dòng)畫(huà)的持續(xù)時(shí)間   animation.setDuration(3000);   //給要漸變的控件設(shè)置動(dòng)畫(huà),比如說(shuō)imageview,textview,linearLayout之類的   ll.setAnimation(animation);   //設(shè)置動(dòng)畫(huà)監(jiān)聽(tīng),結(jié)束時(shí)跳轉(zhuǎn)到下一個(gè)頁(yè)面(首次打開(kāi)就是引導(dǎo)頁(yè)面,反之就是主頁(yè))   animation.setAnimationListener(new Animation.AnimationListener(){      public void onAnimationStart(Animation animation){ }      public void onAnimationEnd(Animation animation){          jump2Activity();            }      public void onAnimationRepeat(Animation animation){ }   });}

分析一下這個(gè)跳轉(zhuǎn)方法jump2Activity(),我們這里使用SharedPeference來(lái)判斷應(yīng)用是否首次打開(kāi),設(shè)變量isFirst默認(rèn)值為0,進(jìn)入引導(dǎo)頁(yè)跳轉(zhuǎn)到主頁(yè)時(shí)再把這個(gè)值設(shè)為1,這樣,每次跳轉(zhuǎn)時(shí)判斷isFirst的值,如果仍是默認(rèn)值0則為首次打開(kāi)進(jìn)入引導(dǎo)頁(yè),反之進(jìn)入主頁(yè)。

public void jump2Activity(){  SharedPreferences sharedPreference= getSharedPreferences("data", MODE_PRIVATE);  String isFirst= sharedPreferences.getString("isFirst", "0");  Intent intent= new Intent();    if("0".equals(isFirst)){    intent.setClass(this, GuideActivity.class);  }else{    intent.setClass(this. MainActivity.class);  }  startActivity(intent);  finish();}

2.接下來(lái)我們做引導(dǎo)頁(yè)面

引導(dǎo)頁(yè)面是由三個(gè)控件組成,Viewpager,圓點(diǎn)指示器的線性布局linearlayout,最后一頁(yè)的 “進(jìn)入應(yīng)用”按鈕。

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent">  <android.support.v4.view.ViewPager  android:id="@+id/guide_vp"  android:layout_width="match_parent"  android:layout_height="match_parent" />  <LinearLayout  android:id="@+id/guide_ll"  android:layout_width="wrap_content"  android:layout_height="wrap_content"  android:layout_alignParentBottom="true"  android:layout_centerHorizontal="true"  android:layout_marginBottom="100dp"  android:orientation="horizontal" />  <Button  android:id="@+id/guide_btn"  android:layout_width="wrap_content"  android:layout_height="wrap_content"  android:layout_above="@id/guide_ll"  android:layout_centerHorizontal="true"  android:text="進(jìn)入應(yīng)用"  android:layout_marginBottom="10dp"  android:visibility="gone"/> </RelativeLayout>

在GuideActivity中,首先初始化引導(dǎo)圖片

/** * 初始化圖片 */private void initImgs() { ViewPager.LayoutParams params= new ViewPager.LayoutParams(); imageViews= new ArrayList<ImageView>(); for (int i= 0; i< imgs.length; i++){  ImageView imageView= new ImageView(this);  imageView.setLayoutParams(params);  imageView.setImageResource(imgs[i]);  imageView.setScaleType(ImageView.ScaleType.FIT_XY);  imageViews.add(imageView); }}

初始化底部圓點(diǎn)指示器,這里值得一提的是我們給各圓點(diǎn)設(shè)置相應(yīng)的點(diǎn)擊事件,當(dāng)點(diǎn)擊某個(gè)位置的圓點(diǎn)時(shí),viewpager自動(dòng)切換到相應(yīng)位置的圖片,不過(guò)實(shí)際應(yīng)用中這里實(shí)用性不是很大,因?yàn)閳A點(diǎn)太小,可觸摸范圍有限,點(diǎn)擊事件不太好觸發(fā)。

/** * 初始化底部圓點(diǎn)指示器 */private void initDots() { LinearLayout layout= findViewById(R.id.guide_ll); LinearLayout.LayoutParams params= new LinearLayout.LayoutParams(20, 20); params.setMargins(10, 0, 10, 0); dotViews= new ImageView[imgs.length]; for (int i= 0; i< imageViews.size(); i++){  ImageView imageView= new ImageView(this);  imageView.setLayoutParams(params);  imageView.setImageResource(R.drawable.dotselector);  if (i== 0){   imageView.setSelected(true);  }else{   imageView.setSelected(false);  }  dotViews[i]= imageView;  final int finalI = i;  dotViews[i].setOnClickListener(new View.OnClickListener() {   @Override   public void onClick(View view) {    vp.setCurrentItem(finalI);   }  });  layout.addView(imageView); }}

設(shè)置viewpager的滑動(dòng)事件

vp.addOnPageChangeListener(this);

生成三個(gè)方法,我們主要在onPageSelected()方法中做操作,當(dāng)某個(gè)位置的圓點(diǎn)被選中時(shí),顯示選中后的圖片,其余圓點(diǎn)顯示未選中的圖片,這里主要應(yīng)用selector控制器,至于相應(yīng)的選中未選中圓點(diǎn)圖片需要大家去找。當(dāng)滑動(dòng)到最后一個(gè)頁(yè)面時(shí),將 "進(jìn)入應(yīng)用"的按鈕顯示,反之隱藏。

@Overridepublic void onPageScrolled(int i, float v, int i1) {} @Override public void onPageScrollStateChanged(int i) {} @Overridepublic void onPageSelected(int arg0) { for (int i= 0; i< dotViews.length; i++){  if (arg0== i){   dotViews[i].setSelected(true);  }else {   dotViews[i].setSelected(false);  }   if (arg0== dotViews.length- 1){   btn.setVisibility(View.VISIBLE);  }else {   btn.setVisibility(View.GONE);  } }}

dotSelector.xml

<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/focus_on" android:state_selected="true"/> <item android:drawable="@drawable/focus_nomal" android:state_selected="false"/></selector>

在最后一個(gè)頁(yè)面點(diǎn)擊 "進(jìn)入應(yīng)用"按鈕跳轉(zhuǎn)到主頁(yè)時(shí),將緩存中的isFirst數(shù)據(jù)改為1,以后打開(kāi)應(yīng)用則不會(huì)再進(jìn)入引導(dǎo)頁(yè)面了。

@Overridepublic void onClick(View view) { switch (view.getId()){  case R.id.guide_btn:   setisFirst();   Intent intent= new Intent(GuideActivity.this, MainActivity.class);   startActivity(intent);   finish();   break; }}/** * 改變首次打開(kāi)的狀態(tài) */private void setisFirst() { SharedPreferences.Editor editor= getSharedPreferences("data", MODE_PRIVATE).edit(); editor.putString("isFirst", "1"); editor.commit();}

看完了這篇文章,相信你對(duì)“如何使用Android實(shí)現(xiàn)漸變啟動(dòng)頁(yè)和帶有指示器的引導(dǎo)頁(yè)”有了一定的了解,如果想了解更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI