Example usage for android.graphics.drawable Drawable draw

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

Introduction

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

Prototype

public abstract void draw(@NonNull Canvas canvas);

Source Link

Document

Draw in its bounds (set via setBounds) respecting optional effects such as alpha (set via setAlpha) and color filter (set via setColorFilter).

Usage

From source file:com.android.leanlauncher.WidgetPreviewLoader.java

private static void renderDrawableToBitmap(Drawable d, Bitmap bitmap, int x, int y, int w, int h) {
    if (bitmap != null) {
        final Canvas c = new Canvas(bitmap);
        Rect oldBounds = d.copyBounds();
        d.setBounds(x, y, x + w, y + h);
        d.draw(c);
        d.setBounds(oldBounds); // Restore the bounds
        c.setBitmap(null);// ww w  . j  a va2  s  .c om
    }
}

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

public static Bitmap drawableToBitmap(Drawable drawable) {
    Bitmap bitmap = null;//  w  w w.java2  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

/**
 * Convert a {@link Drawable} into an {@link Bitmap} object.
 * <p>/*from   w  ww . ja v  a  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;
}

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.
 *///from  w  w  w. jav  a2 s.com
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.tr4android.support.extension.widget.CircleImageView.java

/**
 * Helper for creating a bitmap from a drawable
 *
 * @param drawable The drawable which should be converted to a bitmap
 * @return the bitmap containing the drawable
 *//*w  w w .  ja v  a  2s  . c o m*/
public static Bitmap getBitmapFromDrawable(Drawable drawable) {
    if (drawable == null)
        return null;
    if (drawable instanceof BitmapDrawable) {
        Log.w(LOG_TAG, "For better performance consider using setImageBitmap() instead!");
        return ((BitmapDrawable) drawable).getBitmap();
    } else {
        Bitmap bitmap = Bitmap.createBitmap(Math.max(2, drawable.getIntrinsicWidth()),
                Math.max(2, 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 instanceof BitmapDrawable) {
        return ((BitmapDrawable) drawable).getBitmap();
    }//from   ww w . ja  va 2  s . co  m

    // We ask for the bounds if they have been set as they would be most
    // correct, then we check we are  > 0
    final int width = !drawable.getBounds().isEmpty() ? drawable.getBounds().width()
            : drawable.getIntrinsicWidth();

    final int height = !drawable.getBounds().isEmpty() ? drawable.getBounds().height()
            : drawable.getIntrinsicHeight();

    // Now we check we are > 0
    final Bitmap bitmap = Bitmap.createBitmap(width <= 0 ? 1 : width, height <= 0 ? 1 : 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:org.bottiger.podcast.utils.UIUtils.java

public static Bitmap getBitmapFromVectorDrawable(Context context, int drawableId) {
    Drawable drawable = ContextCompat.getDrawable(context, drawableId);
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
        drawable = (DrawableCompat.wrap(drawable)).mutate();
    }//from  w w  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:io.jawg.osmcontributor.ui.utils.BitmapHandler.java

public static Bitmap drawableToBitmap(Drawable drawable) {
    if (drawable instanceof BitmapDrawable) {
        return ((BitmapDrawable) drawable).getBitmap();
    }//from   w  w w.j a  v  a 2  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 drawableToBitmap(Drawable drawable) {
    Bitmap bitmap = null;//from w w w  .j a  v  a 2s .  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;
}