ImageView 的 src 属性
2016-04-09 15:59
369 查看
一般都是在 ImageView 中展示图片,如果简单是使用 src 属性:
Bitmap 就是图片原尺寸,大小是 2.2MB。
如果使用谷歌官网推荐的 inSampleSize 参数的话:
Bitmap 大小就变成 0.55 MB 了。
但这样比较慢,所以还是选好尺寸吧。
Bitmap 就是图片原尺寸,大小是 2.2MB。
如果使用谷歌官网推荐的 inSampleSize 参数的话:
public class MyImageView extends ImageView{ public MyImageView(Context context) { super(context); } public MyImageView(Context context, AttributeSet attrs) { super(context, attrs); } public MyImageView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } // @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); int height = MeasureSpec.getSize(heightMeasureSpec); int width = MeasureSpec.getSize(widthMeasureSpec); Bitmap bitmap = decodeSampledBitmapFromResource(getResources(), R.drawable.big,width,height); setImageBitmap(bitmap); } public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId, int reqWidth, int reqHeight) { // First decode with inJustDecodeBounds=true to check dimensions final BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeResource(res, resId, options); // Calculate inSampleSize options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight); // Decode bitmap with inSampleSize set options.inJustDecodeBounds = false; return BitmapFactory.decodeResource(res, resId, options); } public static int calculateInSampleSize( BitmapFactory.Options options, int reqWidth, int reqHeight) { // Raw height and width of image final int height = options.outHeight; final int width = options.outWidth; int inSampleSize = 1; if (height > reqHeight || width > reqWidth) { final int halfHeight = height / 2; final int halfWidth = width / 2; // Calculate the largest inSampleSize value that is a power of 2 and keeps both // height and width larger than the requested height and width. while ((halfHeight / inSampleSize) > reqHeight && (halfWidth / inSampleSize) > reqWidth) { inSampleSize *= 2; } } return inSampleSize; } }
Bitmap 大小就变成 0.55 MB 了。
但这样比较慢,所以还是选好尺寸吧。
相关文章推荐
- 在CentOS 7上构建RAID5、LVM和SAMBA服务器(4)——逻辑卷(组)扩容
- 状压dp
- 继承与派生定义及相关程序调用举例
- 1012. The Best Rank
- C语言中使用快速排序算法对元素排序的实例详解
- c++ tuple类型
- Qt5.2.0配置sdk
- 自定义view的measureSpec是谁的mode和size
- dapper 的学习
- BZOJ 4311 向量
- 2-SAT题集
- LuaFileSystem学习心得
- SQLServer函数 left()、charindex()、stuff()的使用
- jQuery validate 插件详解 转载
- 后缀自动机&序列自动机综合
- clock()函数在多线程时的问题
- automaticallyAdjustsScrollViewInsets
- 【LeetCode】75. Sort Colors解法及注释
- ES2015/ES6常用新特性
- zoj 3209 Treasure Map dancing links