Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/svg/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 将SVG图像解码为位图_Android_Svg_Bitmap - Fatal编程技术网

Android 将SVG图像解码为位图

Android 将SVG图像解码为位图,android,svg,bitmap,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; B

我正在使用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); 
您可以通过以下方式使用此库:

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转换为可绘制的:

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

// Turn into a drawable
Drawable drawable = svg.createDrawable();
然后,可绘制为位图:

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

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

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

    @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) 通过vd.getIntrinsicWidth()计算位图比率和大小;和vd.getIntrinsicHeight()

3) 使用位图创建画布

mResId = R.drawable.svgimage
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
Bitmap bitmap = BitmapFactory.decodeResource(
            mContext.getResources(), mResId, options); 
4) 使用垂直立根(左、上、右、下);作为目标矩形

5) 最后得出:

vd.draw( canvas );

在包
androidx.core.graphics.drawable中有一个函数


有没有一种方法不使用外部库就可以做到这一点?很显然,谷歌很久以前就已经构建了一个库:并且继续:但是看起来,是的,你需要它。现在Android studio支持在1.4版本中转换为SVG。我希望Google告诉我们如何在没有外部库的情况下从中获取位图。SVG文件是XML文件。Android Studio没有转换任何内容。Android本机不支持SVG文件。您需要使用其中一个外部库来处理它们。但是,如果您的SVG文件足够简单,您可能可以将其转换为VectorDrawables。请提供仅为右侧和底部设置边界的说明。为什么不呢?为什么要设置它?为什么是@TargetApi(Build.VERSION\u CODES.LOLLIPOP)?这是否意味着它不能在具有其他api级别的设备上工作?正如您所看到的,有两种方法,第一种方法@TargetApi(Build.VERSION\u CODES.LOLLIPOP)用于目标棒棒糖版本,另一种用于rest目标版本。SVG和SVGParser从何而来?
val yourBitmap = getDrawable(R.drawable.svgimage)!!.toBitmap(width, height)