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 Bitmap getRotatedBitmap(Bitmap bitmap, int degrees) {
    if (degrees != 0 && bitmap != null) {
        Matrix m = new Matrix();
        m.setRotate(degrees, (float) bitmap.getWidth() / 2, (float) bitmap.getHeight() / 2);
        try {/*from  w  w  w . j av a  2  s  .  c om*/
            Bitmap b2 = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), m, true);
            if (bitmap.equals(b2)) {
                bitmap.recycle();
                bitmap = b2;
            }
        } catch (OutOfMemoryError ex) {
            // TODO Auto-generated catch block
            ex.printStackTrace();
        }
    }
    return bitmap;
}

From source file:Main.java

private static Bitmap createScaledBitmap(File f, int rotation, int scale) throws FileNotFoundException {
    BitmapFactory.Options o2 = new BitmapFactory.Options();
    o2.inSampleSize = scale;/*from  w  w  w.j  a v  a 2 s .c  o  m*/
    Bitmap bitmap = BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
    if (0 < rotation) {
        Matrix matrix = new Matrix();
        matrix.postRotate(rotation);
        Bitmap rotatedBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix,
                true);
        if (rotatedBitmap != bitmap) {
            bitmap.recycle();
            bitmap = null;
        }
        return rotatedBitmap;
    }
    return bitmap;
}

From source file:Main.java

public static Bitmap decodeSampledBitmapFromBitmap(Bitmap bitmap, int reqWidth, int reqHeight) {
    // Calculate inSampleSize
    final Options options = new Options();
    options.inSampleSize = calculateInSampleSize(bitmap.getWidth(), bitmap.getHeight(), reqWidth, reqHeight);
    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;/*from w w  w.j  av  a 2s .  c  o  m*/
    byte[] data = bitmap2Bytes(bitmap);
    if (data != null) {
        bitmap.recycle();
        return BitmapFactory.decodeByteArray(data, 0, data.length, options);
    } else {
        return bitmap;
    }
}

From source file:Main.java

public static void initTexture(Resources res, BitmapFactory.Options opts, int resource, int handle) {
    Bitmap bmp = BitmapFactory.decodeResource(res, resource, opts);
    GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, handle);
    GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, bmp, 0);
    GLES20.glGenerateMipmap(GLES20.GL_TEXTURE_2D);
    GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_LINEAR_MIPMAP_LINEAR);
    GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR);
    bmp.recycle();
}

From source file:Main.java

public static Bitmap rotate(Bitmap bitmap, int degrees) {
    if (degrees != 0 && bitmap != null) {
        Matrix m = new Matrix();
        m.setRotate(degrees, (float) bitmap.getWidth() / 2, (float) bitmap.getHeight() / 2);

        try {//from  w  w  w .  j a va 2  s . com
            Bitmap converted = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), m,
                    true);
            if (bitmap != converted) {
                bitmap.recycle();
                bitmap = converted;
            }
        } catch (OutOfMemoryError ex) {
            // if out of memory, return original bitmap
        }
    }
    return bitmap;
}

From source file:Main.java

public static Drawable createGradientColorAndCover(Context context, int coveredResID, int CoverColor) {
    //final Resources res=context.getResources();
    final Resources res = context.getResources();
    Bitmap bitmap = BitmapFactory.decodeResource(res, coveredResID);
    if (bitmap == null) {
        return null;
    }//from   ww  w  . j  av a2s. c o m
    final int length = bitmap.getWidth();
    final int height = bitmap.getHeight();
    int[] shape = new int[length * height];
    int[] colors = new int[length * height];
    bitmap.getPixels(shape, 0, length, 0, 0, length, height);
    int color = CoverColor;
    for (int i = 0; i < length * height; i++) {
        float percent = ((float) i % length / length) * 0xff;
        int alpha = ((int) percent << 6 * 4);
        alpha &= shape[i] & 0xFF000000;
        colors[i] = (alpha) | (color & 0x00FFFFFF);
    }
    Bitmap newbitmap = Bitmap.createBitmap(length, height, Bitmap.Config.ARGB_8888);
    newbitmap.setPixels(colors, 0, length, 0, 0, length, height);
    Bitmap fooBitmap = Bitmap.createBitmap(length, height, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(fooBitmap);
    Paint paint = new Paint();
    canvas.drawBitmap(bitmap, 0, 0, paint);
    canvas.drawBitmap(newbitmap, 0, 0, paint);
    newbitmap.recycle();
    bitmap.recycle();
    return new BitmapDrawable(res, fooBitmap);
}

From source file:Main.java

public static int setTexture(Bitmap bitmap) {
    GLES20.glGenTextures(1, texturenames, 0);

    GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
    GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, texturenames[0]);

    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);

    GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, bitmap, 0);
    bitmap.recycle();

    return texturenames[0];
}

From source file:helloopengles.example.com.MyGLRenderer.java

public static Bitmap loadImage(String fileName) {
    try {/*from www. jav a2  s. c  o m*/
        Bitmap tmp = BitmapFactory.decodeStream(OpenGLES20Activity.context.getAssets().open(fileName));
        android.graphics.Matrix matrix = new android.graphics.Matrix();
        matrix.preScale(1.0f, -1.0f);
        Bitmap image = Bitmap.createBitmap(tmp, 0, 0, tmp.getWidth(), tmp.getHeight(), matrix, true);
        tmp.recycle();
        return image;
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static int loadTexture(Resources resources, int resource, int internalFormat, boolean flip) {
    int[] textures = s_LOAD_TEXTURE_ID.get();
    if (textures == null) {
        textures = new int[1];
        s_LOAD_TEXTURE_ID.set(textures);
    }/*from  ww  w .  j a  v  a2s . c o  m*/

    glActiveTexture(GL_TEXTURE0);
    glGenTextures(1, textures, 0);

    int texture = textures[0];
    glBindTexture(GL_TEXTURE_2D, texture);

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

    Bitmap bitmap = BitmapFactory.decodeResource(resources, resource);

    final int width = bitmap.getWidth();
    final int height = bitmap.getHeight();

    if (flip) {
        Matrix matrix = new Matrix();
        matrix.setScale(1, -1);
        matrix.postTranslate(0, height);
        Bitmap flipBitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);

        bitmap.recycle();
        bitmap = flipBitmap;
    }

    GLUtils.texImage2D(GL_TEXTURE_2D, 0, internalFormat, bitmap, GL_UNSIGNED_BYTE, 0);

    bitmap.recycle();

    glBindTexture(GL_TEXTURE_2D, 0);

    return texture;
}

From source file:Main.java

public final static Bitmap rotationBitmap(Bitmap srcBitmap, float degrees) {
    Bitmap result = null;/*from www .  j a va 2 s. co  m*/
    if (degrees != 0 && srcBitmap != null) {
        Matrix m = new Matrix();
        m.setRotate(degrees, (float) srcBitmap.getWidth() / 2, (float) srcBitmap.getHeight() / 2);
        try {
            Bitmap b2 = Bitmap.createBitmap(srcBitmap, 0, 0, srcBitmap.getWidth(), srcBitmap.getHeight(), m,
                    true);
            if (srcBitmap != b2) {
                srcBitmap.recycle();
                srcBitmap = b2;
            }
            result = b2;
        } catch (OutOfMemoryError ex) {
            ex.printStackTrace();
        }
    }
    return result;
}