作物和调整大小的Andr​​oid图像 [英] Crop and resize an image in Android

查看:120
本文介绍了作物和调整大小的Andr​​oid图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是从磁盘读取图像,并在的ListView 行内显示出来。图像文件是不是需要什么样的的ImageView 的行内显示更大。因为我需要缓存位图在RAM中的快速访问,我想他们只是一样大的的ImageView S(85x85 DIP)

I am reading an image from disk and displaying it inside of a row in a ListView. The image files are larger than what needs to be displayed inside the ImageView of the rows. Since I need to cache the bitmaps in RAM for faster access I would like them to only be as large as the ImageViews (85x85 dip)

现在我读文件中以

位= BitmapFactory.de codeFILE(文件);

bitmap = BitmapFactory.decodeFile(file);

和ImageView的负责缩放和剪切它

and the ImageView is responsible for scaling and cropping it

安卓scaleType =centerCrop

android:scaleType="centerCrop"

AFAIK这是保持在内存中整个位图(因为我缓存吧XD),这是不好的。

AFAIK this is keeping the entire bitmap in memory (because I cached it XD) and that is bad

我怎样才能删除该ImageView的这个责任,做作物+规模在加载文件?所有的位图将在85x85倾角被显示和需要centerCrop'

How can I remove this responsibility from the ImageView and do the crop + scale while loading the file? All the bitmaps will be displayed at 85x85 dip and need to be 'centerCrop'

推荐答案

您可以找到您的图片加载前的尺寸,裁剪和缩放:

You can find out the dimensions of your pictures before loading, cropping and scaling:



BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;

    Bitmap bmo = BitmapFactory.decodeFile(file.getAbsolutePath(), options);

然后将其加载在试样尺寸:

Then load it in sample size:


...
options.inSampleSize = 1/2;
bmo = BitmapFactory.decodeFile(file.getAbsolutePath(), options);

...
 = Bitmap.createScaledBitmap(bmo, dW, dH, false);

别忘了回收临时位图或你会得到OOME。

don't forget to recycle temporary bitmaps or you'll get OOME.


bmo.recycle();

这篇关于作物和调整大小的Andr​​oid图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆