Create a rotation animation with a Drawable in SurfaceView - android

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

Related

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

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.

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

Android clip Picture to Bitmap. Is Possible?

I have a Picture object, loaded from an SVG file, and I have set hardwareAccelerated=false to make it works on all devices.
Since there is a bug on android 4.0.4, I have to convert the Picture to Bitmap and I do that, in this way:
...
...
public void draw(Canvas canvas) {
...
...
//myPicture size is 9000x5000 but I want to display only this portion
clipRect.set(50, 50, 370, 530);
Bitmap bmp = getBitmapFromPicture(myPicture, clipRect);
canvas.drawBitmap(bmp, 0, 0, null);
bmp.recycle();
...
...
}
public static Bitmap getBitmapFromPicture(Picture picture, RectF clipRect) {
Bitmap bitmap = Bitmap.createBitmap(Math.round(clipRect.width()), Math.round(clipRect.height()), Config.RGB_565);
Canvas canvas = new Canvas(bitmap);
canvas.drawPicture(picture);
}
Now I want to clip the Picture because I want to display only the visible screen part of it.
But the canvas.drawPicture does not accept srcRect parameter.
How is it possible to achieve this?
EDIT:
By translate the canvas: canvas.translate(-50, -50) it seems that translate the bitmap, too.
You need to set a transform on the Canvas.
To move the portion of the picture at 50,50 down so it is on the bitmap (ie. at 0,0), just do:
canvas.translate(-50, -50);
So your method becomes:
public static Bitmap getBitmapFromPicture(Picture picture, RectF clipRect)
{
Bitmap bitmap = Bitmap.createBitmap(Math.round(clipRect.width()),
Math.round(clipRect.height()),
Config.RGB_565);
Canvas canvas = new Canvas(bitmap);
canvas.translate(-clipRect.left, -clipRect.top);
canvas.drawPicture(picture);
}

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

Create widget with customview

I'd like to use several CustomViews in my appwidget and thus want to create a bitmap out of every CustomView.
So i tried that with
Bitmap customBitmap = customView.getDrawingCache(true);
remoteViews.setImageViewBitmap(R.id.imageView1, customBitmap );
but it doesn't work at all.
Are there any suggestions?
yes, it is drawn on a canvas
I would do something like this:
public Bitmap createCustomView(){
Bitmap bitmap = Bitmap.createBitmap(BITMAP_WIDTH, BITMAP_HEIGHT, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
// draw on the canvas:
// ...
return bitmap;
}
and then set the Bitmap to the ImageView:
remoteViews.setImageViewBitmap(R.id.imageView1, createCustomView() );

Categories

Resources