我正在尝试从 VectorDrawable
(xml image(获取Bitmap
:
VectorDrawable vectorDrawable = (VectorDrawable) ContextCompat.getDrawable(mContext, R.drawable.test);
Bitmap bitmap = UtilMethods.getBitmapFromVector(vectorDrawable);
但是,应用程序在Bitmap.createBitmap
方法上崩溃,使用Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.graphics.Bitmap.setHasAlpha(boolean)' on a null object reference
错误
public static Bitmap getBitmapFromVector(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;
}
P.S。
compile 'com.android.support:appcompat-v7:25.3.1'
compile 'com.android.support:support-v4:25.3.1'
在这里一些简单的XML可绘制(逗号符号(
<?xml version="1.0" encoding="utf-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="16000dp"
android:height="16000dp"
android:viewportWidth="16000"
android:viewportHeight="16000">
<path
android:fillColor="#040607"
android:strokeWidth="1"
android:pathData="M3637 11910 l486 -730 484 0 c265 0 483 3 483 8 0 4 -130 331 -288 727 l-288 720
-682 3 -682 2 487 -730z" />
</vector>
我尝试了从绘制矢量获取位图的所有方法所有这些都失败了此.createBitmap
方法
似乎是
android:width="16000dp"
android:height="16000dp"
android:viewportWidth="16000"
android:viewportHeight="16000">
是大价值...
我没有添加完整的XML,还有更多的path
标签,因此它不仅是具有如此大值的逗号符号:(
我试图将值降低到2000(例如(,并且应用程序停止崩溃,但它破坏了我的图像
我可以使用http://inloop.github.io/svg2android/
您正在尝试分配一个16000x16000dp位图,该位图基于您运行的应用程序的显示密度,必须乘以xhdpi的2倍,为xxhdpi和34对于xxxhdpi。这会导致内存问题,因为您试图分配大于1000MB的位图。
您需要缩小可绘制的缩小。它不应导致您正在遇到的质量损失,所以我建议您检查您的SVG或SVG2Android的输出是否错误...
还请记住,您不能将比gl_max_texture_size更大或更高的位图放入ImageView中。请参阅:&quot'位图太大而无法上传到纹理中&quot'
更改
android:width="16000dp"
android:height="16000dp"
to
android:width="24dp"
android:height="24dp"
不需要chagne:
android:viewportWidth="16000"
android:viewportHeight="16000">