Android中处理大图片时图片压缩

1、BitmapFactory.Options中的属性

在进行图片压缩时,是通过设置BitmapFactory.Options的一些值来改变图片的属性的,下面我们来看看BitmapFactory.Options中常用的属性意思:

  • options.inPreferredConfig - 设置Bitmap的偏好配置,值有Bitmap.Config.ARGB_8888,Bitmap.Config.ARGB_4444,Bitmap.Config.ARGB_8888,Bitmap.Config.RGB_565。默认为ARGB_8888,顾名思义,这是设置Bitmap的显示质量的。
  • options.outHeight - 得到该Bitmap的高。
  • options.outWidth - 得到该Bitmap的宽。
  • options.outMimeType - 得到该Bitmap的MIME值。
  • options.inSampleSize - 压缩比例,如果options.inSampleSize = 4;那么压缩后的图片的宽和高都为原来图片的1/4,压缩后的图片的像素为原来图片的1/16。
  • options.inJustDecodeBounds  - 官方文档上是这样介绍的:
      • If set to true, the decoder will return null (no bitmap), but
        the out... fields will still be set, allowing the caller to query
        the bitmap without having to allocate the memory for its pixels.
      • 意思就是:如果设置为true,那么使用BitmapFactory.decodeStream(InputStream is, Rect outPadding, 
        Options opts)或BitmapFactory.decodeXXX(....,Options opts)方法并不会真的返回一个Bitmap对象,而是返回null,虽然返回null,但是我们却可以通过options来获得该Bitmap的一些值,如它的宽、高、MimeType等值。这样就不必为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);
        上述代码输出的是:可以知道确实是返回null了,而且还获得了该Bitmap的宽高等值。

2、通过实例来看看怎么压缩图片

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可以知道压缩后图片的宽高:


我们再看看效果图:


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值