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:com.amitupadhyay.aboutexample.util.glide.DribbbleTarget.java

@Override
public void onResourceReady(GlideDrawable resource, GlideAnimation<? super GlideDrawable> animation) {
    super.onResourceReady(resource, animation);
    if (!autoplayGifs) {
        resource.stop();/* w w  w.  ja v a  2s. c  om*/
    }

    BadgedFourThreeImageView badgedImageView = (BadgedFourThreeImageView) getView();
    if (resource instanceof GlideBitmapDrawable) {
        Palette.from(((GlideBitmapDrawable) resource).getBitmap()).clearFilters().generate(this);
    } else if (resource instanceof GifDrawable) {
        Bitmap image = ((GifDrawable) resource).getFirstFrame();
        Palette.from(image).clearFilters().generate(this);

        // look at the corner to determine the gif badge color
        int cornerSize = (int) (56 * getView().getContext().getResources().getDisplayMetrics().scaledDensity);
        Bitmap corner = Bitmap.createBitmap(image, image.getWidth() - cornerSize,
                image.getHeight() - cornerSize, cornerSize, cornerSize);
        boolean isDark = ColorUtils.isDark(corner);
        corner.recycle();
        badgedImageView.setBadgeColor(ContextCompat.getColor(getView().getContext(),
                isDark ? R.color.gif_badge_dark_image : R.color.gif_badge_light_image));
    }
}

From source file:Main.java

public static Bitmap normalizeExifRotateBitmap(Bitmap bitmap, int orientation) {

    Matrix matrix = new Matrix();
    switch (orientation) {
    case ExifInterface.ORIENTATION_NORMAL:
        return bitmap;
    case ExifInterface.ORIENTATION_FLIP_HORIZONTAL:
        matrix.setScale(-1, 1);/*from w ww . jav  a 2 s .  c o m*/
        break;
    case ExifInterface.ORIENTATION_ROTATE_180:
        matrix.setRotate(180);
        break;
    case ExifInterface.ORIENTATION_FLIP_VERTICAL:
        matrix.setRotate(180);
        matrix.postScale(-1, 1);
        break;
    case ExifInterface.ORIENTATION_TRANSPOSE:
        matrix.setRotate(90);
        matrix.postScale(-1, 1);
        break;
    case ExifInterface.ORIENTATION_ROTATE_90:
        matrix.setRotate(90);
        break;
    case ExifInterface.ORIENTATION_TRANSVERSE:
        matrix.setRotate(-90);
        matrix.postScale(-1, 1);
        break;
    case ExifInterface.ORIENTATION_ROTATE_270:
        matrix.setRotate(-90);
        break;
    default:
        return bitmap;
    }
    try {
        Bitmap bmRotated = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix,
                true);
        bitmap.recycle();
        return bmRotated;
    } catch (OutOfMemoryError e) {
        e.printStackTrace();
        return null;
    }
}

From source file:Main.java

public static Bitmap rotateBitmap(String src, Bitmap bitmap) {
    try {/*  w w w . j  a  v a 2 s.  co  m*/
        int orientation = getExifOrientation(src);

        if (orientation == 1) {
            return bitmap;
        }

        Matrix matrix = new Matrix();
        switch (orientation) {
        case 2:
            matrix.setScale(-1, 1);
            break;
        case 3:
            matrix.setRotate(180);
            break;
        case 4:
            matrix.setRotate(180);
            matrix.postScale(-1, 1);
            break;
        case 5:
            matrix.setRotate(90);
            matrix.postScale(-1, 1);
            break;
        case 6:
            matrix.setRotate(90);
            break;
        case 7:
            matrix.setRotate(-90);
            matrix.postScale(-1, 1);
            break;
        case 8:
            matrix.setRotate(-90);
            break;
        default:
            return bitmap;
        }

        try {
            Bitmap oriented = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix,
                    true);
            bitmap.recycle();
            return oriented;
        } catch (OutOfMemoryError e) {
            e.printStackTrace();
            return bitmap;
        }
    } catch (IOException e) {
        e.printStackTrace();
    }

    return bitmap;
}

From source file:Main.java

@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
public static Bitmap blur(Context context, Bitmap bitmap) {

    //Let's create an empty bitmap with the same size of the bitmap we want to blur
    Bitmap outBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888);

    //Instantiate a new Renderscript
    RenderScript rs = RenderScript.create(context);

    //Create an Intrinsic Blur Script using the Renderscript
    ScriptIntrinsicBlur blurScript = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));

    //Create the Allocations (in/out) with the Renderscript and the in/out bitmaps
    Allocation allIn = Allocation.createFromBitmap(rs, bitmap);
    Allocation allOut = Allocation.createFromBitmap(rs, outBitmap);

    //Set the radius of the blur
    blurScript.setRadius(25.f);//from   www. j  a  va 2s.co  m

    //Perform the Renderscript
    blurScript.setInput(allIn);
    blurScript.forEach(allOut);

    //Copy the final bitmap created by the out Allocation to the outBitmap
    allOut.copyTo(outBitmap);

    //recycle the original bitmap
    bitmap.recycle();

    //After finishing everything, we destroy the Renderscript.
    rs.destroy();

    return outBitmap;
}

From source file:Main.java

/**
 * create scaled bitmap with required width and height
 *
 * @param srcBitmap//from w ww  .ja v a 2s.com
 * @param reqWidth
 * @param reqHeight
 * @param recycleOrig
 * @param scaleType
 * @return
 */
public synchronized static Bitmap createBitmap(Bitmap srcBitmap, int reqWidth, int reqHeight,
        boolean recycleOrig, int scaleType) {
    int bitmapWidth = srcBitmap.getWidth();
    int bitmapHeight = srcBitmap.getHeight();
    if (reqWidth == 0)
        reqWidth = bitmapWidth;
    if (reqHeight == 0)
        reqHeight = bitmapHeight;

    //        final Rect srcRect = new Rect(0, 0, srcBitmap.getWidth(), srcBitmap.getHeight());
    //        final Rect dstRect = new Rect(0, 0, reqWidth, reqHeight);
    float scaleWidth = 1;
    float scaleHeight = 1;
    if (scaleType == SCALE_TYPE_FIT_START) {
        scaleWidth = (reqWidth / bitmapWidth < reqHeight / bitmapHeight)
                ? (float) reqWidth / (float) bitmapWidth
                : (float) reqHeight / (float) bitmapHeight;
        scaleHeight = scaleWidth;
    } else if (scaleType == SCALE_TYPE_FIT_XY) {
        scaleWidth = (float) reqWidth / (float) bitmapWidth;
        scaleHeight = (float) reqHeight / (float) bitmapHeight;
    }

    Matrix matrix = new Matrix();
    matrix.postScale(scaleWidth, scaleHeight);

    Bitmap resizedBitmap = Bitmap.createBitmap(srcBitmap, 0, 0, bitmapWidth, bitmapHeight, matrix, true);

    if (recycleOrig) {
        srcBitmap.recycle();
    }

    return resizedBitmap;
}

From source file:Main.java

public static int loadTexture(Bitmap bitmap) {
    final int[] textureHandle = new int[1];

    GLES20.glGenTextures(1, textureHandle, 0);

    if (textureHandle[0] != 0) {
        //           final BitmapFactory.Options options = new BitmapFactory.Options();
        //           options.inScaled = false;   // No pre-scaling

        // Read in the resource
        //           final Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), resourceId, options);

        // Bind to the texture in OpenGL
        GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureHandle[0]);

        // Set filtering
        GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_LINEAR);
        GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR);
        GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_CLAMP_TO_EDGE); // Set U Wrapping
        GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_CLAMP_TO_EDGE); // Set V Wrapping

        // Load the bitmap into the bound texture.
        GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, bitmap, 0);

        // Recycle the bitmap, since its data has been loaded into OpenGL.
        bitmap.recycle();
    }//from w ww.ja va  2  s. co m

    if (textureHandle[0] == 0) {
        throw new RuntimeException("Error loading texture.");
    }

    return textureHandle[0];
}

From source file:Main.java

public static Bitmap getDefaultArtwork(Context context, int id, int w, int h) {
    BitmapFactory.Options sBitmapOptionsCache = new BitmapFactory.Options();
    Bitmap b = null;
    int sampleSize = 1;

    sBitmapOptionsCache.inPreferredConfig = Bitmap.Config.ARGB_8888;

    // Compute the closest power-of-two scale factor 
    // and pass that to sBitmapOptionsCache.inSampleSize, which will
    // result in faster decoding and better quality
    sBitmapOptionsCache.inJustDecodeBounds = true;

    BitmapFactory.decodeResource(context.getResources(), id, sBitmapOptionsCache);
    int nextWidth = sBitmapOptionsCache.outWidth >> 1;
    int nextHeight = sBitmapOptionsCache.outHeight >> 1;
    while (nextWidth > w && nextHeight > h) {
        sampleSize <<= 1;//from  www. ja va  2s  .c o m
        nextWidth >>= 1;
        nextHeight >>= 1;
    }

    sBitmapOptionsCache.inSampleSize = sampleSize;
    sBitmapOptionsCache.inJustDecodeBounds = false;
    b = BitmapFactory.decodeResource(context.getResources(), id, sBitmapOptionsCache);

    if (b != null) {
        // finally rescale to exactly the size we need
        if (sBitmapOptionsCache.outWidth != w || sBitmapOptionsCache.outHeight != h) {
            Bitmap tmp = Bitmap.createScaledBitmap(b, w, h, true);
            // Bitmap.createScaledBitmap() can return the same bitmap
            if (tmp != b)
                b.recycle();
            b = tmp;
        }
    }

    return b;
}

From source file:com.google.zxing.client.android.ViewfinderView.java

public void drawViewfinder() {
    Bitmap resultBitmap = this.resultBitmap;
    this.resultBitmap = null;
    if (resultBitmap != null) {
        resultBitmap.recycle();/*  www .  j  ava 2 s  .c o  m*/
    }
    invalidate();
}

From source file:com.foregroundgalleryplugin.ForegroundGalleryLauncher.java

/**
 * Called when the camera view exits./*from  ww w  .  j  a v a 2  s  .c om*/
 * 
 * @param requestCode
 *            The request code originally supplied to
 *            startActivityForResult(), allowing you to identify who this
 *            result came from.
 * @param resultCode
 *            The integer result code returned by the child activity through
 *            its setResult().
 * @param intent
 *            An Intent, which can return result data to the caller (various
 *            data can be attached to Intent "extras").
 */
@Override
public void onActivityResult(int requestCode, int resultCode, Intent intent) {

    if (resultCode == Activity.RESULT_OK) {

        Uri uri = intent.getData();
        ContentResolver resolver = this.cordova.getActivity().getContentResolver();

        try {
            Bitmap bitmap = android.graphics.BitmapFactory.decodeStream(resolver.openInputStream(uri));
            bitmap = scaleBitmap(bitmap);
            this.processPicture(bitmap);
            bitmap.recycle();
            bitmap = null;
            System.gc();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            this.failPicture("Error retrieving image.");
        }
    } else if (resultCode == Activity.RESULT_CANCELED) {
        this.failPicture("Selection cancelled.");
    } else {
        this.failPicture("Selection did not complete!");
    }
}

From source file:com.audiokernel.euphonyrmt.fragments.NowPlayingSmallFragment.java

@Override
public void onDestroyView() {
    if (mCoverArt != null) {
        final Drawable oldDrawable = mCoverArt.getDrawable();
        mCoverArt.setImageResource(AlbumCoverDownloadListener.getNoCoverResource());
        if (oldDrawable != null && oldDrawable instanceof CoverBitmapDrawable) {
            final Bitmap oldBitmap = ((BitmapDrawable) oldDrawable).getBitmap();
            if (oldBitmap != null) {
                oldBitmap.recycle();
            }//  w  w  w . ja  v a  2 s .  co m
        }
    }
    super.onDestroyView();
}