17.Android项目引导页

前沿

学习笔记,便于后续查看。

摘抄自这位大神:http://blog.csdn.net/cjm2484836553/article/details/53292150

效果图

hehe.gif

撸代码

  • GuideActivity.xml



    
    
  • 第五张图片的View



    

    
  • 白点和圆圈的shape

    
    



    
    
    

  • GuideActivity
public class GuideActivity extends AppCompatActivity implements View.OnClickListener{

    private View lastSplash;
    private ArrayList imageViews;
    private LinearLayout dotGroup;
    private ViewPager viewPager;
    private ImageView whiteDot;
    private int dotDistance;
    private Button mSkip;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //初始化视图
        initView();
        //初始化数据
        initData();
        //初始化监听
        initListener();
    }

    private void initView() {
        setContentView(R.layout.activity_guide);
        dotGroup = (LinearLayout)findViewById(R.id.ll_dot_group);
        viewPager = (ViewPager)findViewById(R.id.viewpager);
        whiteDot = (ImageView)findViewById(R.id.iv_white_dot);
        mSkip = (Button)findViewById(R.id.button_skip);
        mSkip.setOnClickListener(this);

        //最后一张
        lastSplash = View.inflate(this,R.layout.last_guide_view,null);
    }

    private void initData() {
        imageViews = new ArrayList<>();
        int dotWidth = DensityUtil.dip2px(this,10);
        int[] imagesId = {
                R.drawable.splash_1,
                R.drawable.splash_2,
                R.drawable.splash_3,
                R.drawable.splash_4,
        };
        for (int i = 0; i < imagesId.length+1; i++) {  //这里需要加1
            //创建小圆点
            ImageView dot = new ImageView(this);
            dot.setBackgroundResource(R.drawable.dot_normal_shape);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(dotWidth,dotWidth);
            if (i!=0){
                params.leftMargin =dotWidth;
            }
            dot.setLayoutParams(params);
            //添加小圆点
            dotGroup.addView(dot);

            if(i!=imagesId.length){
                ImageView imageView = new ImageView(this);
                imageView.setBackgroundResource(imagesId[i]);
                imageViews.add(imageView);
            }

            //ViewPager设置适配器
            viewPager.setAdapter(new ViewpagersAdapter());
        }

    }

    public class ViewpagersAdapter extends PagerAdapter{

        @Override
        public int getCount() {
            return imageViews.size()+1;
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view==object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            View view = null;
            if (position
  • 用到的一个工具类
public class DensityUtil {
    /**
     * 根据手机的分辨率从 dip 的单位 转成为 px(像素)
     */
    public static int dip2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

    /**
     * 根据手机的分辨率从 px(像素) 的单位 转成为 dp
     */
    public static int px2dip(Context context, float pxValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (pxValue / scale + 0.5f);
    }
}

结束……

你可能感兴趣的:(17.Android项目引导页)