igo Asked:2020-03-24 21:40:11 +0800 CST2020-03-24 21:40:11 +0800 CST 2020-03-24 21:40:11 +0800 CST Android 上的 SVG 到 JPEG 772 您知道在离线运行android应用程序时将SVG(矢量图像)转换为JPG/JPEG/PNG (任何光栅格式)的方法吗? android 1 个回答 Voted Best Answer igo 2020-03-27T17:05:48+08:002020-03-27T17:05:48+08:00 VectorDrawable可以在Canvas上绘制,Canvas可以保存为Bitmap,Bitmap本质上就是位图。 @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) private static Bitmap getBitmap(Context context, int drawableId) { 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"); } } @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); return bitmap; } 位图转PNG bitmap.compress(Bitmap.CompressFormat.PNG, 100, someOutStream) Android 的 SVG 库还有一个选项https://github.com/japgolly/svg-android
VectorDrawable可以在Canvas上绘制,Canvas可以保存为Bitmap,Bitmap本质上就是位图。
位图转PNG
Android 的 SVG 库还有一个选项https://github.com/japgolly/svg-android