public Bitmap drawableToBitmap(Drawable drawable) { Bitmap bitmap = null; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && (drawable instanceof VectorDrawable || drawable instanceof VectorDrawableCompat)) { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { drawable = (DrawableCompat.wrap(drawable)).mutate(); } 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); } else if (drawable instanceof BitmapDrawable) { bitmap = ((BitmapDrawable) drawable).getBitmap(); } return bitmap; }
public Bitmap getBitmapFromDrawable(int drawableId) { Bitmap bitmap = null; Drawable drawable = ContextCompat.getDrawable(_context, drawableId); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && (drawable instanceof VectorDrawable || drawable instanceof VectorDrawableCompat)) { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { drawable = (DrawableCompat.wrap(drawable)).mutate(); } 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); } else if (drawable instanceof BitmapDrawable) { bitmap = ((BitmapDrawable) drawable).getBitmap(); } return bitmap; }
public static Bitmap drawableToBitmap(Drawable thumb) { if (thumb == null) { return null; } Bitmap bitmap; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { if (thumb instanceof VectorDrawable) { int w = thumb.getIntrinsicWidth(); int h = thumb.getIntrinsicHeight(); Bitmap.Config config = thumb.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565; bitmap = Bitmap.createBitmap(w, h, config); Canvas canvas = new Canvas(bitmap); thumb.setBounds(0, 0, w, h); thumb.draw(canvas); } else { bitmap = ((BitmapDrawable) thumb).getBitmap(); } } else { bitmap = ((BitmapDrawable) thumb).getBitmap(); } return bitmap; }
/** * Tests the {@link Coloring#colorVectorDrawable(VectorDrawable, int)} method. * <p> * Unfortunately {@link VectorDrawable#setColorFilter(int, PorterDuff.Mode)} is not mocked in Android JAR yet. */ @Test @TargetApi(Build.VERSION_CODES.LOLLIPOP) public final void testColorVectorDrawable() { try { final VectorDrawable vectorDrawable = new VectorDrawable(); assertNotNull("VectorDrawable is null", vectorDrawable); final VectorDrawable colored = Coloring.colorVectorDrawable(vectorDrawable, Color.RED); final PorterDuff.Mode mode = PorterDuff.Mode.SRC_ATOP; assertEquals("Vector color filter does not match", new PorterDuffColorFilter(Color.RED, mode), colored.getColorFilter()); } catch (RuntimeException e) { boolean knownIssue = e.getMessage().contains("not mocked"); if (!knownIssue) { e.printStackTrace(); } assertTrue("Unknown error: " + e.getMessage(), knownIssue); } }
/** * Получение объекта Bitmap из Drawable из ресурсов */ public static Bitmap getBitmapFromDrawable(Context context, @DrawableRes int drawableId) { Drawable drawable = ContextCompat.getDrawable(context, drawableId); if (drawable instanceof BitmapDrawable) { return ((BitmapDrawable) drawable).getBitmap(); } else if (drawable instanceof VectorDrawable || drawable instanceof VectorDrawableCompat) { 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; } else { throw new IllegalArgumentException("unsupported drawable type"); } }
public static Bitmap createBitmap(Context context, @DrawableRes int drawableResId, @ColorRes int tintColor) { Drawable drawable = ContextCompat.getDrawable(context, drawableResId).mutate(); if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { return createBitmapKitKat(context, drawable, tintColor); } if (tintColor != 0) { DrawableCompat.setTint(drawable, ContextCompat.getColor(context, tintColor)); } if (drawable instanceof BitmapDrawable) { return ((BitmapDrawable) drawable).getBitmap(); } else if (drawable instanceof VectorDrawableCompat) { return createBitmap((VectorDrawableCompat) drawable); } else if (drawable instanceof VectorDrawable) { return createBitmap((VectorDrawable) drawable); } else { throw new IllegalArgumentException("Unsupported drawable type"); } }
@Override @TargetApi(21) public void onDraw(Canvas c){ paint.setStrokeWidth(10); for (int i = 2; i < 7; i++){ c.drawLine(20, DensityMetrics.getSpaceHeight() * i + DensityMetrics.Companion.getToolbarHeight(),STAFF_WIDTH, DensityMetrics.getSpaceHeight() * i + DensityMetrics.Companion.getToolbarHeight(), paint); } float drawX = 320; for (ArrayList<Note> chord : MusicStore.sheet){ for (Note note : chord){ int noteHeadID = 0; if (note.getRhythm() == 2){ noteHeadID = R.drawable.half_note_head; }else{ noteHeadID = R.drawable.quarter_note_head; } VectorDrawable noteHead = (VectorDrawable) getResources().getDrawable(noteHeadID); Bitmap nh = NoteBitmap.getBitmap(noteHead); c.drawBitmap(Bitmap.createScaledBitmap(nh, (int) (DensityMetrics.getSpaceHeight() * 1.697), (int) DensityMetrics.getSpaceHeight(), true), (int) drawX , note.getY() - DensityMetrics.getSpaceHeight() / 2, paint); } drawX += 600; } }
/** * Returns {@code true} if the target drawable needs to be tileified. * <p/> * Note: Copied from android.widget.ProgressBar * * @param dr the drawable to check * @return {@code true} if the target drawable needs to be tileified, * {@code false} otherwise */ private static boolean needsTileify(Drawable dr) { if (dr instanceof LayerDrawable) { final LayerDrawable orig = (LayerDrawable) dr; final int N = orig.getNumberOfLayers(); for (int i = 0; i < N; i++) { if (needsTileify(orig.getDrawable(i))) { return true; } } return false; } // If there's a bitmap that's not wrapped with a ClipDrawable or // ScaleDrawable, we'll need to wrap it and apply tiling. return dr instanceof BitmapDrawable || dr instanceof VectorDrawable; }
/** * Colors the given drawable to the specified color. Uses {@link PorterDuff.Mode#SRC_ATOP}. * * @param context Which context to use * @param drawable Which drawable to color * @param color Which color to use * @return A colored drawable, new instance in most cases for bitmaps, cached instance for most other cases */ @NonNull public static Drawable colorDrawable(@NonNull final Context context, @NonNull final Drawable drawable, @ColorInt final int color) { if (drawable instanceof VectorDrawable && Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { return colorVectorDrawable((VectorDrawable) drawable, color); } if (drawable instanceof VectorDrawableCompat) { return colorVectorDrawableCompat((VectorDrawableCompat) drawable, color); } if (drawable instanceof ColorDrawable && Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { ((ColorDrawable) drawable).setColor(color); return drawable; } if (drawable instanceof GradientDrawable) { drawable.setColorFilter(color, PorterDuff.Mode.SRC_ATOP); return drawable; } if (drawable instanceof BitmapDrawable) { final Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap(); return new BitmapDrawable(context.getResources(), colorBitmap(bitmap, color)); } // have no idea what this is.. return colorUnknownDrawable(drawable, color); }
static public Bitmap getBitmap(Context context, int drawableId) { Drawable drawable = ContextCompat.getDrawable(context, drawableId); if (drawable instanceof BitmapDrawable) { return BitmapFactory.decodeResource(context.getResources(), drawableId); } else if (drawable instanceof VectorDrawable) { return getBitmap((VectorDrawable) drawable); } else { throw new IllegalArgumentException("unsupported drawable type"); } }
static private Bitmap getBitmap(VectorDrawable vectorDrawable) { Bitmap bitmap = Bitmap.createBitmap(vectorDrawable.getIntrinsicWidth(), vectorDrawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); vectorDrawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight()); vectorDrawable.draw(canvas); return bitmap; }
private static Bitmap getBitmapFromResource(VectorDrawable vectorDrawable) { Bitmap bitmap = Bitmap.createBitmap(vectorDrawable.getIntrinsicWidth(), vectorDrawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); vectorDrawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight()); vectorDrawable.draw(canvas); return bitmap; }
/** * Get bitmap from resource */ public static Bitmap getBitmapFromResource(Context context, int drawableId) { Drawable drawable = ContextCompat.getDrawable(context, drawableId); if (drawable instanceof BitmapDrawable) { return BitmapFactory.decodeResource(context.getResources(), drawableId); } else if (drawable instanceof VectorDrawable) { return getBitmapFromResource((VectorDrawable) drawable); } else { throw new IllegalArgumentException("unsupported drawable type"); } }
@Override protected void onDraw(Canvas canvas) { Drawable drawable = getDrawable(); if (drawable == null) { return; } if (getWidth() == 0 || getHeight() == 0) { return; } Bitmap b = null; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && drawable instanceof VectorDrawable) { ((VectorDrawable) drawable).draw(canvas); b = Bitmap.createBitmap(canvas.getWidth(), canvas.getHeight(), Bitmap.Config.ARGB_8888); Canvas c = new Canvas(); c.setBitmap(b); drawable.draw(c); } else if (drawable instanceof BitmapDrawable){ b = ((BitmapDrawable) drawable).getBitmap(); } else if (drawable instanceof LayerDrawable) { LayerDrawable layerDrawable = (LayerDrawable) drawable; b = Bitmap.createBitmap(canvas.getWidth(), canvas.getHeight(), Bitmap.Config.ARGB_8888); layerDrawable.draw(new Canvas(b)); } Bitmap bitmap = b.copy(Bitmap.Config.ARGB_8888, true); int w = getWidth(), h = getHeight(); Bitmap roundBitmap = getCroppedBitmap(bitmap, w); canvas.drawBitmap(roundBitmap, 0,0, null); }
public static Bitmap getBitmap(Drawable drawable, int width, int height) { if (drawable instanceof BitmapDrawable) { return ((BitmapDrawable) drawable).getBitmap(); } else if (drawable instanceof VectorDrawableCompat) { return getBitmap((VectorDrawableCompat) drawable, width, height); } else if (drawable instanceof VectorDrawable) { return getBitmap((VectorDrawable) drawable, width, height); } else { throw new IllegalArgumentException("Unsupported drawable type"); } }
@TargetApi(Build.VERSION_CODES.LOLLIPOP) private static Bitmap getBitmap(VectorDrawable vectorDrawable, int width, int height) { Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); vectorDrawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight()); vectorDrawable.draw(canvas); return bitmap; }
public static Drawable getVecDrawable(Context context, @DrawableRes int id) { Drawable drawable = AppCompatResources.getDrawable(context, id); if (!(drawable instanceof VectorDrawableCompat || drawable instanceof VectorDrawable)) { throw new RuntimeException(); } return drawable; }
@TargetApi(Build.VERSION_CODES.LOLLIPOP) private static Bitmap getBitmap(VectorDrawable vectorDrawable) { Bitmap bitmap = Bitmap.createBitmap(vectorDrawable.getIntrinsicWidth(), vectorDrawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); vectorDrawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight()); vectorDrawable.draw(canvas); return bitmap; }
public static Bitmap getBitmap(Context context, @DrawableRes int drawableResId) { Drawable drawable = ContextCompat.getDrawable(context, drawableResId); if (drawable instanceof BitmapDrawable) { return ((BitmapDrawable) drawable).getBitmap(); } else if (drawable instanceof VectorDrawableCompat) { return getBitmap((VectorDrawableCompat) drawable); } else if (drawable instanceof VectorDrawable) { return getBitmap((VectorDrawable) drawable); } else { throw new IllegalArgumentException("Unsupported drawable type"); } }
public Bitmap getBitmap(Context context, int drawableId) { Drawable drawable = ContextCompat.getDrawable(context, drawableId); if (drawable instanceof BitmapDrawable) { return ((BitmapDrawable) drawable).getBitmap(); } else if (drawable instanceof VectorDrawable) { return getBitmap((VectorDrawable) drawable); } else { throw new IllegalArgumentException("unsupported drawable type"); } }
@TargetApi(Build.VERSION_CODES.LOLLIPOP) private Bitmap getBitmap(VectorDrawable vectorDrawable) { Bitmap bitmap = Bitmap.createBitmap(vectorDrawable.getIntrinsicWidth(), vectorDrawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); vectorDrawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight()); vectorDrawable.draw(canvas); return bitmap; }
public HeartBeatIndicator(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); mHeartBeatVectorDrawable = (AnimatedVectorDrawable) context.getDrawable( R.drawable.avd_heartbeat); mCircleGray = (VectorDrawable) context.getDrawable(R.drawable.vd_circle_gray); /* Start in off mode */ off(); }
@TargetApi(Build.VERSION_CODES.LOLLIPOP) private static Bitmap createBitmap(VectorDrawable vectorDrawable) { Bitmap bitmap = Bitmap.createBitmap(vectorDrawable.getIntrinsicWidth(), vectorDrawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); vectorDrawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight()); vectorDrawable.draw(canvas); return bitmap; }
@TargetApi(Build.VERSION_CODES.LOLLIPOP) public static Bitmap getBitmap(VectorDrawable vectorDrawable) { Bitmap bitmap = Bitmap.createBitmap(vectorDrawable.getIntrinsicWidth(), vectorDrawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); vectorDrawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight()); vectorDrawable.draw(canvas); return bitmap; }
@Override protected void onDraw(Canvas canvas) { Drawable drawable = getDrawable(); if (drawable == null) { return; } if (getWidth() == 0 || getHeight() == 0) { return; } Bitmap b = null; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && drawable instanceof VectorDrawable) { ((VectorDrawable) drawable).draw(canvas); b = Bitmap.createBitmap(canvas.getWidth(), canvas.getHeight(), Bitmap.Config.ARGB_8888); Canvas c = new Canvas(); c.setBitmap(b); drawable.draw(c); } else { b = ((BitmapDrawable) drawable).getBitmap(); } Bitmap bitmap = b.copy(Bitmap.Config.ARGB_8888, true); int w = getWidth(), h = getHeight(); Bitmap roundBitmap = getCroppedBitmap(bitmap, w); canvas.drawBitmap(roundBitmap, 0,0, null); }
@CheckResult @Nullable public static Drawable createTintedDrawable(@Nullable Drawable drawable, @ColorInt int color) { if (drawable == null) { return null; } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && drawable instanceof VectorDrawable) { drawable.setColorFilter(color, PorterDuff.Mode.SRC_IN); return drawable; } drawable = DrawableCompat.wrap(drawable.mutate()); DrawableCompat.setTintMode(drawable, PorterDuff.Mode.SRC_IN); DrawableCompat.setTint(drawable, color); return drawable; }
@Override protected void onDraw(Canvas canvas) { Log.d("ONDRAW", "onDraw called "); Drawable drawable = getDrawable(); if (drawable == null) { return; } if (getWidth() == 0 || getHeight() == 0) { return; } Bitmap b = null; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && drawable instanceof VectorDrawable) { ((VectorDrawable) drawable).draw(canvas); b = Bitmap.createBitmap(canvas.getWidth(), canvas.getHeight(), Config.ARGB_8888); Canvas c = new Canvas(); c.setBitmap(b); drawable.draw(c); } else { b = ((BitmapDrawable) drawable).getBitmap(); } Bitmap bitmap = b.copy(Config.ARGB_8888, true); int w = getWidth(), h = getHeight(); //Bitmap roundBitmap = getCroppedBitmap(bitmap, w); //Bitmap roundBitmap = blur(bitmap, 50, 220); //canvas.drawBitmap(roundBitmap, 0,0, null); super.onDraw(canvas); Bitmap ors = this.getDrawingCache(); Bitmap or = blur(ors, 20, (int)(0.3 * ors.getHeight())); canvas.drawBitmap(or, 0,0, null); }