Bitmap缩放(一)

先用位图的option将位图压缩一半,再用matrix缩放0.3f

public class MainActivity extends AppCompatActivity {
    WebView webView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ImageView imageView = findViewById(R.id.image);
        BitmapFactory.Options newOpts = new BitmapFactory.Options();
        newOpts.inJustDecodeBounds = false;
        newOpts.inSampleSize = 2;//设置缩放比例
        //重新读入图片,注意此时已经把options.inJustDecodeBounds 设回false了
        Bitmap bitmap= BitmapFactory.decodeResource(getResources(),    R.drawable.icon8, newOpts);

        int width =bitmap.getWidth();
        int height = bitmap.getHeight();
        // 取得想要缩放的matrix参数
        Matrix matrix = new Matrix();
        matrix.postScale(0.3f, 0.3f);
        // 得到新的图片
        Bitmap newBitMap = Bitmap.createBitmap(bitmap, 0, 0, width, height,
                matrix, true);

        imageView.setImageBitmap(newBitMap);


    }

}

猜你喜欢

转载自www.cnblogs.com/Ocean123123/p/10965894.html
今日推荐