Oliver Dixon
Oliver Dixon

Reputation: 7405

Drawable to Bitmap, Drawable unknown size / dimension

I have a drawable object that is a vector. Since you can't crop (or clip because of ICS forced hardware acceleration) a drawable it make's sprite animation useless.

I'm trying to convert the drawable to a bitmap with a specific size (percentage of the lowest screen dimension for best results).

The version needs to be super memory efficient of course.

What I need help with is creating the bitmap with the specific size, here's what I got so far:

public Bitmap svgTObitmap(String name, int percentage, int screen_width, int screen_height)
{
    _resID = _context.getResources().getIdentifier( name , "raw" , _context.getPackageName());
    SVG svg = SVGParser.getSVGFromResource(_context.getResources(), _resID);

    Drawable drawable = svg.createPictureDrawable();

    Bitmap bmp = ((BitmapDrawable)drawable).getBitmap();

    return bmp;
}

Upvotes: 3

Views: 2297

Answers (1)

kabuko
kabuko

Reputation: 36302

Being from a vector, clearly your Drawable is not in fact a BitmapDrawable. You need to instead draw the Drawable on a Bitmap. See this answer I gave to another question:

Bitmap bmp = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(bmp); 
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
drawable.draw(canvas);

Obviously in your case you would specify the size you want explicitly instead of trying to get the intrinsic height/width of the drawable.

Upvotes: 7

Related Questions