圖片壓縮--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;
        }
    }
}

這裏寫圖片描述

發佈了121 篇原創文章 · 獲贊 18 · 訪問量 7萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章