limit Bitmap Size - Android Graphics

Android examples for Graphics:Bitmap Size

Description

limit Bitmap Size

Demo Code



//package com.book2s;

import android.graphics.Bitmap;

import android.graphics.BitmapFactory;

import android.graphics.Matrix;
import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;

import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

public class Main {

    public static Bitmap limitSize(String path, int maxSize) {
        if (path == null || path.length() == 0)
            return null;

        BitmapFactory.Options opts = decodeBounds(path);

        int sample = 0;
        int nextSample = 1;
        do {/*from  w  w  w  .j a  va2 s.c o m*/
            sample = nextSample;
            nextSample++;
        } while (opts.outWidth / nextSample >= maxSize
                && opts.outHeight / nextSample >= maxSize);
        opts.inSampleSize = sample;
        opts.inJustDecodeBounds = false;
        Bitmap tempBmp = BitmapFactory.decodeFile(path, opts);
        Bitmap bmp = limitSize(tempBmp, maxSize);
        if (tempBmp != bmp) {
            tempBmp.recycle();
        }
        return bmp;
    }

    public static Bitmap limitSize(Bitmap src, int maxSize) {
        if (src == null) {
            return null;
        }

        float scale = measureScale(src, maxSize);
        if (scale == 1) {
            return src;
        }
        return scale(src, scale);
    }

    public static BitmapFactory.Options decodeBounds(String path) {
        BitmapFactory.Options opts = createOptions();
        opts.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(path, opts);
        return opts;
    }

    public static void recycle(ViewGroup parent) {
        for (int i = 0; i < parent.getChildCount(); i++) {
            View child = parent.getChildAt(i);
            if (child instanceof ViewGroup) {
                recycle((ViewGroup) child);
            } else if (child instanceof ImageView) {
                recycle((ImageView) child);
            }
        }
    }

    public static void recycle(ImageView iv) {
        if (iv == null)
            return;
        Drawable drawable = iv.getDrawable();
        if (drawable != null) {
            iv.setImageBitmap(null);
            if (drawable instanceof BitmapDrawable) {
                BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
                Bitmap bmp = bitmapDrawable.getBitmap();
                recycle(bmp);
            } else if (drawable instanceof AnimationDrawable) {
                AnimationDrawable animationDrawable = (AnimationDrawable) drawable;
                recycle(animationDrawable);
            }
        }
    }

    public static void recycle(AnimationDrawable ad) {
        for (int i = 0; i < ad.getNumberOfFrames(); i++) {
            Drawable frame = ad.getFrame(i);
            if (frame instanceof BitmapDrawable) {
                Bitmap tempBmp = ((BitmapDrawable) frame).getBitmap();
                recycle(tempBmp);
            }
        }
    }

    public static void recycle(Bitmap bmp) {
        if (bmp != null && !bmp.isRecycled()) {
            bmp.recycle();
        }
    }

    public static float measureScale(Bitmap src, int maxSize) {
        return measureScale(src.getWidth(), src.getHeight(), maxSize);
    }

    public static float measureScale(String path, int maxSize) {
        BitmapFactory.Options op = decodeBounds(path);
        return measureScale(op.outWidth, op.outHeight, maxSize);
    }

    public static float measureScale(int width, int height, int maxSize) {
        int longSize = width > height ? width : height;
        if (longSize <= maxSize) {
            return 1;
        }
        return (float) maxSize / longSize;
    }

    public static Bitmap scale(Bitmap src, float scale) {
        if (src == null) {
            return null;
        }

        Matrix m = new Matrix();
        m.setScale(scale, scale);
        return Bitmap.createBitmap(src, 0, 0, src.getWidth(),
                src.getHeight(), m, true);
    }

    @SuppressWarnings("deprecation")
    public static BitmapFactory.Options createOptions() {
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inPreferredConfig = Bitmap.Config.RGB_565;
        options.inPurgeable = true;
        options.inInputShareable = true;
        options.inDither = false;
        return options;
    }

    @SuppressWarnings("deprecation")
    public static BitmapFactory.Options createOptions(
            Bitmap.Config colorConfig) {
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inPreferredConfig = colorConfig;
        options.inPurgeable = true;
        options.inInputShareable = true;
        options.inDither = false;
        return options;
    }
}

Related Tutorials