VectorDrawable到位图:空对象引用上的Bitmap.setHasAlpha(boolean)' [英] VectorDrawable to Bitmap: Bitmap.setHasAlpha(boolean)' on a null object reference
问题描述
我正在尝试从VectorDrawable
(xml图像)获取Bitmap
:
I'm trying to get Bitmap
from VectorDrawable
(xml image):
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
错误
But app crashes on Bitmap.createBitmap
method with Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.graphics.Bitmap.setHasAlpha(boolean)' on a null object reference
error
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 drawable(逗号符号)
here some simple xml drawable (comma symbol)
<?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
方法上失败
I tried every method from Getting Bitmap from vector drawable
All of them fails on this .createBitmap
method
似乎是
android:width="16000dp"
android:height="16000dp"
android:viewportWidth="16000"
android:viewportHeight="16000">
具有重要价值...
我没有添加完整的xml,还有更多的path
标记,因此它不仅仅是具有如此大值的逗号符号:)
I've added not full xml, there are more path
tags, so it's not just comma symbol with such big values:)
我试图将值减小到2000(例如),并且应用程序停止崩溃,但它毁了我的图片
I tried to reduce values to 2000 (e.g.) and app stopped crashing, but it ruined my image
我使用 http://inloop.github.io/svg2android/
推荐答案
更改
android:width="16000dp"
android:height="16000dp"
到
android:width="24dp"
android:height="24dp"
不需要加油:
android:viewportWidth="16000"
android:viewportHeight="16000">
这篇关于VectorDrawable到位图:空对象引用上的Bitmap.setHasAlpha(boolean)'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!