我在从一个resource中解析到bitmap,获得宽高之后计算压缩比设置options.inSampleSize,但是最后压缩比例并不是我设置的压缩比例,而且小于1.
Bitmap decodeBitmap = null;
private Bitmap getMybitmap(int resource,int w,int h){
if (decodeBitmap != null)
decodeBitmap.recycle();
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(getResources(),resource,options);
int height = options.outHeight;
int width = options.outWidth;
Log.i("getbitmap",height+" "+width+" "+getWidth()+" "+getHeight());
// 调用上面定义的方法计算inSampleSize值
options.inSampleSize = calculateInSampleSize(options,w,h);
// 使用获取到的inSampleSize值再次解析图片
options.inJustDecodeBounds = false;
decodeBitmap = BitmapFactory.decodeResource(getResources(), resource, options);
//得出压缩后的图片长宽
Log.i("getbitmap2",options.outHeight+" "+options.outWidth+" "+getWidth()+" "+getHeight()+" "+options.inSampleSize);
imageWidth = options.outWidth;
imageHeight = options.outHeight;
return decodeBitmap;
}
压缩前宽高1280 1600 压缩后960 1200 这个压缩比例很奇怪。求问这是怎么回事。
你这个应该是练习官方的代码的吧。
imsample
只是缩小到原来尺寸的多少分之一,且只能是2的次幂。缩放到原尺寸的
3/4
的话,你看下是不是options
里其他的属性,我记得还有其他的两个属性能够调整缩放比例的。