将SVG图像解码为位图

时间:2015-09-22 12:13:26

标签: android svg bitmap

我正在使用Android Studio将我的SVG图像转换为XML文件。当我尝试使用R.drawable.svgimage访问它时它工作正常,但现在我需要将该图像解码为位图。

我尝试了以下内容。它为位图返回null。

mResId = R.drawable.svgimage
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
Bitmap bitmap = BitmapFactory.decodeResource(
            mContext.getResources(), mResId, options); 

5 个答案:

答案 0 :(得分:7)

以下代码将完美运行我已经使用过它:   这里R.drawable.ic_airport是我的svg图像,存储在drawable文件夹中。

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
     private static 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);
        Log.e(TAG, "getBitmap: 1");
        return bitmap;
    }

      private static Bitmap getBitmap(Context context, int drawableId) {
        Log.e(TAG, "getBitmap: 2");
        Drawable drawable = ContextCompat.getDrawable(context, drawableId);
        if (drawable instanceof BitmapDrawable) {
            return BitmapFactory.decodeResource(context.getResources(), drawableId);
        } else if (drawable instanceof VectorDrawable) {
            return getBitmap((VectorDrawable) drawable);
        } else {
            throw new IllegalArgumentException("unsupported drawable type");
        }
    }

       Bitmap bitmap = getBitmap(getContext(), R.drawable.ic_airport);

答案 1 :(得分:2)

您可以使用此库SVG Android并以这种方式使用它:

SVG svg = new SVGBuilder()
            .readFromResource(getResources(), R.raw.someSvgResource) // if svg in res/raw
            .readFromAsset(getAssets(), "somePicture.svg")           // if svg in assets
            // .setWhiteMode(true) // draw fills in white, doesn't draw strokes
            // .setColorSwap(0xFF008800, 0xFF33AAFF) // swap a single colour
            // .setColorFilter(filter) // run through a colour filter
            // .set[Stroke|Fill]ColorFilter(filter) // apply a colour filter to only the stroke or fill
            .build();

之后,将SVG转换为Drawable:

// Draw onto a canvas
canvas.drawPicture(svg.getPicture());

// Turn into a drawable
Drawable drawable = svg.createDrawable();

然后,将drawable转换为位图:

Bitmap bitmapsvg = BitmapFactory.decodeResource(context.getResources(),drawable);

答案 2 :(得分:1)

试试这个,

SVG svg = SVGParser.getSVGFromResource(getResources(), R.raw.android);
PictureDrawable pictureDrawable = svg.createPictureDrawable();
Bitmap bitmap = Bitmap.createBitmap(pictureDrawable.getIntrinsicWidth(), pictureDrawable.getIntrinsicHeight(), Config.ARGB_8888);

答案 3 :(得分:0)

1)创建一个VectorDrawable

VectorDrawable vd = (VectorDrawable) context.getDrawable( R.drawable.ic_00 );

2)通过vd.getIntrinsicWidth();计算位图比率和大小。和vd.getIntrinsicHeight();。

3)使用位图创建画布。

4)使用vd.setBounds(left,top,right,bottom);作为目标矩形

5)最后画:

vd.draw( canvas );

答案 4 :(得分:0)

在软件包androidx.core.graphics.drawable中有一个函数Drawable.toBitmap

val yourBitmap = getDrawable(R.drawable.svgimage)!!.toBitmap(width, height)