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);
本人水平有限,如有问题请及时与我联系
Android剪裁图片
最新推荐文章于 2024-08-09 07:39:19 发布