图片压缩--BitmapFactory.Options的使用

  • 下面是压缩图片的工具类,主要的核心代码如下:
public class BitMapUtils {
    public static Bitmap zipBitMap(String filePath) {
        BitmapFactory.Options options = new BitmapFactory.Options();
        //只得到图片的宽和高
        options.inJustDecodeBounds = true;
        //得到传递过来的图片的信息
        BitmapFactory.decodeFile(filePath, options);
        //设置图片的压缩比例
        options.inSampleSize = computSampleSize(options, 200, 320);
        //设置完压缩比酷之后,必须将这个属性改为false
        options.inJustDecodeBounds = false;
        return BitmapFactory.decodeFile(filePath,options);
    }

    private static int computSampleSize(BitmapFactory.Options options, int w, int h) {
        int width = options.outWidth;
        int height = options.outHeight;
        //图片的缩小比例,只要小于等于,就是保持原图片的大小不变
        int inSqmpleSize = 1;
        if (width > w || height > h) {
            int zipSizeWidth = Math.round(width / w);
            int zipSizeHeight = Math.round(height / h);
            inSqmpleSize = zipSizeWidth < zipSizeHeight ? zipSizeWidth : zipSizeHeight;
        }
        return inSqmpleSize;
    }
}
  • 我们来看这一行代码
        options.inSampleSize = computSampleSize(options, 200, 320);
  • 上面的代码我只是一个简单的demo,我只是简单的将压缩后的大小设置为了200*320的尺寸
  • 其实我们在实际使用中可以将显示该bitmap的对象一起传递进来,来根据控件的尺寸压缩图片,就像这样
        int width = imageView.getWidth();
        int height = imageView.getHeight();
  • 再或者我们根据窗口尺寸来动态的压缩图片
        Display currentDisplay = getWindowManager().getDefaultDisplay();  
        int dw = currentDisplay.getWidth();  
        int dh = currentDisplay.getHeight();
  • 下面是逻辑代码,我们点击按钮,就会进行图片的压缩
  • 同时将压缩前后的图片的大小字节数吐司出来,证明我们的图片确实被压缩了
public class MainActivity extends AppCompatActivity implements OnClickListener {

    private Button button_getImg, button_zipImg;
    private ImageView imageView;
    private GridView gridView;
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
        initListener();
    }

    private void initListener() {
        button_zipImg.setOnClickListener(this);
        button_getImg.setOnClickListener(this);
    }

    private void init() {
        button_getImg = (Button) findViewById(R.id.button_gitImg);
        button_zipImg = (Button) findViewById(R.id.button_zipImg);
        imageView = (ImageView) findViewById(R.id.imageView);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.button_gitImg:
                Bitmap bitmap = BitmapFactory.decodeFile(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/1.png");
                Toast.makeText(MainActivity.this, "原图的大小" + bitmap.getByteCount(), Toast.LENGTH_SHORT).show();
                imageView.setImageBitmap(bitmap);
                break;
            case R.id.button_zipImg:
                Bitmap bitmap1 = BitMapUtils.zipBitMap(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/1.png");
                Toast.makeText(MainActivity.this, "压缩的大小" + bitmap1.getByteCount(), Toast.LENGTH_SHORT).show();
                imageView.setImageBitmap(bitmap1);
                break;
        }
    }
}

这里写图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值