【问题标题】:Decoding SVG image to bitmap将 SVG 图像解码为位图
【发布时间】:2015-12-19 09:55:15
【问题描述】:

我正在使用 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 文件 XML 文件。 Android Studio 没有转换任何东西。 Android 本身不支持 SVG 文件。您将需要使用其中一个外部库来对它们执行任何操作。但是,如果您的 SVG 文件足够简单,您也许可以将它们转换为 VectorDrawables。

标签: android svg bitmap


【解决方案1】:

以下代码可以完美运行我已经使用它: 这里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);

【讨论】:

  • 请有人解释一下仅设置右下角的边界。为什么不适合所有人?为什么要设置它?
  • 为什么@TargetApi(Build.VERSION_CODES.LOLLIPOP)?这是否意味着它不适用于具有其他 api 级别的设备?
  • 如您所见,有两种方法,第一种方法@TargetApi(Build.VERSION_CODES.LOLLIPOP) 适用于Target lollipop 以上版本,另一种适用于其余目标版本。
【解决方案2】:

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

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

【讨论】:

    【解决方案3】:

    你可以使用这个库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);
    

    【讨论】:

    【解决方案4】:

    试试这个,

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

    【讨论】:

    • SVG 和 SVGParser 来自哪里?
    【解决方案5】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-04-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-04
      • 2012-12-04
      • 1970-01-01
      • 2012-09-19
      相关资源
      最近更新 更多