Android實現啟動引導圖
本文實例為大傢分享瞭Android實現啟動引導圖的具體代碼,供大傢參考,具體內容如下
下面是安卓啟動引導圖的實現,話不多說,直接上代碼。
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.關於的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 返回頁面的個數 */ @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;//圖片資源的數組 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: //從啟動頁跳轉到引導頁,修改保存的值,再次進入時跳過此頁面。 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); //根據ViewPager的item數量實例化數組 ivPointArray = new ImageView[viewList.size()]; //循環新建底部圓點ImageView,將生成的ImageView保存到數組中 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; //第一個頁面需要設置為選中狀態,這裡采用兩張不同的圖片 if (i == 0) { iv_point.setBackgroundResource(R.drawable.guide_dot_select); } else { iv_point.setBackgroundResource(R.drawable.guide_dot_unselect); } //將數組中的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參數,設置為全屏 LinearLayout.LayoutParams params = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT); //循環創建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)); //設置滑動監聽 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) { } /** * 滑動後的監聽 * * @param position */ @Override public void onPageSelected(int position) { //循環設置當前頁的標記圖 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.總結,代碼中的註釋比較詳細,各位博友都能看懂,沒有效果圖,裡面涉及的圖片請使用簡單的替代就行。
以上就是本文的全部內容,希望對大傢的學習有所幫助,也希望大傢多多支持WalkonNet。
推薦閱讀:
- viewpager實現自動循環輪播圖
- Android如何使用ViewPager2實現頁面滑動切換效果
- Android開發實現圖片切換APP
- ViewPager+Fragment實現側滑導航欄
- android輪播圖組件的制作方法