简体   繁体   中英

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM