Example usage for android.graphics Bitmap recycle

List of usage examples for android.graphics Bitmap recycle

Introduction

In this page you can find the example usage for android.graphics Bitmap recycle.

Prototype

public void recycle() 

Source Link

Document

Free the native object associated with this bitmap, and clear the reference to the pixel data.

Usage

From source file:Main.java

public static void recycleImageViewBitmap(ImageView imageView) {
    if (imageView == null)
        return;/*from   w ww . ja v a 2 s . c  o m*/

    Drawable drawable = imageView.getDrawable();
    if (drawable == null)
        return;

    imageView.setImageDrawable(null);

    BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
    Bitmap bitmap = bitmapDrawable.getBitmap();
    if (bitmap != null) {
        bitmap.recycle();
    }
}

From source file:Main.java

public static void recycle(Bitmap bitmap) {
    if (bitmap == null)
        return;//from w ww .j  a v  a  2  s  .co m
    if (bitmap.isRecycled())
        return;
    bitmap.recycle();
    bitmap = null;
}

From source file:Main.java

public static void release(Bitmap... bitmaps) {
    if (bitmaps != null) {
        try {//from  www  .j a v  a  2  s.c  om
            for (Bitmap bitmap : bitmaps) {
                if (bitmap != null && !bitmap.isRecycled()) {
                    bitmap.recycle();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

From source file:Main.java

public static void recycleImageViewBitmap(ImageView imageView) {
    if (imageView == null)
        return;/*from   ww  w . j  a  va2s. com*/
    Drawable drawable = imageView.getDrawable();
    if (drawable != null && drawable instanceof BitmapDrawable) {
        BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
        Bitmap bitmap = bitmapDrawable.getBitmap();
        if (bitmap != null && !bitmap.isRecycled()) {
            bitmap.recycle();
        }
    }
}

From source file:Main.java

public static Bitmap drawViewToBitmap(View view, int width, int height, float translateX, float translateY,
        int downSampling, String color) {
    float scale = 1f / downSampling;
    int bmpWidth = (int) (width * scale - translateX / downSampling);
    int bmpHeight = (int) (height * scale - translateY / downSampling);
    Bitmap dest = Bitmap.createBitmap(bmpWidth, bmpHeight, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(dest);
    canvas.translate(-translateX / downSampling, -translateY / downSampling);
    if (downSampling > 1) {
        canvas.scale(scale, scale);//ww  w.  j  a  v a2 s.c om
    }
    Paint paint = new Paint();
    paint.setFlags(Paint.FILTER_BITMAP_FLAG | Paint.ANTI_ALIAS_FLAG);
    PorterDuffColorFilter filter = new PorterDuffColorFilter(Color.parseColor(color), PorterDuff.Mode.SRC_ATOP);
    paint.setColorFilter(filter);
    view.buildDrawingCache();
    Bitmap cache = view.getDrawingCache();
    canvas.drawBitmap(cache, 0, 0, paint);
    cache.recycle();
    view.destroyDrawingCache();

    return dest;
}

From source file:Main.java

public static void freeBitmapMap(Map map) {
    Iterator iterator = map.entrySet().iterator();
    do {//  www . jav  a2  s .  c o m
        if (!iterator.hasNext())
            break;
        Bitmap bitmap = (Bitmap) ((java.util.Map.Entry) iterator.next()).getValue();
        if (bitmap != null)
            bitmap.recycle();
    } while (true);
    map.clear();
}

From source file:Main.java

public static void releaseBitmap(Bitmap bitmap) {
    if (bitmap != null) {
        try {/*from www.j  a  va 2s  .c  om*/
            if (!bitmap.isRecycled()) {
                bitmap.recycle();
            }
        } catch (Exception e) {
        }
        bitmap = null;
    }
}

From source file:Main.java

/**
 * Garbage recycle/*  w w  w .j  a v a  2  s  . c  o m*/
 * 
 * @throws
 */
public static void recycle(Bitmap bitmap) {
    if (bitmap != null && !bitmap.isRecycled()) {
        // recycle bitmap and assign to null
        bitmap.recycle();
        bitmap = null;
    }
    System.gc();
}

From source file:Main.java

public static byte[] Bitmap2Bytes(Bitmap bm) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bm.compress(CompressFormat.PNG, 100, baos);
    bm.recycle();
    return baos.toByteArray();
}

From source file:Main.java

public static int getAverageColor(@NonNull Bitmap bitmap) {
    Bitmap onePixelBitmap = Bitmap.createScaledBitmap(bitmap, 1, 1, true);
    int color = onePixelBitmap.getPixel(0, 0);
    onePixelBitmap.recycle();
    return color;
}