NinePatchDrawable cannot be cast to android.graphics.drawable.BitmapDrawable - android

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.

Related

Create a rotation animation with a Drawable in SurfaceView

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

How can i convert a statelistDrawable to bitmapdrawable in android

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
}

Drawable loses color filter after converting into bitmap

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

How to get color pixel on touch from GradientDrawable

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

Android - spitting bitmap results to low quality

My goal is to create a half-oval gradient image. I create a gradient Drawable which afterwards is converted to a bitmap and cut in half to get my final shape which shows at the right of the picture.
I have doubts about if this is the best way to go about that so feel free to recommend other approaches.
The problem is that the quality is low after splitting the bitmap.You can see that the edges of the half gradient are pixelized. This happens at the point of splitting the bitmap.
What should I do to retain the initial quality?
//create gradient drawable
GradientDrawable gd = new GradientDrawable(orientation,new int[] {firstColor, secondColor});
gd.setCornerRadius(0f);
gd.setShape(GradientDrawable.OVAL);
//convert drawable to bitmap
int width=imageView.getLayoutParams().width;
int height=imageView.getLayoutParams().height;
Bitmap bitmap = Bitmap.createBitmap(width,height, Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
drawable.draw(canvas);
//cut bitmap in half
bitmap=Bitmap.createBitmap(bitmap, width/2, 0, width/2, height);
imageView.setImageBitmap(bitmap);
imageView..setScaleType(ScaleType.FIT_XY);

Categories

Resources