减少BitmapDrawable大小 [英] Reducing BitmapDrawable size
问题描述
我缩小图像大小用此功能:
I am reducing size of images with this function:
Drawable reduceImageSize (String path)
{
BitmapDrawable bit1 = (BitmapDrawable) Drawable.createFromPath(path);
Bitmap bit2 = Bitmap.createScaledBitmap(bit1.getBitmap(), 640, 360, true);
BitmapDrawable bit3 = new BitmapDrawable(getResources(),bit2);
return bit3;
}
和它的做工精细,唯一的问题是,当我把这个函数的多个时间的应用越来越慢,有没有什么办法来优化这个功能吗?通过矩阵也许减少大小?
我也是从读取SD卡的图像和需要追溯到绘制对象的动画,而这个功能提供了这一点。
And its working fine, the only problem is when I call this function multiple time the app is getting slow, is there any way to optimize this function? Maybe reducing size via matrix? Also I am reading images from SD Card and need the back as Drawable for animations, and this function provides this.
推荐答案
使用 BitmapFactory.Options
和 inJustDe codeBounds
来规模下来:
位图位= getBitmapFromFile(道路,宽,高);
public static Bitmap getBitmapFromFile(String path, int width, int height) {
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(path, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, width, height);
options.inJustDecodeBounds = false;
Bitmap bitmap = BitmapFactory.decodeFile(path, options);
return bitmap;
}
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) {
if (width > height) {
inSampleSize = Math.round((float)height / (float)reqHeight);
} else {
inSampleSize = Math.round((float)width / (float)reqWidth);
}
}
return inSampleSize;
}
了解更多关于在这里:装载大位图的高效
另外,我不知道你在哪里调用此方法,但如果你有很多人请确保您使用的缓存位图 LruCache
或类似:< A HREF =http://developer.android.com/training/displaying-bitmaps/cache-bitmap.html相对=nofollow>缓存位图
Also, I'm not sure where you are calling this method but if you have many of them please make sure you are caching the Bitmaps using LruCache
or similar: Caching Bitmaps
这篇关于减少BitmapDrawable大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!