Android大图片加载处理

 BitmapFactory.Options options=new BitmapFactory.Options();//创建权利
                //先不计算内存
                options.inJustDecodeBounds=true;
                BitmapFactory.decodeResource(getResources(),R.mipmap.bitmap,options);
                //调用Options方法来获取图片宽高
                int outHeight = options.outHeight;
                int outWidth = options.outWidth;
                Log.d(TAG,"outHeight == >"+outHeight);
                Log.d(TAG,"outWidth == >"+outWidth);
                //拿控件的尺寸
                int measuredHeight = result_image.getMeasuredHeight();
                int measuredWidth = result_image.getMeasuredWidth();
                Log.d(TAG,"measureHeight ==>"+measuredHeight);
                Log.d(TAG,"measureWidth ==>"+measuredWidth);
                //默认不改变大小
                options.inSampleSize=1;
                //图片的宽度/控件的宽度
                //图片的高度/控件的高度
                //取两者间的最小值
                if (outHeight>measuredHeight||outWidth>measuredWidth){
                    int subHeight = outHeight / measuredHeight;
                    int subWidth = outWidth / measuredWidth;
                    options.inSampleSize=subHeight>subWidth?subWidth : subHeight;//
                }
                options.inJustDecodeBounds=false;//可以设置进去了
                Log.d(TAG,"options.inSampleSize==>"+options.inSampleSize);
                //根据控件大小,动态计算sample值
                Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.bitmap, options);
                result_image.setImageBitmap(bitmap);

你可能感兴趣的:(Android)