Android實現(xiàn)漸變啟動頁和帶有指示器的引導(dǎo)頁
引導(dǎo)頁是項目中很常見的東西了,在用戶下載app首次打開后,會進入引導(dǎo)界面,通常都是三四張圖片說明,簡單介紹下app的功能和使用方法之類,最后一張有著“進入應(yīng)用”的按鈕,點擊即可進入主頁,之后打開app則不會再次進入啟動頁,話不多說,以下做個歸納。
效果圖:
實現(xiàn)步驟:
1.首先我們做個有漸變動畫的啟動頁面SplashActivity
在onCreate里設(shè)置核心方法setAlphaAnimation()
public void setAlphaAnimation(){ //生成AlphaAnimation的對象 AlphaAnimation animation= new AlphaAnimation(this); //設(shè)置動畫的持續(xù)時間 animation.setDuration(3000); //給要漸變的控件設(shè)置動畫,比如說imageview,textview,linearLayout之類的 ll.setAnimation(animation); //設(shè)置動畫監(jiān)聽,結(jié)束時跳轉(zhuǎn)到下一個頁面(首次打開就是引導(dǎo)頁面,反之就是主頁) animation.setAnimationListener(new Animation.AnimationListener(){ public void onAnimationStart(Animation animation){ } public void onAnimationEnd(Animation animation){ jump2Activity(); } public void onAnimationRepeat(Animation animation){ } }); }
分析一下這個跳轉(zhuǎn)方法jump2Activity(),我們這里使用SharedPeference來判斷應(yīng)用是否首次打開,設(shè)變量isFirst默認值為0,進入引導(dǎo)頁跳轉(zhuǎn)到主頁時再把這個值設(shè)為1,這樣,每次跳轉(zhuǎn)時判斷isFirst的值,如果仍是默認值0則為首次打開進入引導(dǎo)頁,反之進入主頁。
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.接下來我們做引導(dǎo)頁面
引導(dǎo)頁面是由三個控件組成,Viewpager,圓點指示器的線性布局linearlayout, 最后一頁的 “進入應(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="進入應(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); } }
初始化底部圓點指示器,這里值得一提的是我們給各圓點設(shè)置相應(yīng)的點擊事件,當點擊某個位置的圓點時,viewpager自動切換到相應(yīng)位置的圖片,不過實際應(yīng)用中這里實用性不是很大,因為圓點太小,可觸摸范圍有限,點擊事件不太好觸發(fā)。
/** * 初始化底部圓點指示器 */ 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的滑動事件
vp.addOnPageChangeListener(this);
生成三個方法,我們主要在onPageSelected()方法中做操作,當某個位置的圓點被選中時,顯示選中后的圖片,其余圓點顯示未選中的圖片,這里主要應(yīng)用selector控制器,至于相應(yīng)的選中未選中圓點圖片需要大家去找。當滑動到最后一個頁面時,將 "進入應(yīng)用" 的按鈕顯示,反之隱藏。
@Override public void onPageScrolled(int i, float v, int i1) {} @Override public void onPageScrollStateChanged(int i) {} @Override public 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>
在最后一個頁面點擊 "進入應(yīng)用" 按鈕跳轉(zhuǎn)到主頁時,將緩存中的isFirst數(shù)據(jù)改為1,以后打開應(yīng)用則不會再進入引導(dǎo)頁面了。
@Override public 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; } } /** * 改變首次打開的狀態(tài) */ private void setisFirst() { SharedPreferences.Editor editor= getSharedPreferences("data", MODE_PRIVATE).edit(); editor.putString("isFirst", "1"); editor.commit(); }
至此全部完成!demo附上
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android神兵利器之Image Asset Studio的實現(xiàn)
這篇文章主要介紹了Android神兵利器之Image Asset Studio的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06Flutter?將Dio請求轉(zhuǎn)發(fā)原生網(wǎng)絡(luò)庫的實現(xiàn)方案
這篇文章主要介紹了Flutter?將Dio請求轉(zhuǎn)發(fā)原生網(wǎng)絡(luò)庫,需要注意添加NativeNetInterceptor,如果有多個攔截器,例如LogInterceptors等等,需要將NativeNetInterceptor放到最后,需要的朋友可以參考下2022-05-05Android基于ListView實現(xiàn)類似Market分頁加載效果示例
這篇文章主要介紹了Android基于ListView實現(xiàn)類似Market分頁加載效果,結(jié)合完整實例形式分析了ListView的OnScroll方法來實現(xiàn)分頁與滾動加載的操作步驟與相關(guān)實現(xiàn)技巧,需要的朋友可以參考下2016-10-10Android學(xué)習(xí)教程之日歷庫使用(15)
這篇文章主要為大家詳細介紹了Android學(xué)習(xí)教程之日歷庫使用的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-11-11