public void image(int id) {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
// 获取这个图片的宽和高
bitmap = BitmapFactory.decodeResource(getResources(), id, options); // 此时返回bm为空
options.inJustDecodeBounds = false;
// 计算缩放比
int be = (int) (options.outHeight / (float) 50);
if (be <= 0)
be = 1;
options.inSampleSize = be;
// 重新读入图片,注意这次要把options.inJustDecodeBounds 设为 false哦
bitmap = BitmapFactory.decodeResource(getResources(), id, options);
int bmpWidth = bitmap.getWidth();
int bmpHeight = bitmap.getHeight();
Log.i("RG", "bmpWidth = " + bmpWidth + ", bmpHeight = " + bmpHeight);
}
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
// 获取这个图片的宽和高
bitmap = BitmapFactory.decodeResource(getResources(), id, options); // 此时返回bm为空
options.inJustDecodeBounds = false;
// 计算缩放比
int be = (int) (options.outHeight / (float) 50);
if (be <= 0)
be = 1;
options.inSampleSize = be;
// 重新读入图片,注意这次要把options.inJustDecodeBounds 设为 false哦
bitmap = BitmapFactory.decodeResource(getResources(), id, options);
int bmpWidth = bitmap.getWidth();
int bmpHeight = bitmap.getHeight();
Log.i("RG", "bmpWidth = " + bmpWidth + ", bmpHeight = " + bmpHeight);
}
本文介绍了一种在Android中处理图片资源的方法,通过调整图片大小来优化内存使用。该方法首先获取图片尺寸,然后计算合适的缩放比例,最后读取并返回缩放后的Bitmap对象。
1256

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



