标签:tco 配置 str oid resources private target 并且 tor
在进行图片压缩时,是通过设置BitmapFactory.Options的一些值来改变图片的属性的,以下我们来看看BitmapFactory.Options中经常使用的属性意思:
默觉得ARGB_8888,顾名思义。这是设置Bitmap的显示质量的。
这样就不必为Bitmap对象分配内存空间也能够获得它的Width和Height。从而节约内存。所以这个属性对我们压缩图片前进行检查大有帮助。经常使用的技巧就是为了避免图片过大而导致OOM,所以在我们载入图片之前就通过设置它为true,获取到图片的宽、高等值,然后进行一些推断检查等。决定图片是否压缩。我们来看看载入一张405x579图片的样例:
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
Bitmap bitmap = BitmapFactory.decodeResource(getResource(), R.mipmap.two, options);
Log.v("zxy","bitmap="+bitmap);
int height = options.outHeight;
int width = options.outWidth;
String mimeType = options.outMimeType;
Log.v("zxy","mimeType="+mimeType+",height="+height+",width="+width);上述代码输出的是:public class MainActivity extends ActionBarActivity {
private ImageView mImageView, mResizeImageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mImageView = (ImageView) findViewById(R.id.imageView);
mResizeImageView = (ImageView) findViewById(R.id.resize_imageView);
mImageView.setImageBitmap(BitmapFactory.decodeResource(getResources(),R.mipmap.two));
Bitmap bitmap = compressBitmap(getResources(), R.mipmap.two, 100, 100);
Log.v("zxy", "compressBitmap,width=" + bitmap.getWidth() + ",height=" + bitmap.getHeight());
mResizeImageView.setImageBitmap(bitmap);
}
/**
* @param res Resource
* @param resId 资源id
* @param targetWidth 目标图片的宽,单位px
* @param targetHeight 目标图片的高,单位px
* @return 返回压缩后的图片的Bitmap
*/
public Bitmap compressBitmap(Resources res, int resId, int targetWidth, int targetHeight) {
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;//设为true,节约内存
BitmapFactory.decodeResource(res, resId, options);//返回null
int height = options.outHeight;//得到源图片height,单位px
int width = options.outWidth;//得到源图片的width。单位px
//计算inSampleSize
options.inSampleSize = calculateInSampleSize(width,height,targetWidth,targetHeight);
options.inJustDecodeBounds = false;//设为false,能够返回Bitmap
return BitmapFactory.decodeResource(res,resId,options);
}
/**
* 计算压缩比例
* @param width 源图片的宽
* @param height 源图片的高
* @param targetWidth 目标图片的宽
* @param targetHeight 目标图片的高
* @return inSampleSize 压缩比例
*/
public int calculateInSampleSize(int width,int height, int targetWidth, int targetHeight) {
int inSampleSize = 1;
if (height > targetHeight || width > targetWidth) {
//计算图片实际的宽高和目标图片宽高的比率
final int heightRate = Math.round((float) height / (float) targetHeight);
final int widthRate = Math.round((float) width / (float) targetWidth);
//选取最小的比率作为inSampleSize
inSampleSize = heightRate < widthRate ? heightRate : widthRate;
}
return inSampleSize;
}
}我们通过看Log能够知道压缩后图片的宽高:
我们再看看效果图:
标签:tco 配置 str oid resources private target 并且 tor
原文地址:http://www.cnblogs.com/lxjshuju/p/6884476.html