android 图片

本文介绍了如何使用BitmapFactory.Options来实现图片的高效加载与缩放,包括如何避免内存溢出,以及通过设置inJustDecodeBounds来获取图片尺寸的方法。此外,还提供了使用Drawable和URI加载图片的替代方案。

图片的缩放

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);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值