2015-09-22 27 views
6

Sto utilizzando Android Studio per convertire la mia immagine SVG in file XML. Funziona bene quando provo ad accedervi usando R.drawable.svgimage ma ora ho bisogno di decodificare quell'immagine in bitmap.Decodifica immagine SVG in bitmap

Ho provato quanto segue. Restituisce null per la bitmap.

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

I file SVG * sono * file XML. Android Studio non sta convertendo nulla. Android non supporta i file SVG in modo nativo. Dovrai utilizzare una delle librerie esterne per fare qualsiasi cosa con loro. Tuttavia, se i file SVG sono abbastanza semplici, è possibile convertirli in VectorDrawables. –

risposta

2

È possibile utilizzare questa libreria SVG Android e usarlo in questo modo:

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

Dopo di che, convertire il formato SVG in un Drawable:

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

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

e poi, drawable in un bitmap:

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

Non c'è un modo per farlo senza usare la libreria esterna? – RisingUp

+0

apparentemente anche google costruisce una libreria tempo fa: https://code.google.com/p/svg-android/ e continua oltre: https://github.com/pents90/svg-android ma sembra, sì, tu bisogno di essa. –

+2

Ora che Android Studio supporta la conversione in SVG in 1.4 build. http://android-developers.blogspot.in/2015/09android-studio-14.html. Spero che Google ci dica come ottenere una bitmap senza una libreria esterna. – RisingUp

1

provare questo,

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 è la stessa libreria di: https://code.google.com/p/svg-android/ –

+0

sì, prova questo ok –

3

Il seguente codice funziona perfettamente ho usato: Qui R.drawable.ic_airport è la mia immagine svg memorizzati nella cartella drawable.

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