How can i convert a state list Drawable to bitmap drawable in android.
I want to convert a statelist drawable into bitmap drawable.
I found a work around if i find any icon whose type is other then bitmap drawable then use this code
here searchResult.getIcon() is my icon value
Bitmap bitmap = Bitmap.createBitmap(searchResult.getIcon().getIntrinsicWidth(),searchResult.getIcon().getIntrinsicHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
searchResult.getIcon().setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
searchResult.getIcon().draw(canvas);
itemIconBitmap = bitmap;
e.printStackTrace();
You can use toBitmap or casting the current drawable
(stateListDrawable as? StateListDrawable)?.let { drawable ->
(drawable.current as BitmapDrawable).bitmap
}
Related
I am reading this blog post about VectorDrawable. It is mentioned that one of the trade-offs of using vector images is that it is more expensive to render. The blog also states:
For static vectors, the drawing stage only needs to be performed once and can then be cached to a Bitmap.
But the blog did not explain how to do the caching. How to do it?
Here are some ways to do it:
public static Bitmap getBitmapFromVectorDrawable(Context context, int drawableId) {
Drawable drawable = ContextCompat.getDrawable(context, drawableId);
// depending on the support lib version you may have to use
// Drawable drawable = AppCompatResources.getDrawable(context, drawableId);
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);
return bitmap;
}
With Android KTX
val bitmap = AppCompatResources.getDrawable(context, drawableId).toBitmap()
Hope it helps
I have a Drawable (Vector) and I want to animate it rotating like a wheel. How would I go about this problem?
This is the only question I could find similar to my problem but the issue is the answer doesn't apply to VectorDrawables: Animating and rotating an image in a Surface View
I don't have any code to show because I don't even know where to start. I need some guidance.
You can convert VectorDrawable to Bitmap and than use solution from link.
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;
}
I use a nine patch drawable in imageview and using below code I want to get Bitmap but this error happens.
Bitmap stickerBitmap = ((BitmapDrawable) Front.getDrawable()).getBitmap();
You cannot directly cast a NinePatchDrawable to a BitmapDrawable, since those are two different incompatible drawable types.
If you want to get a Bitmap from a non-BitmapDrawable you have to draw it with a Canvas onto a Bitmap:
Drawable drawable = Front.getDrawable();
Bitmap stickerBitmap = Bitmap.createBitmap(Front.getWidth(),
Front.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(stickerBitmap);
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
drawable.draw(canvas);
Note that your ImageView has to be layouted so that it has a non-zero size.
I am trying to add a color filer in a drawable and then convert it in Bitmap. The problem is when convert the drawable into bitmap it loses it's color filter.I used drawable in imageview and its have the color filter but using bitmap in imageview doesn't have any color effect. Why this happen ? Thanks in advance.
Use a canvas to blit the Drawable back onto a bitmap:
Canvas canvas;
Drawable drawable = <yourDrawable created from wherever>;
Bitmap bmp = <your Bitmap which is the same width/height as the drawable>
// blit the drawable onto the bitmap using a Canvas
canvas = new Canvas(bmp);
drawable.draw(canvas);
http://developer.android.com/guide/topics/graphics/2d-graphics.html
I had the same problem and I figured it out finally!
We need to do following thing to get the bitmap with color filter applied on it.
image_view.setDrawingCacheEnabled(true);
Bitmap bitmap = Bitmap.createBitmap(image_view.getDrawingCache());
I've faced the same issue and finally found a solution.
Drawable can have multiple states thus you might be drawing wrong state.
You should switch it to proper mode before drawing:
Drawable drawable = icon.loadDrawable(getContext());
if (drawable == null) {
return;
}
drawable.setState(new int[] {android.R.attr.state_enabled});
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.compress(Bitmap.CompressFormat.PNG, 100, out);
I have a gradient in view. I want to take a color on pixel on touch.I'm trying to get drawable from view and later bitmap. But it's throw
java.lang.ClassCastException: android.graphics.drawable.GradientDrawable cannot be cast to android.graphics.drawable.BitmapDrawable
at com.mti.videodiary.activity.SplashActivity.addColorGradient(SplashActivity.java:431)
Any ways to help. Thanks
You can create a bitmap of the GradientDrawable and then look for the colour of a specific pixel.
From this answer:
// create bitmap based on width and height of the gradient drawable
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
gd.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
gd.draw(canvas);
// bitmap now contains a bitmap of the gradient drawable