Android 本地svg的使用和转换成Bitmap

android studio 导入本地SVG文件

1. 创建xml文件

在这里插入图片描述

2. 生成xml文件请添加图片描述

3. 完成在这里插入图片描述

在这里插入图片描述

使用SVG

SVG 转 Bitmap

    /**
     * SVG 转 Bitmap
     * @param context 上下文
     * @param drawableId SVG资源
     * @return
     */
    public static Bitmap getBitmapFromVectorDrawable(Context context, int drawableId) {
    
    
        Drawable drawable = ContextCompat.getDrawable(context, drawableId);
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
    
    
            drawable = (DrawableCompat.wrap(drawable)).mutate();
        }

        Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(),
                drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        canvas.drawColor(WHITE);//背景
        drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
        drawable.draw(canvas);

        return bitmap;
    }
```
`java
在这里插入代码片
```

猜你喜欢

转载自blog.csdn.net/Hh19900902/article/details/127849591