List of usage examples for android.graphics.drawable Drawable draw
public abstract void draw(@NonNull Canvas canvas);
From source file:com.dm.material.dashboard.candybar.helpers.DrawableHelper.java
@Nullable public static Drawable getDefaultImage(@NonNull Context context, @DrawableRes int res) { try {// ww w. ja v a2 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:com.xxxifan.devbox.core.util.ViewUtils.java
public static Bitmap toBitmap(Drawable drawable) { if (drawable instanceof BitmapDrawable) { return ((BitmapDrawable) drawable).getBitmap(); }//w ww . j a v a 2s . c om 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:com.dv.Utils.Tools.java
/** * BitMap/*from w w w. j a va 2 s. c om*/ * * @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:Main.java
public static Bitmap createFramedImage(Drawable imageDrawable, int borderThickness) { int size = Math.min(imageDrawable.getMinimumWidth(), imageDrawable.getMinimumHeight()); Bitmap output = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(output); RectF outerRect = new RectF(0, 0, size, size); Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); paint.setColor(Color.RED);/* w w w. jav a 2 s. co m*/ canvas.drawRect(outerRect, paint); paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN)); imageDrawable.setBounds(0, 0, size, size); // Save the layer to apply the paint canvas.saveLayer(outerRect, paint, Canvas.ALL_SAVE_FLAG); imageDrawable.draw(canvas); canvas.restore(); // FRAMING THE PHOTO float border = size / 15f; // 1. Create offscreen bitmap link: http://www.youtube.com/watch?feature=player_detailpage&v=jF6Ad4GYjRU#t=1035s Bitmap framedOutput = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888); Canvas framedCanvas = new Canvas(framedOutput); // End of Step 1 // Start - TODO IMPORTANT - this section shouldn't be included in the final code // It's needed here to differentiate step 2 (red) with the background color of the activity // It's should be commented out after the codes includes step 3 onwards // Paint squaredPaint = new Paint(Paint.ANTI_ALIAS_FLAG); // squaredPaint.setColor(Color.BLUE); // framedCanvas.drawRoundRect(outerRect, 0f, 0f, squaredPaint); // End // 2. Draw an opaque rounded rectangle link: RectF innerRect = new RectF(border, border, size - border, size - border); Paint innerPaint = new Paint(Paint.ANTI_ALIAS_FLAG); innerPaint.setColor(Color.RED); // framedCanvas.drawRoundRect(innerRect, cornerRadius, cornerRadius, outerPaint); framedCanvas.drawRect(innerRect, innerPaint); // 3. Set the Power Duff mode link: Paint outerPaint = new Paint(Paint.ANTI_ALIAS_FLAG); outerPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_OUT)); // 4. Draw a translucent rounded rectangle link: outerPaint.setColor(Color.argb(255, 255, 255, 255)); // framedCanvas.drawRoundRect(outerRect, cornerRadius, cornerRadius, outerPaint); framedCanvas.drawRect(outerRect, outerPaint); // 5. Draw the frame on top of original bitmap canvas.drawBitmap(framedOutput, 0f, 0f, null); return output; }
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 ww. j a v a 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.google.samples.apps.ourstreets.view.ViewUtils.java
/** * Creates a {@link BitmapDescriptor} from a drawable. * This is particularly useful for {@link GoogleMap} {@link Marker}s. * * @param drawable The drawable that should be a {@link BitmapDescriptor}. * @return The created {@link BitmapDescriptor}. *///from w w w .ja v a 2 s . co m @NonNull public static BitmapDescriptor getBitmapDescriptorFromDrawable(@NonNull Drawable drawable) { BitmapDescriptor bitmapDescriptor; // Usually the pin could be loaded via BitmapDescriptorFactory directly. // The target map_pin is a VectorDrawable which is currently not supported // within BitmapDescriptors. int width = drawable.getIntrinsicWidth(); int height = drawable.getIntrinsicHeight(); drawable.setBounds(0, 0, width, height); Bitmap markerBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(markerBitmap); drawable.draw(canvas); bitmapDescriptor = BitmapDescriptorFactory.fromBitmap(markerBitmap); return bitmapDescriptor; }
From source file:Main.java
@SuppressWarnings("unused") public static Bitmap drawableToBitmap(Drawable drawable) { Bitmap bitmap;//from w w w.j a v a 2s .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:com.destin.sehaikun.DrawableUtils.java
public static Bitmap getBitmapFromDrawable(Drawable drawable) { if (drawable == null) { return null; }// ww w. j av a2s. c om 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
public static Bitmap drawableToBitmap(Drawable drawable) { if (drawable == null) drawable = new ColorDrawable(Color.TRANSPARENT); Bitmap bitmap;//from w ww. java 2 s.co 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); 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. *//*from w w w . j a v a2s. 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; }