Example usage for android.graphics.drawable Drawable setBounds

List of usage examples for android.graphics.drawable Drawable setBounds

Introduction

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

Prototype

public void setBounds(int left, int top, int right, int bottom) 

Source Link

Document

Specify a bounding rectangle for the Drawable.

Usage

From source file:Main.java

/**
 * Create darkened version of input drawable offset.
 *
 * @param res//from w  ww. ja va2  s. co  m
 * @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:Main.java

private static Bitmap drawable2Bitmap(Drawable drawable, int... defaultWH) {
    if (drawable == null)
        return null;
    if (drawable instanceof BitmapDrawable)
        return ((BitmapDrawable) drawable).getBitmap();
    try {//from   www. ja va2  s .c om
        Bitmap bitmap;
        if (drawable instanceof ColorDrawable)
            bitmap = Bitmap.createBitmap(defaultWH[0], defaultWH[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;
    } catch (OutOfMemoryError e) {
        return null;
    }
}

From source file:com.tafayor.selfcamerashot.taflib.helpers.GraphicsHelper.java

public static Bitmap drawableToBitmap(Drawable drawable, int width, int height) {
    Bitmap bitmap;/*from  w w w.j  av a2 s.  c o m*/

    bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);

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

    /*
    The drawable may be composite of bitmap and drawable layers
     */
    if (drawable instanceof BitmapDrawable) {
        Bitmap bmp = ((BitmapDrawable) drawable).getBitmap();
        canvas.drawBitmap(bmp, (canvas.getWidth() - bmp.getWidth()) / 2,
                (canvas.getHeight() - bmp.getHeight()) / 2, null);
    }
    drawable.draw(canvas);

    return bitmap;
}

From source file:Main.java

public static Bitmap convertToBitmap(Drawable dw) {
    Bitmap bitmap;//from  w  w  w.j  a  va2 s  .  com

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

    if (dw.getIntrinsicWidth() <= 0 || dw.getIntrinsicHeight() <= 0) {
        bitmap = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888);
    } else {
        bitmap = Bitmap.createBitmap(dw.getIntrinsicWidth(), dw.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
    }

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

From source file:Main.java

public static Bitmap getBitmapFromDrawable(Drawable drawable) {
    if (drawable == null) {
        return null;
    }/*from   www.  jav a  2 s .com*/
    if (drawable instanceof BitmapDrawable) {
        return ((BitmapDrawable) drawable).getBitmap();
    }
    try {
        Bitmap bitmap;
        if (drawable instanceof ColorDrawable) {
            bitmap = Bitmap.createBitmap(COLORDRAWABLE_DIMENSION, COLORDRAWABLE_DIMENSION, BITMAP_CONFIG);
        } else {
            bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(),
                    BITMAP_CONFIG);
        }
        Canvas canvas = new Canvas(bitmap);
        drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
        drawable.draw(canvas);
        return bitmap;
    } catch (OutOfMemoryError e) {
        return null;
    }
}

From source file:com.battlelancer.seriesguide.util.Utils.java

/**
 * Sets the Drawables (if any) to appear to the start of, above, to the end of, and below the
 * text.  Use null if you do not want a Drawable there. The Drawables' bounds will be set to
 * their intrinsic bounds.//from  w  w  w. j  a  va2 s  .c  o m
 */
public static void setCompoundDrawablesRelativeWithIntrinsicBounds(Button button, Drawable left, Drawable top,
        Drawable right, Drawable bottom) {
    if (left != null) {
        left.setBounds(0, 0, left.getIntrinsicWidth(), left.getIntrinsicHeight());
    }
    if (right != null) {
        right.setBounds(0, 0, right.getIntrinsicWidth(), right.getIntrinsicHeight());
    }
    if (top != null) {
        top.setBounds(0, 0, top.getIntrinsicWidth(), top.getIntrinsicHeight());
    }
    if (bottom != null) {
        bottom.setBounds(0, 0, bottom.getIntrinsicWidth(), bottom.getIntrinsicHeight());
    }
    button.setCompoundDrawables(left, top, right, bottom);
}

From source file:com.dv.Utils.Tools.java

/**
 * BitMap/* w  w  w.  j a  v  a 2s .c  o  m*/
 *
 * @param drawable
 * @return
 */
public static Bitmap drawableToBitmap(Drawable drawable) {
    Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(),
            (drawable.getOpacity() != PixelFormat.OPAQUE) ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565);
    Canvas canvas = new Canvas(bitmap);

    // canvas.setBitmap(bitmap);
    drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
    drawable.draw(canvas);

    return bitmap;
}

From source file:com.nadmm.airports.utils.UiUtils.java

public static Drawable combineDrawables(Context context, Drawable d1, Drawable d2, int paddingDp) {
    // Assumes both d1 & d2 are same size and square shaped
    int w = d1.getIntrinsicWidth();
    int h = d1.getIntrinsicHeight();
    int paddingPx = convertDpToPx(context, paddingDp);
    Bitmap result = Bitmap.createBitmap(w + (d2 != null ? w + paddingPx : 0), h, Bitmap.Config.ARGB_8888);

    Canvas canvas = new Canvas(result);
    canvas.setDensity(Bitmap.DENSITY_NONE);
    d1.setBounds(0, 0, w - 1, h - 1);
    d1.draw(canvas);/*from w  ww  . j  ava2  s  . c  om*/
    if (d2 != null) {
        canvas.translate(w + paddingPx, 0);
        d2.setBounds(0, 0, w - 1, h - 1);
        d2.draw(canvas);
    }

    return new BitmapDrawable(context.getResources(), result);
}

From source file:com.dm.material.dashboard.candybar.helpers.DrawableHelper.java

@Nullable
public static Drawable getDefaultImage(@NonNull Context context, @DrawableRes int res) {
    try {//from ww w . j  ava2  s  .c o  m
        int color = ColorHelper.getAttributeColor(context, android.R.attr.textColorSecondary);
        int padding = context.getResources().getDimensionPixelSize(R.dimen.default_image_padding);

        Drawable drawable = AppCompatDrawableManager.get().getDrawable(context, res);
        drawable.setColorFilter(color, PorterDuff.Mode.SRC_IN);
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
            drawable = (DrawableCompat.wrap(drawable)).mutate();
        }

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

        Bitmap tintedBitmap = Bitmap.createBitmap(bitmap.getWidth() + padding, bitmap.getHeight() + padding,
                Bitmap.Config.ARGB_8888);
        Canvas tintedCanvas = new Canvas(tintedBitmap);
        int background = ColorHelper.getAttributeColor(context, R.attr.card_background);
        Paint paint = new Paint();
        paint.setFilterBitmap(true);
        paint.setAntiAlias(true);
        tintedCanvas.drawColor(background, PorterDuff.Mode.ADD);
        tintedCanvas.drawBitmap(bitmap, (tintedCanvas.getWidth() - bitmap.getWidth()) / 2,
                (tintedCanvas.getHeight() - bitmap.getHeight()) / 2, paint);
        return new BitmapDrawable(context.getResources(), tintedBitmap);
    } catch (Exception | OutOfMemoryError e) {
        return null;
    }
}

From source file:org.wikipedia.page.shareafact.SnippetImage.java

private static void drawWordmarkFromStaticImage(@NonNull Context context, @NonNull Canvas canvas,
        boolean isArticleRTL) {
    // scaling it a bit down from original 317x54px size
    final int width = 130;
    final int height = 22;
    final int bottom = HEIGHT - BOTTOM_PADDING;
    final int top = bottom - height;

    Drawable d = ContextCompat.getDrawable(context, R.drawable.wp_wordmark);
    DrawableCompat.setTint(d, Color.LTGRAY);

    int left = WIDTH - HORIZONTAL_PADDING - width;
    if (isArticleRTL) {
        left = HORIZONTAL_PADDING;/*from  w  w w  . j  a  va 2 s. c o m*/
    }
    int right = left + width;

    d.setBounds(left, top, right, bottom);
    d.draw(canvas);
}