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大图片加载处理
最新推荐文章于 2021-05-26 07:00:44 发布
本文介绍如何使用BitmapFactory.Options动态调整图片尺寸,确保图片能够完美适配于指定控件,避免内存溢出和拉伸失真。通过计算图片与控件的宽高比,确定合适的inSampleSize值。
8170

被折叠的 条评论
为什么被折叠?



