2015-09-22 13 views
6

Ich verwende Android Studio, um mein SVG-Bild in XML-Datei zu konvertieren. Es funktioniert gut, wenn ich versuche, mit R.drawable.svgimage darauf zuzugreifen, aber jetzt muss ich dieses Bild zu Bitmap decodieren.Decoding SVG-Bild zu Bitmap

Ich habe Folgendes versucht. Es gibt null für die Bitmap zurück.

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

SVG-Dateien * sind * XML-Dateien. Android Studio konvertiert nichts. Android unterstützt SVG-Dateien nicht nativ. Sie müssen eine der externen Bibliotheken verwenden, um etwas mit ihnen zu tun. Wenn SVG-Dateien jedoch einfach genug sind, können Sie sie möglicherweise in VectorDrawables konvertieren. –

Antwort

2

können Sie diese Bibliothek verwenden SVG Android und es auf diese Weise verwenden:

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

Danach wandeln die SVG in eine Drawable:

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

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

und dann die ziehbar in ein Bitmap:

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

Gibt es keine Möglichkeit, dies ohne externe Bibliothek zu tun? – RisingUp

+0

anscheinend noch Google bauen eine Bibliothek Zeit vor: https://code.google.com/p/svg-android/ und geht weiter: https://github.com/pents90/svg-android sieht aber aus, ja, du brauchen. –

+2

Nun, dass Android Studio Konvertierung in SVG in 1.4 Build unterstützt. http://android-developers.blogspot.in/2015/09/android-studio-14.html. Ich hoffe, dass Google uns erklärt, wie man ohne eine externe Bibliothek eine Bitmap daraus machen kann. – RisingUp

1

versuchen dies,

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

SVGParser ist die gleiche Bibliothek wie: https://code.google.com/p/svg-android/ –

+0

ja, versuche das ok –

3

Der folgende Code funktioniert perfekt ich es benutzt haben: Hier R.drawable.ic_airport mein svg Bild in ziehbar Ordner gespeichert ist.

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