Android中3种图片压缩处理方法

Android中3种图片压缩处理方法,第1张

概述Android中图片的存在形式:1:文件形式:二进制形式存在与硬盘中。2:流的形式:二进制形式存在与内存中。

AndroID中图片的存在形式:

1:文件形式:二进制形式存在与硬盘中。
2:流的形式:二进制形式存在与内存中。
3:Bitmap的形式

三种形式的区别:
文件形式和流的形式:对图片体积大小并没有影响。也就是说,如果你手机SD卡上的图片通过流的形式读到内存中,在内存中的大小也是原图的大小。
注意:不是Bitmap的形式。
Bitmap的形式:图片占用的内存会瞬间变大。
以下是代码的形式:

    

 /**   * 图片压缩的方法总结   */  /*   * 图片压缩的方法01:质量压缩方法   */  private Bitmap compressImage(Bitmap beforBitmap) {    // 可以捕获内存缓冲区的数据,转换成字节数组。    ByteArrayOutputStream bos = new ByteArrayOutputStream();    if (beforBitmap != null) {      // 第一个参数:图片压缩的格式;第二个参数:压缩的比率;第三个参数:压缩的数据存放到bos中      beforBitmap.compress(CompressFormat.JPEG,100,bos);      int options = 100;      // 循环判断压缩后的图片是否是大于100kb,如果大于,就继续压缩,否则就不压缩      while (bos.toByteArray().length / 1024 > 100) {        bos.reset();// 置为空        // 压缩options%        beforBitmap.compress(CompressFormat.JPEG,options,bos);        // 每次都减少10        options -= 10;      }      // 从bos中将数据读出来 存放到ByteArrayinputStream中      ByteArrayinputStream bis = new ByteArrayinputStream(          bos.toByteArray());      // 将数据转换成图片      Bitmap afterBitmap = BitmapFactory.decodeStream(bis);      return afterBitmap;    }    return null;  }  /*   * 图片压缩方法02:获得缩略图   */  public Bitmap getthumbnail(int ID) {    // 获得原图    Bitmap beforeBitmap = BitmapFactory.decodeResource(        mContext.getResources(),ID);    // 宽    int w = mContext.getResources()        .getDimensionPixelOffset(R.dimen.image_w);    // 高    int h = mContext.getResources().getDimensionPixelSize(R.dimen.image_h);    // 获得缩略图    Bitmap afterBitmap = thumbnailUtils        .extractthumbnail(beforeBitmap,w,h);    return afterBitmap;  }  /**   * 图片压缩03   *    * @param ID   *      要 *** 作的图片的大小   * @param newWIDth   *      图片指定的宽度   * @param newHeight   *      图片指定的高度   * @return   */  public Bitmap compressBitmap(int ID,double newWIDth,double newHeight) {    // 获得原图    Bitmap beforeBitmap = BitmapFactory.decodeResource(        mContext.getResources(),ID);    // 图片原有的宽度和高度    float beforeWIDth = beforeBitmap.getWIDth();    float beforeHeight = beforeBitmap.getHeight();    // 计算宽高缩放率    float scaleWIDth = 0;    float scaleHeight = 0;    if (beforeWIDth > beforeHeight) {      scaleWIDth = ((float) newWIDth) / beforeWIDth;      scaleHeight = ((float) newHeight) / beforeHeight;    } else {      scaleWIDth = ((float) newWIDth) / beforeHeight;      scaleHeight = ((float) newHeight) / beforeWIDth;    }    // 矩阵对象    Matrix matrix = new Matrix();    // 缩放图片动作 缩放比例    matrix.postscale(scaleWIDth,scaleHeight);    // 创建一个新的Bitmap 从原始图像剪切图像    Bitmap afterBitmap = Bitmap.createBitmap(beforeBitmap,(int) beforeWIDth,(int) beforeHeight,matrix,true);    return afterBitmap;  }

您可能感兴趣的文章:Android图片处理:识别图像方向并显示实例教程Android 异步获取网络图片并处理导致内存溢出问题解决方法Android图片处理实例介绍(图)android图片处理之让图片变成圆形@L_403_4@android图片圆角、图片去色处理示例android图片处理之让图片一直匀速旋转Android编程之图片颜色处理方法Android 图片特效处理的方法实例Android图片处理工具类BitmapUtils 总结

以上是内存溢出为你收集整理的Android中3种图片压缩处理方法全部内容,希望文章能够帮你解决Android中3种图片压缩处理方法所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1142874.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-31
下一篇2022-05-31

发表评论

登录后才能评论

评论列表(0条)

    保存