2015-09-22 29 views
6

Tôi đang sử dụng Android Studio để chuyển đổi hình ảnh SVG thành tệp XML. Nó hoạt động tốt khi tôi cố gắng truy cập nó bằng cách sử dụng R.drawable.svgimage nhưng bây giờ tôi cần giải mã hình ảnh đó thành bitmap.Giải mã hình ảnh SVG thành bitmap

Tôi đã thử cách sau. Nó trả về null cho 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

Tệp SVG * là * Tệp XML. Android Studio không chuyển đổi bất kỳ thứ gì. Android không hỗ trợ tệp SVG nguyên bản. Bạn sẽ cần phải sử dụng một trong các thư viện bên ngoài để làm bất cứ điều gì với họ. Tuy nhiên, nếu tệp SVG của bạn đủ đơn giản, bạn có thể chuyển đổi chúng thành VectorDrawables. –

Trả lời

2

Bạn có thể sử dụng thư viện này SVG Android và sử dụng nó theo cách này:

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

Sau đó, chuyển đổi SVG thành một drawable:

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

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

và sau đó, đối tượng vẽ thành một bitmap:

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

Không có cách nào để làm điều đó mà không sử dụng thư viện bên ngoài? – RisingUp

+0

dường như thậm chí google xây dựng một thư viện thời gian trước đây: https://code.google.com/p/svg-android/ và tiếp tục qua: https://github.com/pents90/svg-android nhưng có vẻ như, vâng, bạn cần nó. –

+2

Giờ đây, Android studio hỗ trợ chuyển đổi sang SVG trong 1,4 bản dựng. http://android-developers.blogspot.in/2015/09/android-studio-14.html. Tôi hy vọng Google sẽ cho chúng tôi biết cách lấy bitmap ra khỏi nó mà không có thư viện bên ngoài. – RisingUp

1

thử này,

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 là thư viện giống như: https://code.google.com/p/svg-android/ –

+0

có, hãy thử điều này ok –

3

Đoạn mã dưới đây sẽ hoạt động hoàn hảo Tôi đã sử dụng nó: Dưới đây là hình ảnh R.drawable.ic_airport svg tôi được lưu trữ trong thư mục 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); 
Các vấn đề liên quan