Android剪裁图片

ImageView iv_01 = (ImageView) findViewById(R.id.iv_01);
ImageView iv_02 = (ImageView) findViewById(R.id.iv_02);
ImageView iv_03= (ImageView) findViewById(R.id.iv_03);
ImageView iv_04 = (ImageView) findViewById(R.id.iv_04);

//第一种方法:压缩质量
File saveFile = getFileStreamPath("compress.jpg");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.photo);
bitmap.compress(Bitmap.CompressFormat.JPEG, 10, baos);
try {
    FileOutputStream fos = new FileOutputStream(saveFile);
    fos.write(baos.toByteArray());
    fos.flush();
    fos.close();
} catch (Exception e) {
    e.printStackTrace();
}
bitmap = BitmapFactory.decodeFile(saveFile.getAbsolutePath());
iv_01.setImageBitmap(bitmap);

//第二种方法:尺寸缩放
int ratio = 4;
bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.photo);
int width = bitmap.getWidth() / 4;
int height = bitmap.getHeight() / 4;
Rect rect = new Rect(0, 0, width, height);
Bitmap bit = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bit);
canvas.drawBitmap(bitmap, null, rect, null);
iv_02.setImageBitmap(bit);

//第三种方法:设置采样率
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(getResources(), R.drawable.photo, options);
int w = options.outWidth;
int h = options.outHeight;
int standard = 100;
int sample = w / 100;
options.inJustDecodeBounds = false;
options.inSampleSize = sample;
bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.photo, options);
iv_03.setImageBitmap(bitmap);
Log.e("TAG", w+"");
Log.e("TAG", h+"");

iv_04.setImageResource(R.drawable.photo);

本人水平有限,如有问题请及时与我联系

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值