android自定义控件Android文集Android知识

Android 图片压缩处理

2017-07-26  本文已影响123人  Luh的星河

android 图片上传前压缩


参考:https://github.com/guizhigang/LGImageCompressor

详解:

压缩比例换算:

float srcWidth = options.outWidth;
float srcHeight = options.outHeight;
float widthScale = outWidth / srcWidth;//目标/原始 宽比例
float heightScale = outHeight / srcHeight; //目标原始 高比
//对比宽高比选择较大的一种比例
float scale = widthScale > heightScale ? widthScale : heightScale;
float actualOutWidth = srcWidth;
float actualOutHeight = srcHeight;
if (scale < 1) {
    actualOutWidth = srcWidth * scale;
    actualOutHeight = srcHeight * scale;
}

设置缩放比例--生成新的位图

    Matrix matrix1 = new Matrix();
    matrix1.postScale(scale, scale);// 放大缩小比例
    //生成最终输出的bitmap
    Bitmap actualOutBitmap = Bitmap.createBitmap(scaledBitmap, 0, 0, scaledBitmap.getWidth(), scaledBitmap.getHeight(), matrix1, true);

    if (actualOutBitmap != scaledBitmap) {
        scaledBitmap.recycle();
        scaledBitmap = null;
        System.gc();
    }
上一篇 下一篇

猜你喜欢

热点阅读