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:android.support.v7.testutils.TestUtils.java

/**
 * Checks whether all the pixels in the specified drawable are of the same specified color.
 *
 * In case there is a color mismatch, the behavior of this method depends on the
 * <code>throwExceptionIfFails</code> parameter. If it is <code>true</code>, this method will
 * throw an <code>Exception</code> describing the mismatch. Otherwise this method will call
 * <code>Assert.fail</code> with detailed description of the mismatch.
 */// w ww. j  a v a  2 s .  c  om
public static void assertAllPixelsOfColor(String failMessagePrefix, @NonNull Drawable drawable,
        int drawableWidth, int drawableHeight, boolean callSetBounds, @ColorInt int color,
        int allowedComponentVariance, boolean throwExceptionIfFails) {
    // Create a bitmap
    Bitmap bitmap = Bitmap.createBitmap(drawableWidth, drawableHeight, Bitmap.Config.ARGB_8888);
    // Create a canvas that wraps the bitmap
    Canvas canvas = new Canvas(bitmap);
    if (callSetBounds) {
        // Configure the drawable to have bounds that match the passed size
        drawable.setBounds(0, 0, drawableWidth, drawableHeight);
    }
    // And ask the drawable to draw itself to the canvas / bitmap
    drawable.draw(canvas);

    try {
        assertAllPixelsOfColor(failMessagePrefix, bitmap, drawableWidth, drawableHeight, color,
                allowedComponentVariance, throwExceptionIfFails);
    } finally {
        bitmap.recycle();
    }
}

From source file:com.xxxifan.devbox.core.util.ViewUtils.java

public static Bitmap toBitmap(Drawable drawable) {
    if (drawable instanceof BitmapDrawable) {
        return ((BitmapDrawable) drawable).getBitmap();
    }// ww w  .  j a v a2  s . c  o m

    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 bitmap;
}

From source file:Main.java

public static Bitmap createFromDrawable(Drawable drawable) {
    if (drawable == null)
        return null;

    if (drawable instanceof BitmapDrawable)
        return ((BitmapDrawable) drawable).getBitmap();

    int width = drawable.getIntrinsicWidth();
    if (width == -1) // e.g. for ColorDrawable.
        width = 1;/*w  w  w .  ja va  2 s  .  c o m*/

    int height = drawable.getIntrinsicHeight();
    if (height == -1)
        height = 1;

    // NOTE: Although the following code is a bit expensive, it will not be necessary
    // for most drawables, since they are normally bitmap resources.
    Bitmap bitmap = Bitmap.createBitmap(width, height, 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:com.destin.sehaikun.DrawableUtils.java

public static Bitmap getBitmapFromDrawable(Drawable drawable) {
    if (drawable == null) {
        return null;
    }// w w w  . j av  a  2  s . c  o m

    if (drawable instanceof BitmapDrawable) {
        return ((BitmapDrawable) drawable).getBitmap();
    }

    try {
        Bitmap bitmap;

        if (drawable instanceof ColorDrawable) {
            bitmap = Bitmap.createBitmap(2, 2, 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 (Exception e) {
        e.printStackTrace();
        return null;
    }
}

From source file:Main.java

@SuppressWarnings("unused")
public static Bitmap drawableToBitmap(Drawable drawable) {
    Bitmap bitmap;/*from   ww  w. jav 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) {
        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;
}

From source file:Main.java

public static Bitmap drawableToBitmap(Drawable drawable) {
    if (drawable == null)
        drawable = new ColorDrawable(Color.TRANSPARENT);

    Bitmap bitmap;//  w  w  w.  ja v a2s  .  c  om

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

    if (drawable.getIntrinsicWidth() <= 0 || drawable.getIntrinsicHeight() <= 0)
        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:Main.java

/**
 * TODO: Never tested. I don't remember if it works, but plese don't remove this.
 *//*www  .ja v  a 2s  . c  o  m*/
private static Bitmap drawableToBitmap(Drawable drawable) {
    Bitmap bitmap = null;

    if (drawable instanceof BitmapDrawable) {
        BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
        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;
}

From source file:Main.java

public static Bitmap drawableToBitmap(Drawable drawable) {
    Bitmap bitmap = null;//  w ww .  j  a va 2s . c  om

    if (drawable instanceof BitmapDrawable) {
        BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
        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;
}

From source file:de.gebatzens.sia.SettingsActivity.java

public static CustomTabsIntent createCustomTab(Activity activity, String url) {
    Drawable d = ContextCompat.getDrawable(activity, R.drawable.ic_arrow_back);
    Bitmap bitmap = Bitmap.createBitmap(d.getIntrinsicWidth(), d.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    d.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
    d.draw(canvas);//from w ww .ja v  a 2 s.  c  om
    CustomTabsIntent customTabsIntent = new CustomTabsIntent.Builder()
            .setToolbarColor(SIAApp.SIA_APP.school.getColor()).setSecondaryToolbarColor(Color.RED)
            .setCloseButtonIcon(bitmap).setShowTitle(true).build();
    customTabsIntent.launchUrl(activity, Uri.parse(url));
    return customTabsIntent;
}

From source file:Main.java

/**
 * Convert a {@link Drawable} into an {@link Bitmap} object.
 * <p>/*from  www.j ava  2  s.c o  m*/
 * Draws the {@code Drawable} onto a RAM-only {@link Canvas} and grabs the resulting
 * {@code Bitmap}.
 * </p>
 * <p>
 * If the {@code Drawable} is a {@link BitmapDrawable}, no conversion is needed, and no
 * conversion will be done.
 * </p>
 *
 * @param drawable The {@link Drawable} to convert. Can be any drawable.
 * @return A {@link Bitmap} representing the given {@code drawable}.
 */
public static Bitmap drawableToBitmap(Drawable drawable) {
    Bitmap bitmap;

    if (drawable instanceof BitmapDrawable) {
        BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
        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;
}