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:mobisocial.musubi.objects.PictureObj.java

/**
 * Pass in one of raw or fd as the source of the image.
 * Not thread safe, only call on the ui thread.
 *///from  w  w w . j a v a 2s.c o m
protected static void bindImageToView(Context context, ImageView imageView, byte[] raw, FileDescriptor fd) {
    // recycle old images (vs. caching in ImageCache)
    if (imageView.getDrawable() != null) {
        BitmapDrawable d = (BitmapDrawable) imageView.getDrawable();
        if (d != null && d.getBitmap() != null) {
            d.getBitmap().recycle();
        }
    }

    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    if (fd != null) {
        BitmapFactory.decodeFileDescriptor(fd, null, options);
    } else {
        BitmapFactory.decodeByteArray(raw, 0, raw.length, options);
    }
    Resources res = context.getResources();

    float scaleFactor;
    if (res.getBoolean(R.bool.is_tablet)) {
        scaleFactor = 3.0f;
    } else {
        scaleFactor = 2.0f;
    }
    DisplayMetrics dm = context.getResources().getDisplayMetrics();
    int pixels = dm.widthPixels;
    if (dm.heightPixels < pixels) {
        pixels = dm.heightPixels;
    }
    int width = (int) (pixels / scaleFactor);
    int height = (int) ((float) width / options.outWidth * options.outHeight);
    int max_height = (int) (AppStateObj.MAX_HEIGHT * dm.density);
    if (height > max_height) {
        width = width * max_height / height;
        height = max_height;
    }

    options.inJustDecodeBounds = false;
    options.inTempStorage = getTempData();
    options.inSampleSize = 1;
    //TODO: lame, can just compute
    while (options.outWidth / (options.inSampleSize + 1) >= width
            && options.outHeight / (options.inSampleSize + 1) >= height) {
        options.inSampleSize++;
    }
    options.inPurgeable = true;
    options.inInputShareable = true;

    Bitmap bm;
    if (fd != null) {
        bm = BitmapFactory.decodeFileDescriptor(fd, null, options);
    } else {
        bm = BitmapFactory.decodeByteArray(raw, 0, raw.length, options);
    }
    imageView.getLayoutParams().width = width + 13;
    imageView.getLayoutParams().height = height + 14;
    imageView.setImageBitmap(bm);
}

From source file:io.indy.seni.util.ImageCache.java

/**
 * Get the size in bytes of a bitmap in a BitmapDrawable. Note that from Android 4.4 (KitKat)
 * onward this returns the allocated memory size of the bitmap which can be larger than the
 * actual bitmap data byte count (in the case it was re-used).
 *
 * @param value//from   w w  w . j ava  2s  . co  m
 * @return size in bytes
 */
@TargetApi(VERSION_CODES.KITKAT)
public static int getBitmapSize(BitmapDrawable value) {
    Bitmap bitmap = value.getBitmap();

    return bitmap.getAllocationByteCount();
    /*
    // From KitKat onward use getAllocationByteCount() as allocated bytes can potentially be
    // larger than bitmap byte count.
    if (Utils.hasKitKat()) {
    return bitmap.getAllocationByteCount();
    }
            
    if (Utils.hasHoneycombMR1()) {
    return bitmap.getByteCount();
    }
            
    // Pre HC-MR1
    return bitmap.getRowBytes() * bitmap.getHeight();
    */
}

From source file:me.zhang.bingo.Utility.java

public static Bitmap drawableToBitmap(Drawable drawable) {
    Bitmap bitmap;//from   w ww  .ja v  a  2 s  .  c o  m

    if (drawable instanceof BitmapDrawable) {
        BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
        if (bitmapDrawable.getBitmap() != null) {
            return bitmapDrawable.getBitmap();
        }
    }

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

From source file:gr.unfold.android.tsibato.images.ImageCache.java

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

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

From source file:com.lovebridge.chat.view.ImageCache.java

/**
 * Get the size in bytes of a bitmap in a BitmapDrawable. Note that from
 * Android 4.4 (KitKat) onward this returns the allocated memory size of the
 * bitmap which can be larger than the actual bitmap data byte count (in the
 * case it was re-used)./*  w w w  .j  a va2  s  .c o  m*/
 *
 * @param value
 * @return size in bytes
 */
@TargetApi(19)
public static int getBitmapSize(BitmapDrawable value) {
    Bitmap bitmap = value.getBitmap();
    // From KitKat onward use getAllocationByteCount() as allocated bytes
    // can potentially be
    // larger than bitmap byte count.
    // if (Utils.hasKitKat()) {
    // return bitmap.getAllocationByteCount();
    // }
    if (com.lovebridge.chat.utils.Utils.hasHoneycombMR1()) {
        return bitmap.getByteCount();
    }
    // Pre HC-MR1
    return bitmap.getRowBytes() * bitmap.getHeight();
}

From source file:com.nextgis.maplibui.util.ControlHelper.java

public static Drawable getIconByVectorType(Context context, int geometryType, int color, int defaultIcon,
        boolean syncable) {
    int drawableId;

    switch (geometryType) {
    case GTPoint:
        drawableId = R.drawable.ic_type_point;
        break;//  w  w  w  . j  a va 2s  . c  o  m
    case GTMultiPoint:
        drawableId = R.drawable.ic_type_multipoint;
        break;
    case GTLineString:
        drawableId = R.drawable.ic_type_line;
        break;
    case GTMultiLineString:
        drawableId = R.drawable.ic_type_multiline;
        break;
    case GTPolygon:
        drawableId = R.drawable.ic_type_polygon;
        break;
    case GTMultiPolygon:
        drawableId = R.drawable.ic_type_multipolygon;
        break;
    default:
        return ContextCompat.getDrawable(context, defaultIcon);
    }

    BitmapDrawable icon = (BitmapDrawable) ContextCompat.getDrawable(context, drawableId);
    if (icon != null) {
        Bitmap src = icon.getBitmap();
        Bitmap bitmap = Bitmap.createBitmap(src.getWidth(), src.getHeight(), src.getConfig());
        Canvas canvas = new Canvas(bitmap);

        Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        ColorFilter filter = new PorterDuffColorFilter(color, PorterDuff.Mode.SRC_ATOP);
        paint.setColorFilter(filter);
        canvas.drawBitmap(src, 0, 0, paint);

        if (syncable) {
            int syncIconId = isDarkTheme(context) ? R.drawable.ic_action_refresh_dark
                    : R.drawable.ic_action_refresh_light;
            ;
            src = BitmapFactory.decodeResource(context.getResources(), syncIconId);
            src = Bitmap.createScaledBitmap(src, bitmap.getWidth() / 2, bitmap.getWidth() / 2, true);
            canvas.drawBitmap(src, bitmap.getWidth() - bitmap.getWidth() / 2,
                    bitmap.getWidth() - bitmap.getWidth() / 2, new Paint());
        }

        icon = new BitmapDrawable(context.getResources(), bitmap);
    }

    return icon;
}

From source file:com.jefftharris.passwdsafe.lib.view.GuiUtils.java

/** Show a simple notification */
public static void showSimpleNotification(NotificationManager notifyMgr, Context ctx, int iconId, String title,
        int bigIcon, String content, PendingIntent intent, int notifyId, boolean autoCancel) {
    BitmapDrawable b = (BitmapDrawable) getDrawable(ctx.getResources(), bigIcon);
    if (b == null) {
        return;//w  ww. jav  a  2 s .com
    }
    NotificationCompat.Builder builder = new NotificationCompat.Builder(ctx).setContentTitle(title)
            .setContentText(content).setContentIntent(intent).setSmallIcon(iconId).setLargeIcon(b.getBitmap())
            .setTicker(title).setAutoCancel(autoCancel);
    Notification notif = builder.build();
    notifyMgr.notify(notifyId, notif);
}

From source file:com.sven.im.video.util.ImageCache.java

/**
 * Get the size in bytes of a bitmap in a BitmapDrawable. Note that from
 * Android 4.4 (KitKat) onward this returns the allocated memory size of the
 * bitmap which can be larger than the actual bitmap data byte count (in the
 * case it was re-used).//ww w. j  a  v  a 2s  . c o  m
 *
 * @param value
 * @return size in bytes
 */
@TargetApi(19)
public static int getBitmapSize(BitmapDrawable value) {
    Bitmap bitmap = value.getBitmap();

    if (Utils.hasHoneycombMR1()) {
        return bitmap.getByteCount();
    }

    // Pre HC-MR1
    return bitmap.getRowBytes() * bitmap.getHeight();
}

From source file:com.jefftharris.passwdsafe.lib.view.GuiUtils.java

/** Show a notification */
public static void showNotification(NotificationManager notifyMgr, Context ctx, int iconId, String tickerText,
        String title, int bigIcon, String content, List<String> bigLines, PendingIntent intent, int notifyId,
        boolean autoCancel) {
    BitmapDrawable b = (BitmapDrawable) getDrawable(ctx.getResources(), bigIcon);
    if (b == null) {
        return;//from ww w. j a va 2 s  .  co m
    }
    NotificationCompat.Builder builder = new NotificationCompat.Builder(ctx).setContentTitle(title)
            .setContentText(content).setContentIntent(intent).setSmallIcon(iconId).setLargeIcon(b.getBitmap())
            .setTicker(tickerText).setAutoCancel(autoCancel);
    NotificationCompat.InboxStyle style = new NotificationCompat.InboxStyle(builder).setBigContentTitle(title)
            .setSummaryText(content);

    int numLines = Math.min(bigLines.size(), 5);
    for (int i = 0; i < numLines; ++i) {
        style.addLine(bigLines.get(i));
    }
    if (numLines < bigLines.size()) {
        style.addLine("");
    }

    builder.setStyle(style);
    Notification notif = builder.build();
    notifyMgr.notify(notifyId, notif);
}

From source file:com.liangxun.university.huanxin.chat.video.util.VideoImageCache.java

/**
 * Get the size in bytes of a bitmap in a BitmapDrawable. Note that from
 * Android 4.4 (KitKat) onward this returns the allocated memory size of the
 * bitmap which can be larger than the actual bitmap data byte count (in the
 * case it was re-used)./*from w  ww.  j a v a  2s.  c  o  m*/
 *
 * @param value
 * @return size in bytes
 */
@TargetApi(19)
public static int getBitmapSize(BitmapDrawable value) {
    Bitmap bitmap = value.getBitmap();

    // From KitKat onward use getAllocationByteCount() as allocated bytes
    // can potentially be
    // larger than bitmap byte count.
    //      if (Utils.hasKitKat()) {
    //         return bitmap.getAllocationByteCount();
    //      }

    if (HxUtils.hasHoneycombMR1()) {
        return bitmap.getByteCount();
    }

    // Pre HC-MR1
    return bitmap.getRowBytes() * bitmap.getHeight();
}