The use and conversion of Android local svg to Bitmap

Android studio imports local SVG files

1. Create an xml file

insert image description here

2. Generate xml filePlease add a picture description

3. finishinsert image description here

insert image description here

Use SVG

SVG to 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
在这里插入代码片
```

Guess you like

Origin blog.csdn.net/Hh19900902/article/details/127849591