【问题标题】:Drawable to Bitmap, Drawable unknown size / dimension可绘制到位图,可绘制未知大小/尺寸
【发布时间】:2012-05-11 09:59:17
【问题描述】:

我有一个可绘制对象,它是一个矢量。由于您无法裁剪(或由于 ICS 强制硬件加速而剪辑)可绘制对象,因此它使精灵动画无用。

我正在尝试将可绘制对象转换为具有特定大小的位图(最低屏幕尺寸的百分比以获得最佳效果)。

版本当然需要超高内存效率。

我需要帮助的是创建具有特定大小的位图,这是我目前得到的:

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

【问题讨论】:

    标签: java android bitmap drawable


    【解决方案1】:

    来自向量,显然您的Drawable 实际上不是BitmapDrawable。您需要在Bitmap 上绘制Drawable。看到这个answer我给了另一个问题:

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

    显然,在您的情况下,您将明确指定所需的大小,而不是尝试获取可绘制对象的固有高度/宽度。

    【讨论】:

    • 非常感谢,我已经坚持了几个小时了。
    猜你喜欢
    • 1970-01-01
    • 2012-02-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多