图片的缩放
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 4; //长、宽分别缩放至原图的1/4
Bitmap bitmap = BitmapFactory.decodeFile(filepath, options);
viewHolder.mImageView.setImageBitmap(bitmap);
不占用内存,获取图片信息,可以认为是dummy decoder
BitmapFactory.Options
boolean inJustDecodeBounds
If set to true, the decoder will return null (no bitmap), but the out… fields will still be set, allowing the caller to query the bitmap without having to allocate the memory for its pixels.
outHeight与outWidth就是图片的长和宽。
可以先用这种方式获取长宽,然后再决定图片的尺寸。
Drawable from file
Drawable drawable = Drawable.createFromPath(filepath);
viewHolder.mImageView.setImageDrawable(drawable);
SET IMAGE WITH URI
Uri uri = Uri.fromFile(new File(filepath));
viewHolder.mImageView.setImageURI(uri);
本文介绍了如何使用BitmapFactory.Options来实现图片的高效加载与缩放,包括如何避免内存溢出,以及通过设置inJustDecodeBounds来获取图片尺寸的方法。此外,还提供了使用Drawable和URI加载图片的替代方案。
2万+

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



