Example usage for android.graphics.drawable BitmapDrawable BitmapDrawable

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

Introduction

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

Prototype

private BitmapDrawable(BitmapState state, Resources res) 

Source Link

Usage

From source file:Main.java

private static Drawable getMoreSuggestionsHint(final Resources res, final float textSize, final int color) {
    final Paint paint = new Paint();
    paint.setAntiAlias(true);//from   ww  w  .j  a  va  2  s.  com
    paint.setTextAlign(Align.CENTER);
    paint.setTextSize(textSize);
    paint.setColor(color);
    final Rect bounds = new Rect();
    paint.getTextBounds(MORE_SUGGESTIONS_HINT, 0, MORE_SUGGESTIONS_HINT.length(), bounds);
    final int width = Math.round(bounds.width() + 0.5f);
    final int height = Math.round(bounds.height() + 0.5f);
    final Bitmap buffer = Bitmap.createBitmap(width, (height * 3 / 2), Bitmap.Config.ARGB_8888);
    final Canvas canvas = new Canvas(buffer);
    canvas.drawText(MORE_SUGGESTIONS_HINT, width / 2, height, paint);
    BitmapDrawable bitmapDrawable = new BitmapDrawable(res, buffer);
    bitmapDrawable.setTargetDensity(canvas);
    return bitmapDrawable;
}

From source file:Main.java

public static Drawable get_scaled_drawable_from_uri_string_for_square_container(Context context, String uri,
        int max_size) {
    Drawable drawable = null;//  ww  w.j a  v a  2 s  . com
    Uri img_uri = Uri.parse(uri);
    if (uri != null) {
        try {

            InputStream inputStream = context.getContentResolver().openInputStream(img_uri);
            Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
            int sx = bitmap.getWidth();
            int sy = bitmap.getHeight();

            int fsx = max_size;
            int fsy = max_size;

            if (sy > sx)
                fsx = (int) ((float) max_size * ((float) sx / (float) sy));
            else if (sx > sy)
                fsy = (int) ((float) max_size * ((float) sy / (float) sx));

            Bitmap small_bitmap = Bitmap.createScaledBitmap(bitmap, fsx, fsy, false);
            bitmap.recycle();
            drawable = new BitmapDrawable(context.getResources(), small_bitmap);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }
    return drawable;
}

From source file:Main.java

/**
 * Resize a {@link Drawable} to the given size.<br />
 * Metrics and density are given by the {@link Resources} bount to this instance.
 * @param ctx Context./*from   w w w  .j  ava 2 s  .  co  m*/
 * @param drawable {@link Drawable} to resize.
 * @param width Desired width in {@code dp}.
 * @param height Desired height in {@code dp}.
 * @return A scaled {@link Drawable}.
 */
public static Drawable resizeDrawable(Context ctx, Drawable drawable, int width, int height) {
    Resources res = ctx.getResources();
    float density = res.getDisplayMetrics().density;

    //Get a bitmap from the drawable
    Bitmap bmp = drawableToBitmap(drawable);

    //Create a scaled bitmap
    bmp = Bitmap.createScaledBitmap(bmp, (int) (width * density), (int) (height * density), true);

    //Convert bitmap to drawable
    return new BitmapDrawable(res, bmp);
}

From source file:Main.java

/**
 * Creates new drawable with invisible pixels trimmed at the borders
 *
 * @param input drawable to be trimmed/*from   w w  w  . j  a v a2s.co m*/
 * @return trimmed copy of the drawable
 */
public static Drawable trimTransparent(Drawable input, Resources res) {
    final Bitmap bitmap = drawableToBitmap(input);
    final Bitmap trimmed = trimTransparentRegions(bitmap, 0);
    return new BitmapDrawable(res, trimmed);
}

From source file:Main.java

/**
 * Resize a {@link Drawable} to the given size.<br />
 * Metrics and density are given by the {@link Resources} bount to this instance.
 * @param ctx Context.//  ww  w  . j a v a2 s . com
 * @param drawable {@link Drawable} to resize.
 * @param widthId Reference to the dimension containing the desired width.
 * @param heightId Reference to the dimension containing the desired height.
 * @return A scaled {@link Drawable}.
 */
public static Drawable resizeDrawable(Context ctx, Drawable drawable, int widthId, int heightId) {
    Resources res = ctx.getResources();
    //float density = res.getDisplayMetrics().density;

    //Get a bitmap from the drawable
    Bitmap bmp = drawableToBitmap(drawable);

    //Create a scaled bitmap
    //      bmp = Bitmap.createScaledBitmap(
    //            bmp,
    //            (int)(width * density),
    //            (int)(height * density),
    //            true);
    bmp = Bitmap.createScaledBitmap(bmp, res.getDimensionPixelSize(widthId),
            res.getDimensionPixelSize(heightId), true);

    //Convert bitmap to drawable
    return new BitmapDrawable(res, bmp);
}

From source file:Main.java

/**
 * This method of image pixelization utilizes the bitmap scaling operations built
 * into the framework. By downscaling the bitmap and upscaling it back to its
 * original size (while setting the filter flag to false), the same effect can be
 * achieved with much better performance.
 *//*from   w w w .j a  va 2  s .co  m*/
public static BitmapDrawable builtInPixelization(Context context, float pixelizationFactor, Bitmap bitmap) {

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

    int downScaleFactorWidth = (int) (pixelizationFactor * width);
    downScaleFactorWidth = downScaleFactorWidth > 0 ? downScaleFactorWidth : 1;
    int downScaleFactorHeight = (int) (pixelizationFactor * height);
    downScaleFactorHeight = downScaleFactorHeight > 0 ? downScaleFactorHeight : 1;

    int downScaledWidth = width / downScaleFactorWidth;
    int downScaledHeight = height / downScaleFactorHeight;

    Bitmap pixelatedBitmap = Bitmap.createScaledBitmap(bitmap, downScaledWidth, downScaledHeight, false);

    /* Bitmap's createScaledBitmap method has a filter parameter that can be set to either
     * true or false in order to specify either bilinear filtering or point sampling
     * respectively when the bitmap is scaled up or now.
     *
     * Similarly, a BitmapDrawable also has a flag to specify the same thing. When the
     * BitmapDrawable is applied to an ImageView that has some scaleType, the filtering
     * flag is taken into consideration. However, for optimization purposes, this flag was
     * ignored in BitmapDrawables before Jelly Bean MR1.
     *
     * Here, it is important to note that prior to JBMR1, two bitmap scaling operations
     * are required to achieve the pixelization effect. Otherwise, a BitmapDrawable
     * can be created corresponding to the downscaled bitmap such that when it is
     * upscaled to fit the ImageView, the upscaling operation is a lot faster since
     * it uses internal optimizations to fit the ImageView.
     * */
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
        BitmapDrawable bitmapDrawable = new BitmapDrawable(context.getResources(), pixelatedBitmap);
        bitmapDrawable.setFilterBitmap(false);
        return bitmapDrawable;
    } else {
        Bitmap upscaled = Bitmap.createScaledBitmap(pixelatedBitmap, width, height, false);
        return new BitmapDrawable(context.getResources(), upscaled);
    }
}

From source file:Main.java

/**
 * Create darkened version of input drawable offset.
 *
 * @param res/* ww w  . j a v a 2 s  .c  om*/
 * @param inImage
 * @param offsetX
 * @param offsetY
 * @param blurRadius   Not currently used.
 * @param shadowColor
 * @return
 */
public static BitmapDrawable shadowImage(Resources res, Drawable inImage, int offsetX, int offsetY,
        float blurRadius, int shadowColor) {

    Bitmap inBitmap;
    if (inImage instanceof BitmapDrawable) {
        inBitmap = ((BitmapDrawable) inImage).getBitmap();
    } else {
        // Bitmap from drawable
        int imgWidth = inImage.getIntrinsicWidth();
        int imgHeight = inImage.getIntrinsicHeight();
        inBitmap = Bitmap.createBitmap(imgWidth, imgHeight, Bitmap.Config.ARGB_8888);
        Canvas bottomCanvas = new Canvas(inBitmap);
        inImage.setBounds(0, 0, bottomCanvas.getWidth(), bottomCanvas.getHeight());
        inImage.draw(bottomCanvas);
    }

    Bitmap blurBitmap = shadowBitmap(inBitmap, offsetX, offsetY, blurRadius, shadowColor);
    return new BitmapDrawable(res, blurBitmap);
}

From source file:com.danimahardhika.android.helpers.core.DrawableHelper.java

@Nullable
public static Drawable getResizedDrawable(@NonNull Context context, @NonNull Drawable drawable,
        float sizeInDp) {
    try {/*  w  w w. j  av a 2  s . c  o m*/
        int size = Math.round(UnitHelper.toPixel(context, sizeInDp));

        Bitmap 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 new BitmapDrawable(context.getResources(), Bitmap.createScaledBitmap(bitmap, size, size, true));
    } catch (OutOfMemoryError e) {
        return null;
    }
}

From source file:ac.robinson.bettertogether.QRPopupWindow.java

void setQRBitmap(Bitmap background) {
    mPopupWindow.setBackgroundDrawable(new BitmapDrawable(mAnchorView.getResources(), background));
}

From source file:com.facebook.samples.musicdashboard.Song.java

public Drawable getDrawable(Context c) {
    // Return a cached value if it exists
    if (drawable != null)
        return drawable;

    File file = getLocalFile(c);//from   ww  w  .  j a v a 2 s . co m

    // Cache the song image
    if (file != null && file.exists()) {
        drawable = new BitmapDrawable(c.getResources(), file.getPath());
        return drawable;
    } else {
        return c.getResources().getDrawable(resId);
    }
}