Example usage for android.graphics.drawable BitmapDrawable getBitmap

List of usage examples for android.graphics.drawable BitmapDrawable getBitmap

Introduction

In this page you can find the example usage for android.graphics.drawable BitmapDrawable getBitmap.

Prototype

public final Bitmap getBitmap() 

Source Link

Document

Returns the bitmap used by this drawable to render.

Usage

From source file:Main.java

public static void freeImageView(ImageView imageView) {
    // This code behaves differently on various OS builds. That is why put into try catch.
    try {//w  w w . j ava 2  s  .c o m
        if (imageView != null) {
            Drawable dr = imageView.getDrawable();

            if (dr == null) {
                return;
            }

            if (!(dr instanceof BitmapDrawable)) {
                return;
            }
            BitmapDrawable bd = (BitmapDrawable) imageView.getDrawable();
            if (bd.getBitmap() != null) {
                bd.getBitmap().recycle();
                imageView.setImageBitmap(null);
            }
        }
    } catch (Exception e) {
        Log.e("free image view", e.getMessage());
    }
}

From source file:com.hellosky.recyclingimageloader.util.ImageMemoryCache.java

public static int getBitmapSize(BitmapDrawable value) {
    Bitmap bitmap = value.getBitmap();

    if (Utils.hasHoneycombMR1()) {
        return bitmap.getByteCount();
    }/*from w ww.  j a  va 2s .  c  om*/
    // Pre HC-MR1
    return bitmap.getRowBytes() * bitmap.getHeight();
}

From source file:Main.java

public static BitmapDrawable toRoundCorner(BitmapDrawable bitmapDrawable, int pixels) {
    Bitmap bitmap = bitmapDrawable.getBitmap();
    bitmapDrawable = new BitmapDrawable(toRoundCorner(bitmap, pixels));
    return bitmapDrawable;
}

From source file:Main.java

/** simple resizing of the given image to the desired width/height */
public static Bitmap getBitmapFromResource(Context ctx, int id, int x1, int y1) {
    BitmapDrawable bd = (BitmapDrawable) ctx.getResources().getDrawable(id);
    Bitmap b = bd.getBitmap();

    int x = b.getWidth();
    int y = b.getHeight();

    float scaleX = (float) x1 / x;
    float scaleY = (float) y1 / y;
    float scale = 1f;

    boolean scaleXInBounds = (scaleX * x) <= x1 && (scaleX * y) <= y1;
    boolean scaleYInBounds = (scaleY * x) <= x1 && (scaleY * y) <= y1;

    if (scaleXInBounds && scaleYInBounds) {
        scale = (scaleX > scaleY) ? scaleX : scaleY;
    } else if (scaleXInBounds) {
        scale = scaleX;/*  www . jav a 2  s.  c  o m*/
    } else if (scaleYInBounds) {
        scale = scaleY;
    }

    return Bitmap.createScaledBitmap(b, (int) (scale * x), (int) (scale * y), true);
}

From source file:Main.java

@SuppressWarnings("deprecation")
private static Drawable getSelectedDrawable(BitmapDrawable mDrawable) {
    Bitmap srcBitmap = mDrawable.getBitmap();
    Bitmap bmp = Bitmap.createBitmap(srcBitmap.getWidth(), srcBitmap.getHeight(), Config.ARGB_8888);
    ColorMatrix cMatrix = new ColorMatrix();
    cMatrix.set(new float[] { 1, 0, 0, 0, -DRAW_DEEP, 0, 1, 0, 0, -DRAW_DEEP, 0, 0, 1, 0, -DRAW_DEEP, 0, 0, 0,
            1, 0 });//  w ww  .ja v  a 2 s  .c  o  m
    Paint paint = new Paint();
    paint.setColorFilter(new ColorMatrixColorFilter(cMatrix));
    Canvas canvas = new Canvas(bmp);
    canvas.drawBitmap(srcBitmap, 0, 0, paint);
    return new BitmapDrawable(bmp);
}

From source file:Main.java

public static Bitmap getDrawableBitmap(Context context, String rawName) {
    int id = context.getResources().getIdentifier(rawName, "drawable", context.getPackageName());
    if (id != 0) {
        BitmapDrawable drawable = (BitmapDrawable) context.getResources().getDrawable(id);
        if (drawable != null) {
            return drawable.getBitmap();
        }/*from   w  w  w .  j  a v  a  2s .co  m*/
    }

    return null;
}

From source file:Main.java

private static InputStream getDrawableStream(Context context, String rawName) throws IOException {
    int id = context.getResources().getIdentifier(rawName, "drawable", context.getPackageName());
    if (id != 0) {
        BitmapDrawable drawable = (BitmapDrawable) context.getResources().getDrawable(id);
        Bitmap bitmap = drawable.getBitmap();

        ByteArrayOutputStream os = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG, 0, os);
        return new ByteArrayInputStream(os.toByteArray());
    }/*from www .j a v a2 s .c o  m*/

    throw new IOException(String.format("bitmap of id: %s from %s not found", id, rawName));
}

From source file:Main.java

public static void recycleBitmapCaches(List<URL> mList, int fromPosition, int toPosition) {
    // TODO Auto-generated method stub
    BitmapDrawable mBitmapDrawable = null;
    for (int del = fromPosition; del < toPosition; del++) {
        mBitmapDrawable = sGridViewBitmapDrawableCache.get(mList.get(del));
        if (mBitmapDrawable != null && !mBitmapDrawable.getBitmap().isRecycled()) {
            sGridViewBitmapDrawableCache.remove(mList.get(del));
            mBitmapDrawable.getBitmap().recycle();
            mBitmapDrawable = null;//  ww w  . java 2  s.  c o  m
        }
    }
}

From source file:Main.java

public static void unbindImageView(ImageView imageView) {
    if (imageView == null) {
        return;//from  ww  w. jav a 2s  .co m
    }

    if (imageView.getBackground() != null) {
        imageView.getBackground().setCallback(null);
    }

    if (imageView.getDrawable() == null)
        return;
    if (!(imageView.getDrawable() instanceof BitmapDrawable))
        return;
    if (((BitmapDrawable) imageView.getDrawable()).getBitmap() == null)
        return;

    BitmapDrawable drawable = (BitmapDrawable) imageView.getDrawable();
    try {
        if (drawable != null && imageView.getTag() != null && !drawable.getBitmap().isRecycled()) {
            if (!imageView.getTag().toString().equalsIgnoreCase("resource") && !usingMemoryCache) {
                drawable.getBitmap().recycle();
            }
        }
    } catch (RuntimeException e) {
        e.printStackTrace();
    }
    drawable.setCallback(null);
    imageView.setImageBitmap(null);
    imageView.setImageDrawable(null);
}

From source file:Main.java

public static Bitmap drawableToBitmap(Drawable drawable) {
    Bitmap bitmap = null;/*from  ww w . j  av  a 2  s. c o m*/

    if (drawable instanceof BitmapDrawable) {
        BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
        bitmapDrawable.setAntiAlias(true);
        bitmapDrawable.setDither(true);
        bitmapDrawable.setTargetDensity(Integer.MAX_VALUE);
        if (bitmapDrawable.getBitmap() != null) {
            return bitmapDrawable.getBitmap();
        }
    }

    if (drawable.getIntrinsicWidth() <= 0 || drawable.getIntrinsicHeight() <= 0) {
        bitmap = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888); // Single color bitmap will be created of 1x1 pixel
    } else {
        bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(),
                Bitmap.Config.ARGB_8888);
    }

    Canvas canvas = new Canvas(bitmap);
    drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
    drawable.draw(canvas);
    return bitmap;
}