在良好的品质和内存资源的有效缩减位图 [英] Scale down Bitmap from resource in good quality and memory efficient
问题描述
欲缩小一个500x500px资源以适应总是由在屏幕的宽度来确定的特定大小。结果
目前我使用code从Android开发者网站(加载大型高效位图的),但质量不如我会用500x500px资源上的的ImageView
(如XML源),只是缩放的ImageView
,而不是位图。
但它的慢,我要缩放的位图
,也为内存高效,快速。
编辑:我想规模在提拉我的应用程序绘制
文件夹中。
EDIT2:我目前的做法
正确的图像是图像源在XML和我想用缩放位图达到的质量定义的ImageView的。
私人位图德$ C $的CImage(文件f){
位图B = NULL;
尝试{
BitmapFactory.Options O =新BitmapFactory.Options();
o.inJustDe codeBounds = TRUE; FIS的FileInputStream =新的FileInputStream(F);
BitmapFactory.de codeStream(FIS,空,O);
fis.close(); 浮SC = 0.0;
int标= 1;
//如果图像高度大于宽度
如果(o.outHeight> o.outWidth){
SC = o.outHeight / 400;
规模= Math.round(SC);
}
//如果图像的宽度大于高度
其他{
SC = o.outWidth / 400;
规模= Math.round(SC);
} //德code。与inSampleSize
BitmapFactory.Options O2 =新BitmapFactory.Options();
o2.inSampleSize =规模;
FIS =新的FileInputStream(F);
B = BitmapFactory.de codeStream(FIS,空,O2);
fis.close();
}赶上(IOException异常五){
}
返回b;
}
下面'400'是新的宽度(如果图像是在纵向模式下)或新的高度(如果图像是横向模式)。你可以设置你自己的选择..缩放位的值不会花费太多的内存空间。
I want to scale down a 500x500px resource to fit always a specific size which is determined by the width of the screen.
Currently I use the code from the Android Developers Site (Loading Large Bitmaps Efficiently), but the quality is not as good as I would use the 500x500px resource in a ImageView
(as source in xml) and just scale the ImageView
and not the Bitmap.
But it's slow and I want to scale the Bitmap
, too, to be memory efficient and fast.
Edit: The drawable which I wanna scale is in the drawable
folder of my app.
Edit2: My current approaches.
The left image is the method from Loading Large Bitmaps Efficiently without any modifications. The center image is done with the method provided by @Salman Zaidi with this little modification: o.inPreferredConfig = Config.ARGB_8888;
and o2.inPreferredConfig = Config.ARGB_8888;
The right image is an imageview where the image source is defined in xml and the quality I wanna reach with a scaled bitmap.
private Bitmap decodeImage(File f) {
Bitmap b = null;
try {
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
FileInputStream fis = new FileInputStream(f);
BitmapFactory.decodeStream(fis, null, o);
fis.close();
float sc = 0.0f;
int scale = 1;
//if image height is greater than width
if (o.outHeight > o.outWidth) {
sc = o.outHeight / 400;
scale = Math.round(sc);
}
//if image width is greater than height
else {
sc = o.outWidth / 400;
scale = Math.round(sc);
}
// Decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
fis = new FileInputStream(f);
b = BitmapFactory.decodeStream(fis, null, o2);
fis.close();
} catch (IOException e) {
}
return b;
}
Here '400' is the new width (in case image is in portrait mode) or new height (in case image is in landscape mode). You can set the value of your own choice.. Scaled bitmap will not take much memory space..
这篇关于在良好的品质和内存资源的有效缩减位图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!