这篇“Android怎么实现启动引导图”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“Android怎么实现启动引导图”文章吧。
布局文件: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>
关于的ViewPager的适配器GuidePageAdapter.java
public class GuidePageAdapter extends PagerAdapter { //存放图片的集合 private List<View> viewList; public GuidePageAdapter(List<View> viewList) { this.viewList = viewList; } @Override public int getCount() { if (viewList != null) { return viewList.size(); } return 0; } @Override public boolean isViewFromObject(View view, Object object) { return view == object; } @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)); }}
引导页的Activity,GuideActivity.java
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]); } } 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) { } @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怎么实现启动引导图”这篇文章的内容,相信大家都有了一定的了解,希望小编分享的内容对大家有帮助,若想了解更多相关的知识内容,请关注编程网行业资讯频道。