Android App引导页的写法(有效避免内存溢出和ANR,卡顿问题等问题),支持Ctrl+C和Ctrl+V,直接复制使用

xml代码:




    
    

    

java代码:

public class NewGuideActivity extends BaseActivity {

    private static final String TAG = "NewGuideActivity";
    @BindView(R.id.guid_viewPager)
    ViewPager      mGuidViewPager;
    @BindView(R.id.buton_begin)
    Button         mButonBegin;
    @BindView(R.id.guid_indicator_container)
    LinearLayout   mGuidIndicatorContainer;
    @BindView(R.id.guid_indicator_selected)
    ImageView      mGuidIndicatorSelected;
    @BindView(R.id.activity_main)
    RelativeLayout activityMain;
    private int[]     mGuidArray;
    private ImageView mMImageView;//滑动的动态大图片

    @Override
    public int getLayOutID() {
        return R.layout.activity_new_guide;
    }

    @Override
    protected void initData() {
        //实现viewpager和数据的绑定
        //创建数据集
        mGuidArray = new int[]{R.drawable.bg_guide1, R.drawable.bg_guide2, R.drawable.bg_guide3};
        //设置适配器
        mGuidViewPager.setAdapter(new GuidPagerAdapter());

        //动态的添加indicator
        for (int i = 0; i < mGuidArray.length; i++) {
            ImageView ivIndicator = new ImageView(NewGuideActivity.this);
            ivIndicator.setImageResource(R.drawable.dot_normal);

            //给数字赋单位
            int unit = (int) (TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 10, getResources().getDisplayMetrics()) + .5f);
            int width = unit;//10dp
            int height = unit;//10dp

            LinearLayout.LayoutParams parmas = new LinearLayout.LayoutParams(width, height);

            if (i == 0) {//第一个点
                mGuidIndicatorContainer.addView(ivIndicator, parmas);
            } else {
                //左间距
                parmas.leftMargin = unit * 5;//设置两点之间的距离.
                mGuidIndicatorContainer.addView(ivIndicator, parmas);
            }
        }

    }

    @Override
    protected void initEvent() {
        initListener();
    }

    class GuidPagerAdapter extends PagerAdapter {

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

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

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            //view
            ImageView imageView = new ImageView(NewGuideActivity.this);
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
            //data
            int resId = mGuidArray[position];
            try {
                getBitmapForImgResourse(mContext, resId, imageView);
            } catch (Exception e) {
                Logger.e(TAG, "e------" + e);
                Glide.with(mContext).load(resId).placeholder(resId).diskCacheStrategy(DiskCacheStrategy.RESULT).into(imageView);
            }

            //加入容器
            container.addView(imageView);

            return imageView;
        }

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

    //滑动监听的方法
    private void initListener() {
        mGuidViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            private int mSpace;

            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                //position 移动的页面的索引的位置
                //positionoffset 页面移动的比值---页面移动出去的宽度/页面总的宽度
                //positionOffsetPixels 页面移动出去的宽度
                //space 两个点之间的距离(从第一个点移动到第二个点需要的距离)
                if (mSpace == 0) {
                    mSpace = mGuidIndicatorContainer.getChildAt(1).getLeft() - mGuidIndicatorContainer.getChildAt(0).getLeft();
                }
                int marginLeft = (int) (mSpace * position + mSpace * positionOffset + .5f);
                //设置红色点的marginLeft
                RelativeLayout.LayoutParams curLayoutparms = (RelativeLayout.LayoutParams) mGuidIndicatorSelected.getLayoutParams();
                curLayoutparms.leftMargin = marginLeft;

                //重新设置LayoutParams
                mGuidIndicatorSelected.setLayoutParams(curLayoutparms);
            }

            @Override
            public void onPageSelected(int position) {
                if (position == mGuidViewPager.getAdapter().getCount() - 1) {
                    mButonBegin.setVisibility(View.VISIBLE);
                } else {
                    mButonBegin.setVisibility(View.INVISIBLE);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    //点击开始按钮的点击事件
    @OnClick(R.id.buton_begin)
    public void onClick(View v) {
        //存储已经走完向导页的状态
        AppConfig.put(mContext, Constant.GUIDE, true);
        Constant.GUIDE_FLAG = true;
        Intent intent = new Intent(NewGuideActivity.this, LoginActivity.class);
        startActivity(intent);
        finish();
    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    /**
     * 大图片处理机制
     * 利用Bitmap 转存 R图片
     */
    public static Bitmap btp;

    public void getBitmapForImgResourse(Context mContext, int imgId, ImageView mImageView) {
        try {
            mMImageView = mImageView;
            InputStream is = mContext.getResources().openRawResource(imgId);
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inJustDecodeBounds = false;
            options.inPreferredConfig = Bitmap.Config.RGB_565;
            options.inPurgeable = true;
            options.inInputShareable = true;
            options.inSampleSize = 1;
            btp = BitmapFactory.decodeStream(is, null, options);
            if (btp != null && mImageView != null) {//防止使用的时候,该图片已经被回收了.
                mImageView.setImageBitmap(btp);
            }
            is.close();
        } catch (Exception e) {
            Logger.e(TAG, "E------" + e.getMessage());
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (btp != null) {
            btp.recycle();
            btp = null;
        }
        if (mMImageView != null) {
            try {
                mMImageView.setBackground(null);
            } catch (Exception e) {
                Logger.e(TAG, "E----" + e.getMessage());
            }

        }

        System.gc();
    }
}

 

  因为引导页的图片非常大,一定要放入App资源目录的时候,先进行压缩,代码中采用采样率的方式,展示,避免在大规模使用的时候,出现OOM等异常!

你可能感兴趣的:(安卓)