Android實現(xiàn)啟動引導圖
本文實例為大家分享了Android實現(xiàn)啟動引導圖的具體代碼,供大家參考,具體內(nèi)容如下
下面是安卓啟動引導圖的實現(xiàn),話不多說,直接上代碼。
1.布局文件:activity_guide.xml
<?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/ll_guide_point" ? ? ? ? android:layout_width="match_parent" ? ? ? ? android:layout_height="wrap_content" ? ? ? ? android:layout_alignParentBottom="true" ? ? ? ? android:layout_marginBottom="40dp" ? ? ? ? android:gravity="center_horizontal" ? ? ? ? android:orientation="horizontal" /> ? ? ? <ImageButton ? ? ? ? android:id="@+id/guide_ib_start" ? ? ? ? android:layout_width="wrap_content" ? ? ? ? android:layout_height="60dp" ? ? ? ? android:layout_alignParentBottom="true" ? ? ? ? android:layout_centerHorizontal="true" ? ? ? ? android:layout_gravity="center_vertical" ? ? ? ? android:layout_marginBottom="120dp" ? ? ? ? android:src="@mipmap/img_guide_start" ? ? ? ? android:visibility="gone" /> </RelativeLayout>
2.關(guān)于的ViewPager的適配器GuidePageAdapter.java
/** ?* Created by gdk on 2019/7/22 14:55 ?* Describe:安裝apk第一次啟動的引導頁 ?* ?* @author gdk ?*/ public class GuidePageAdapter extends PagerAdapter { ? ? //存放圖片的集合 ? ? private List<View> viewList; ? ? ? public GuidePageAdapter(List<View> viewList) { ? ? ? ? this.viewList = viewList; ? ? } ? ? ? /** ? ? ?* @return 返回頁面的個數(shù) ? ? ?*/ ? ? @Override ? ? public int getCount() { ? ? ? ? if (viewList != null) { ? ? ? ? ? ? return viewList.size(); ? ? ? ? } ? ? ? ? return 0; ? ? } ? ? ? /** ? ? ?* 判斷對象是否生成界面 ? ? ?* ? ? ?* @param view ? ? ?* @param object ? ? ?* @return ? ? ?*/ ? ? @Override ? ? public boolean isViewFromObject(View view, Object object) { ? ? ? ? return view == object; ? ? } ? ? ? /** ? ? ?* 初始化position位置的界面 ? ? ?* ? ? ?* @param container ? ? ?* @param position ? ? ?* @return ? ? ?*/ ? ? @Override ? ? public Object instantiateItem(ViewGroup container, int position) { ? ? ? ? container.addView(viewList.get(position)); ? ? ? ? return viewList.get(position); ? ? } ? ? ? ? @Override ? ? public void destroyItem(ViewGroup container, int position, Object object) { ? ? ? ? container.removeView(viewList.get(position)); ? ? } }
3.引導頁的Activity,GuideActivity.java
/** ?* 第一次安裝的引導頁 ?* Created by gdk on 2019-07-22 ?* ?* @author gdk ?*/ ? public class GuideActivity extends BaseActivity<LoginContract.Presenter> implements LoginContract.View, ViewPager.OnPageChangeListener { ? ? ? @BindView(R.id.guide_ib_start) ? ? ImageButton guideIbStart; ? ? @BindView(R.id.guide_vp) ? ? ViewPager guideVp; ? ? @BindView(R.id.ll_guide_point) ? ? LinearLayout llGuidePoint; ? ? private int[] imagePositionArray;//圖片資源的數(shù)組 ? ? private List<View> viewList;//圖片資源的集合 ? ? ? //實例化原點View ? ? private ImageView iv_point; ? ? private ImageView[] ivPointArray; ? ? ? @Override ? ? public int getLayoutId() { ? ? ? ? return R.layout.activity_guide; ? ? } ? ? ? @Override ? ? public LoginContract.Presenter initPresenter() { ? ? ? ? return new LoginPresenter(this); ? ? } ? ? ? @Override ? ? public void initView() { ? ? ? ? //加載滑動的ViewPager ? ? ? ? initViewPager(); ? ? ? ? //加載底部圓點 ? ? ? ? initPoint(); ? ? } ? ? ? @OnClick({R.id.ll_guide_point, R.id.guide_ib_start}) ? ? public void onViewClicked(View view) { ? ? ? ? switch (view.getId()) { ? ? ? ? ? ? case R.id.guide_ib_start: ? ? ? ? ? ? ? ? //從啟動頁跳轉(zhuǎn)到引導頁,修改保存的值,再次進入時跳過此頁面。 ? ? ? ? ? ? ? ? SpUitlsInfo.putString(GuideActivity.this, "VERSION", "1"); ? ? ? ? ? ? ? ? startActivity(new Intent(GuideActivity.this, MainActivity.class)); ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? default: ? ? ? ? ? ? ? ? break; ? ? ? ? } ? ? } ? ? ? /** ? ? ?* 加載底部圓點 ? ? ?*/ ? ? private void initPoint() { ? ? ? ? //這里實例化LinearLayout ? ? ? ? llGuidePoint = findViewById(R.id.ll_guide_point); ? ? ? ? //根據(jù)ViewPager的item數(shù)量實例化數(shù)組 ? ? ? ? ivPointArray = new ImageView[viewList.size()]; ? ? ? ? //循環(huán)新建底部圓點ImageView,將生成的ImageView保存到數(shù)組中 ? ? ? ? int size = viewList.size(); ? ? ? ? for (int i = 0; i < size; i++) { ? ? ? ? ? ? iv_point = new ImageView(this); ? ? ? ? ? ? LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, ? ? ? ? ? ? ? ? ? ? LinearLayout.LayoutParams.WRAP_CONTENT); ? ? ? ? ? ? //設置小圓點的間距 ? ? ? ? ? ? lp.setMargins(20, 0, 20, 0); ? ? ? ? ? ? iv_point.setLayoutParams(lp); ? ? ? ? ? ? ivPointArray[i] = iv_point; ? ? ? ? ? ? //第一個頁面需要設置為選中狀態(tài),這里采用兩張不同的圖片 ? ? ? ? ? ? if (i == 0) { ? ? ? ? ? ? ? ? iv_point.setBackgroundResource(R.drawable.guide_dot_select); ? ? ? ? ? ? } else { ? ? ? ? ? ? ? ? iv_point.setBackgroundResource(R.drawable.guide_dot_unselect); ? ? ? ? ? ? } ? ? ? ? ? ? //將數(shù)組中的ImageView加入到ViewGroup ? ? ? ? ? ? llGuidePoint.addView(ivPointArray[i]); ? ? ? ? } ? ? } ? ? ? /** ? ? ?* 加載圖片ViewPager ? ? ?*/ ? ? private void initViewPager() { ? ? ? ? //實例化圖片資源 ? ? ? ? imagePositionArray = new int[]{R.mipmap.img_guide_one, R.mipmap.img_guide_two, R.mipmap.img_guide_three, ? ? ? ? ? ? ? ? R.mipmap.img_guide_four}; ? ? ? ? viewList = new ArrayList<>(); ? ? ? ? //獲取一個Layout參數(shù),設置為全屏 ? ? ? ? LinearLayout.LayoutParams params = new LinearLayout.LayoutParams( ? ? ? ? ? ? ? ? LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT); ? ? ? ? //循環(huán)創(chuàng)建View并加入到集合中 ? ? ? ? int len = imagePositionArray.length; ? ? ? ? for (int i = 0; i < len; i++) { ? ? ? ? ? ? //new ImageView并設置全屏和圖片資源 ? ? ? ? ? ? ImageView imageView = new ImageView(this); ? ? ? ? ? ? imageView.setLayoutParams(params); ? ? ? ? ? ? imageView.setBackgroundResource(imagePositionArray[i]); ? ? ? ? ? ? //將ImageView加入到集合中 ? ? ? ? ? ? viewList.add(imageView); ? ? ? ? } ? ? ? ? ? //View集合初始化好后,設置Adapter ? ? ? ? guideVp.setAdapter(new GuidePageAdapter(viewList)); ? ? ? ? //設置滑動監(jiān)聽 ? ? ? ? guideVp.setOnPageChangeListener(this); ? ? } ? ? ? @Override ? ? public void onResult(Object result, String message) { ? ? ? } ? ? ? @Override ? ? public void onError(Throwable throwable, String message) { ? ? ? } ? ? ? @Override ? ? public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { ? ? ? } ? ? ? /** ? ? ?* 滑動后的監(jiān)聽 ? ? ?* ? ? ?* @param position ? ? ?*/ ? ? @Override ? ? public void onPageSelected(int position) { ? ? ? ? //循環(huán)設置當前頁的標記圖 ? ? ? ? int length = imagePositionArray.length; ? ? ? ? for (int i = 0; i < length; i++) { ? ? ? ? ? ? ivPointArray[position].setBackgroundResource(R.drawable.guide_dot_select); ? ? ? ? ? ? if (position != i) { ? ? ? ? ? ? ? ? ivPointArray[i].setBackgroundResource(R.drawable.guide_dot_unselect); ? ? ? ? ? ? } ? ? ? ? } ? ? ? ? //判斷是否是最后一頁,若是則顯示按鈕 ? ? ? ? if (position == imagePositionArray.length - 1) { ? ? ? ? ? ? guideIbStart.setVisibility(View.VISIBLE); ? ? ? ? } else { ? ? ? ? ? ? guideIbStart.setVisibility(View.GONE); ? ? ? ? } ? ? } ? ? ? @Override ? ? public void onPageScrollStateChanged(int state) { ? ? ? } ? ? ? @Override ? ? protected void onCreate(Bundle savedInstanceState) { ? ? ? ? super.onCreate(savedInstanceState); ? ? ? ? ButterKnife.bind(this); ? ? } ? }
涉及到兩個布局文件
guide_dot_select.xml
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" ? ? android:shape="oval"> ? ? ? <solid android:color="@color/white"></solid> ? ? <corners android:radius="3dp" /> ? ? <size ? ? ? ? android:width="8dp" ? ? ? ? android:height="8dp" /> </shape>
guide_dot_unselect.xml
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" ? ? android:shape="oval"> ? ? <solid android:color="@color/colorPrimaryDark"></solid> ? ? <corners android:radius="3dp" /> ? ? <size ? ? ? ? android:width="8dp" ? ? ? ? android:height="8dp" /> </shape>
4.總結(jié),代碼中的注釋比較詳細,各位博友都能看懂,沒有效果圖,里面涉及的圖片請使用簡單的替代就行。
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android動畫之TranslateAnimation用法案例詳解
這篇文章主要介紹了Android動畫之TranslateAnimation用法案例詳解,本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細內(nèi)容,需要的朋友可以參考下2021-08-08關(guān)于Android中Gradle和jar包下載慢的問題及解決方法
這篇文章主要介紹了解決Android中Gradle和jar包下載慢的問題,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-10-10Android中asset文件夾與raw文件夾的區(qū)別深入解析
本篇文章是對Android中的asset文件夾與raw文件夾區(qū)別進行了詳細的分析介紹,需要的朋友參考下2013-06-06Android性能優(yōu)化之JVMTI與內(nèi)存分配
這篇文章主要為大家介紹了Android性能優(yōu)化之JVMTI與內(nèi)存分配,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-10-10Android利用Intent啟動和關(guān)閉Activity
這篇文章主要為大家詳細介紹了Android利用Intent啟動和關(guān)閉Activity的相關(guān)操作,感興趣的小伙伴們可以參考一下2016-06-06