加载图片
-
加载大图: 3648 * 2736
总像素的个数: 9980928 每一个像素占用的空间: ARGB_4444(2bytes), ARGB_8888(4bytes), RGB_565(2bytes) 图片占用的空间 = 总像素的个数 * 像素的单位. 9980928 * 2bytes = 19961856bytes = 19.037109375MB
-
缩放加载.
- 图片的宽和高: 3648 * 2736
-
屏幕的宽和高: 320 * 480
-
计算缩放比例
- 宽度缩放比例: 3648 / 320 = 11;
-
高度缩放比例: 2736 / 480 = 5;
-
比较宽和高的缩放比例, 哪一个大用哪一个进行缩放.
-
进行缩放: 331 * 248 = 82088 * 2bytes = 164176bytes = 160KB
3648 / 11 = 331; 2736 / 11 = 248;
核心代码: String picpath = et_picpath.getText().toString().trim(); Options opts = new Options(); opts.inJustDecodeBounds = true; BitmapFactory.decodeFile(picpath, opts); int picHeight = opts.outHeight; int picWidth = opts.outWidth; WindowManager manager = getWindowManager(); int screenHeight = manager.getDefaultDisplay().getHeight(); int screenWidth = manager.getDefaultDisplay().getWidth(); int heightScale = picHeight / screenHeight; int widthScale = picWidth / screenWidth; int scale = heightScale > widthScale ? heightScale : widthScale; opts.inJustDecodeBounds = false; opts.inSampleSize = scale; Bitmap bitmap = BitmapFactory.decodeFile(picpath, opts); iv_image.setImageBitmap(bitmap);