2018-08-24 如何编写WeclomeSplash页面

思路 :利用sharedpreferences+ViewPager+handler+bitmap去编写引导页面。废话不说直接上代码

private Handler mHandler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message message) {
            switch (message.what) {
                case 0x01:
                    //利用bitmap加载图片
                    setBitmap();
                    break;
                default:
                    break;
            }
            return false;
        }
    });

 //利用bitmap加载图片
    private void setBitmap() {
        ViewUtil.getViewWidth(mImageView, new OnViewListener() {
            @Override
            public void onView(int width, int height) {
                mBitmap = BitmapUtil.Resource(getResources(), R.drawable.host_splash, width, height);
                mImageView.setImageBitmap(mBitmap);
            }
        });
    }

因为加载图片内存比较大,所以使用bitmap去加载,这里用一个bitmap方法类去加载.

public class BitmapUtil {
    /**
     * @param resources 加载文件的路径
     * @param piexlW    真正显示图片的宽
     * @param pieylH    真正显示图片的高
     * @return 返回bitmap
     */
    public static Bitmap Resource(Resources resources, int id, int piexlW, int pieylH) {
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;//只加载图片宽高
        options.inPreferredConfig = Bitmap.Config.ARGB_8888;//设置位深度,减少内存消耗
        //预加载
        BitmapFactory.decodeResource(resources, id, options);
        int oringinalW = options.outWidth;
        int oringinalH = options.outHeight;

        //真正加载
        options.inJustDecodeBounds = false;
        options.inSampleSize = getSimpleSize(oringinalW, oringinalH, piexlW, pieylH);

        return BitmapFactory.decodeResource(resources, id, options);
    }

    private static int getSimpleSize(int oringinalW, int oringinalH, int piexlW, int pieylH) {
        int SimpleSize = 1;//设置采样率为1,不压缩

        if (oringinalW > oringinalH && oringinalW > piexlW) {
            SimpleSize = oringinalW / piexlW;
        } else if (oringinalW < oringinalH && oringinalH > pieylH) {
            SimpleSize = oringinalH / pieylH;
        }
        if (SimpleSize <= 0) {
            SimpleSize = 1;
        }
        return SimpleSize;
    }
}

这样就可将主页面解析并加载到我们手机上面了

 SharedPreferences preferences = getSharedPreferences("data", MODE_PRIVATE);
        isFirstload = preferences.getBoolean("isFirstload", true);
        mEditor = preferences.edit();
        isFirstload();
    }

利用sharedPreferences去缓存我们的登录信息,如果是第一次安装登录就加载跳转到我们的主页面,如果不是,就跳转到我们主页面Activity,这个没设么好说的

private void isFirstload() {

        if (isFirstload) {
            mHandler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    startActivity(new Intent(WelcomeActivity.this,
                            WelcomeSplashActivity.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK |
                            Intent.FLAG_ACTIVITY_NEW_TASK));
                    overridePendingTransition(R.anim.activity_right_in, R.anim.activity_left_out);
                }
            }, 1500);
            mEditor.putBoolean("isFirstload", false);
            mEditor.apply();
        } else {
            mHandler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    startActivity(new Intent(WelcomeActivity.this,
                            HomeActivity.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK |
                            Intent.FLAG_ACTIVITY_NEW_TASK));
                    overridePendingTransition(R.anim.activity_right_in, R.anim.activity_left_out);
                }
            }, 1500);
        }

    }

这里有一个跳转的动画overridePendingTransition,这是要紧跟着start,finsh等Intent活动去使用的,具体为什么,我也不太清楚。
这样就跳转到了我们的引导页面。
引导页面是我们要准备几个XML文件,很简单的,想放什么图片就放什么图片.




    

    

    

这是WelcomeSplashActivity的XML文件,很简单,一个ViewPager放循环图片,一个LineaerLayout用来放指示器,剩下一个按钮是等到最后一张图片显示出来用来跳转到主页面的。接下来到OnCreate方法中去编写吧

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //隐藏状态栏
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_welcome_splash);
        //编写布局
        initView();
        //编写指示器
        initPoint();

    }

 private void initView() {
        //实例化控件
        mViewPager = findViewById(R.id.viewpager_splash);
        mBtn_splash = findViewById(R.id.btn_splash);
        mlayout = findViewById(R.id.linear_layout_indicator);

        //添加视图
        Id = new int[]{R.layout.splash_layout_1, R.layout.splash_layout_2,
                R.layout.splash_layout_3, R.layout.splash_layout_4};
        for (int i = 0; i < Id.length; i++) {
            View view = LayoutInflater.from(this).inflate(Id[i], null);
            mViewList.add(view);
        }
        mViewPager.setPageTransformer(true, new DepthPageTransformer());
        adapter = new ViewPagerAdapter(mViewList);
        mViewPager.setAdapter(adapter);
        mViewPager.setOnPageChangeListener(this);
        mBtn_splash.setOnClickListener(this);
    }

    private void initPoint() {
        mIvPointArray = new ImageView[mViewList.size()];
        for (int i = 0; i < mViewList.size(); i++) {
            mIvpoint = new ImageView(this);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(30, 30);
            mIvpoint.setLayoutParams(params);
            params.setMargins(10, 0, 10, 0);
            mIvPointArray[i] = mIvpoint;
            if (i == 0) {
                mIvpoint.setImageResource(R.drawable.shape_circle_yellow);
            } else {
                mIvpoint.setImageResource(R.drawable.shape_circle_white);
            }

            mlayout.addView(mIvpoint);
        }
    }

    @Override
    public void onPageScrolled(int i, float v, int i1) {

    }

    @Override
    public void onPageSelected(int i) {
        for (int j = 0; j < mIvPointArray.length; j++) {
            mIvPointArray[i].setImageResource(R.drawable.shape_circle_yellow);
            if (i != j) {
                mIvPointArray[j].setImageResource(R.drawable.shape_circle_white);
            }
        }

        if (i == mViewList.size() - 1) {
            mBtn_splash.setVisibility(View.VISIBLE);

        } else {
            mBtn_splash.setVisibility(View.INVISIBLE);
        }
    }

    @Override
    public void onPageScrollStateChanged(int i) {

    }

    @Override
    public void onClick(View view) {
        startActivity(new Intent(WelcomeSplashActivity.this, HomeActivity.class)
                .setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK));
    }

initView()方法主要是实例化控件,然后去获取到我们之前准备的XML文件
并将图片设置到Adapter中.
mViewPager.setPageTransformer(true, new DepthPageTransformer());
这个是为了让动画有淡出淡入的效果,需要另一个工具类.不用设置.
initPoint()主要是设置指示器,然后监听WiewPager变化,实现指示器和图片联动.
哦,还有Adapter没有贴出,送上代码

class ViewPagerAdapter extends PagerAdapter {
        private List lists;

        public ViewPagerAdapter(List list) {
            this.lists = list;
        }

        @Override
        public int getCount() {
            if (lists != null) {
                return lists.size();
            }
            return 0;
        }

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

        @NonNull
        @Override
        public Object instantiateItem(@NonNull ViewGroup container, int position) {
            container.addView(lists.get(position));
            return lists.get(position);
        }

        @Override
        public void destroyItem(@NonNull ViewGroup container, int position,
                                @NonNull Object object) {
            container.removeView(lists.get(position));
        }
    }

比较简单,就不在赘述了,最后实现效果:


2018-08-24 如何编写WeclomeSplash页面_第1张图片
效果.gif

你可能感兴趣的:(2018-08-24 如何编写WeclomeSplash页面)