List of usage examples for android.graphics.drawable Drawable draw
public abstract void draw(@NonNull Canvas canvas);
From source file:com.android.dialer.contactinfo.ContactPhotoLoader.java
private static Bitmap drawableToBitmap(Drawable drawable, int width, int height) { 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.achep.acdisplay.graphics.IconFactory.java
@NonNull private static Bitmap createIcon(@NonNull Drawable drawable, int size) { Bitmap icon = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_4444); Canvas canvas = new Canvas(icon); drawable = drawable.mutate();/*from w ww . ja va 2s .com*/ drawable.setBounds(0, 0, size, size); drawable.draw(canvas); return icon; }
From source file:Main.java
public static Bitmap drawableToBitmap(Drawable drawable) { if (drawable instanceof BitmapDrawable) return ((BitmapDrawable) drawable).getBitmap(); else {/*from www . 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.draw(canvas); return bitmap; } }
From source file:Main.java
public static Bitmap drawableToBitmap(Drawable drawable) { if (drawable instanceof BitmapDrawable) { return ((BitmapDrawable) drawable).getBitmap(); }//from w ww . j a va 2s .co m Bitmap bitmap = Bitmap.createBitmap(128, 128, 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 cropMaxVisibleBitmap(Drawable drawable, int iconSize) { Bitmap tmp = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);//from ww w.j a v a 2 s .c o m Canvas canvas = new Canvas(tmp); drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight()); drawable.draw(canvas); Rect crop = new Rect(tmp.getWidth(), tmp.getHeight(), -1, -1); for (int y = 0; y < tmp.getHeight(); y++) { for (int x = 0; x < tmp.getWidth(); x++) { int alpha = (tmp.getPixel(x, y) >> 24) & 255; if (alpha > 0) { // pixel is not 100% transparent if (x < crop.left) crop.left = x; if (x > crop.right) crop.right = x; if (y < crop.top) crop.top = y; if (y > crop.bottom) crop.bottom = y; } } } if (crop.width() <= 0 || crop.height() <= 0) { return Bitmap.createScaledBitmap(tmp, iconSize, iconSize, true); } // We want to crop a square region. float size = Math.max(crop.width(), crop.height()); float xShift = (size - crop.width()) * 0.5f; crop.left -= Math.floor(xShift); crop.right += Math.ceil(xShift); float yShift = (size - crop.height()) * 0.5f; crop.top -= Math.floor(yShift); crop.bottom += Math.ceil(yShift); Bitmap finalImage = Bitmap.createBitmap(iconSize, iconSize, Bitmap.Config.ARGB_8888); canvas.setBitmap(finalImage); float scale = iconSize / size; canvas.scale(scale, scale); canvas.drawBitmap(tmp, -crop.left, -crop.top, new Paint(Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG)); canvas.setBitmap(null); return finalImage; }
From source file:Main.java
/** * Extract an underlying bitmap from a drawable * * @param sourceDrawable The source drawable * @return The underlying bitmap//from w ww .ja v a2 s. c om */ public static Bitmap getBitmapFromDrawable(Drawable sourceDrawable) { if (sourceDrawable == null) { return null; } if (sourceDrawable instanceof BitmapDrawable) { return ((BitmapDrawable) sourceDrawable).getBitmap(); } else { //copying drawable object to not manipulate on the same reference Drawable.ConstantState constantState = sourceDrawable.getConstantState(); if (constantState == null) { return null; } Drawable drawable = constantState.newDrawable().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); return bitmap; } }
From source file:Main.java
public static Bitmap drawableToBitmap(Drawable drawable) { Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), drawable.getOpacity() != PixelFormat.OPAQUE ? Config.ARGB_8888 : Config.RGB_565); Canvas canvas = new Canvas(bitmap); drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight()); drawable.draw(canvas); return bitmap; }
From source file:Main.java
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); drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight()); drawable.draw(canvas); return bitmap; }
From source file:com.google.android.apps.santatracker.presentquest.util.VectorUtil.java
public static BitmapDescriptor vectorToBitmap(Context context, @DrawableRes int id) { Drawable vectorDrawable = ResourcesCompat.getDrawable(context.getResources(), id, null); Bitmap bitmap = Bitmap.createBitmap(vectorDrawable.getIntrinsicWidth(), vectorDrawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);// www. j a va 2s . c o m Canvas canvas = new Canvas(bitmap); vectorDrawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight()); vectorDrawable.draw(canvas); return BitmapDescriptorFactory.fromBitmap(bitmap); }
From source file:Main.java
public static Bitmap drawable2Bitmap(Drawable drawable) { int width = drawable.getIntrinsicWidth(); int height = drawable.getIntrinsicHeight(); Bitmap result = Bitmap.createBitmap(width, height, drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565); Canvas canvas = new Canvas(result); drawable.setBounds(0, 0, width, height); drawable.draw(canvas); return result; }