Android中图片处理

加载图片

  • 加载大图: 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);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值