您好,登錄后才能下訂單哦!
這篇“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>
以上就是關于“Android怎么實現啟動引導圖”這篇文章的內容,相信大家都有了一定的了解,希望小編分享的內容對大家有幫助,若想了解更多相關的知識內容,請關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。