2.Bitmap优化

来源:互联网 发布:windows loader 8.1 编辑:程序博客网 时间:2024/06/01 20:26
主要有两类方法:
一、decodeBitmap:对Bitmap不压缩,但是会根据屏幕的密度合适的进行缩放压缩
二、compressBimtap:对Bitmap进行超过最大宽高的压缩,同时也会根据屏幕的密度合适的进行缩放压缩。

publicclass BitmapDecodeUtil {
    privatestatic final int DEFAULT_DENSITY = 240;
    privatestatic final float SCALE_FACTOR = 0.75f;
    privatestatic final Bitmap.Config DEFAULT_BITMAP_CONFIG = Bitmap.Config.RGB_565;
    privatestatic BitmapFactory.Options getBitmapOptions(Context context) {
        BitmapFactory.Options options = newBitmapFactory.Options();
        options.inScaled = true;
        options.inPreferredConfig = DEFAULT_BITMAP_CONFIG;
        options.inPurgeable = true;
        options.inInputShareable = true;
        options.inJustDecodeBounds = false;
        if(Build.VERSION.SDK_INT <= Build.VERSION_CODES.GINGERBREAD_MR1) {
            Field field = null;
            try{
                field = BitmapFactory.Options.class.getDeclaredField("inNativeAlloc");
                field.setAccessible(true);
                field.setBoolean(options, true);
            catch(NoSuchFieldException e) {
                e.printStackTrace();
            catch(IllegalAccessException e) {
                e.printStackTrace();
            }
        }
        intdisplayDensityDpi = context.getResources().getDisplayMetrics().densityDpi;
        floatdisplayDensity = context.getResources().getDisplayMetrics().density;
        if(displayDensityDpi > DEFAULT_DENSITY && displayDensity > 1.5f) {
            intdensity = (int) (displayDensityDpi * SCALE_FACTOR);
            options.inDensity = density;
            options.inTargetDensity = density;
        }
        returnoptions;
    }
    publicstatic Bitmap decodeBitmap(Context context, intresId) {
        checkParam(context);
        returnBitmapFactory.decodeResource(context.getResources(), resId, getBitmapOptions(context));
    }
    publicstatic Bitmap decodeBitmap(Context context, String pathName) {
        checkParam(context);
        returnBitmapFactory.decodeFile(pathName, getBitmapOptions(context));
    }
    publicstatic Bitmap decodeBitmap(Context context, InputStream is) {
        checkParam(context);
        checkParam(is);
        returnBitmapFactory.decodeStream(is, null, getBitmapOptions(context));
    }
    publicstatic Bitmap compressBitmap(Context context,intresId, intmaxWidth, intmaxHeight) {
        checkParam(context);
        finalTypedValue value = newTypedValue();
        InputStream is = null;
        try{
            is = context.getResources().openRawResource(resId, value);
            returncompressBitmap(context, is, maxWidth, maxHeight);
        catch(Exception e) {
            e.printStackTrace();
        finally{
            if(is != null) {
                try{
                    is.close();
                catch(IOException e) {
                    e.printStackTrace();
                }
            }
        }
        returnnull;
    }
    publicstatic Bitmap compressBitmap(Context context, String pathName, intmaxWidth, intmaxHeight) {
        checkParam(context);
        InputStream is = null;
        try{
            is = newFileInputStream(pathName);
            returncompressBitmap(context, is, maxWidth, maxHeight);
        catch(FileNotFoundException e) {
            e.printStackTrace();
        finally{
            if(is != null) {
                try{
                    is.close();
                catch(IOException e) {
                    e.printStackTrace();
                }
            }
        }
        returnnull;
    }
    publicstatic Bitmap compressBitmap(Context context, InputStream is, intmaxWidth, intmaxHeight) {
        checkParam(context);
        checkParam(is);
        BitmapFactory.Options opt = newBitmapFactory.Options();
        opt.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(is, null, opt);
        intheight = opt.outHeight;
        intwidth = opt.outWidth;
        intsampleSize = computeSampleSize(width, height, maxWidth, maxHeight);
        BitmapFactory.Options options = getBitmapOptions(context);
        options.inSampleSize = sampleSize;
        returnBitmapFactory.decodeStream(is, null, options);
    }
    privatestatic int computeSampleSize(intwidth, intheight, intmaxWidth, intmaxHeight) {
        intinSampleSize = 1;
        if(height &amp;gt; maxHeight || width > maxWidth) {
            finalint heightRate = Math.round((float) height / (float) maxHeight);
            finalint widthRate = Math.round((float) width / (float) maxWidth);
            inSampleSize = heightRate < widthRate ? heightRate : widthRate;
        }
        if(inSampleSize % 2!= 0) {
            inSampleSize -= 1;
        }
        returninSampleSize &amp;lt;= 11: inSampleSize;
    }
    privatestatic &amp;lt;T&amp;gt; voidcheckParam(T param){
        if(param == null)
            thrownew NullPointerException();
    }
}
0 0