我正在尝试在我的小部件 bt中使用矢量drawable on pre-lollipop,图像不显示,因为我尝试以编程方式显示图像bcoz app:srcCompat don&# 39;基于下面给出的答案,在小部件的情况下工作:
AppCompat 23.2 use VectorDrawableCompat with RemoteViews (AppWidget) on API<21
但在使用上述方法后,我最终得到了这个堆栈跟踪:
02-11 18:55:12.686 12176-12176/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.android.mygarden, PID: 12176
java.lang.RuntimeException: Unable to start receiver com.example.android.mygarden.PlantWidgetProvider: android.content.res.Resources$NotFoundException: File res/drawable/water_drop_blue.xml from drawable resource ID #0x7f08008d
at android.app.ActivityThread.handleReceiver(ActivityThread.java:2580)
at android.app.ActivityThread.access$1700(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
at android.os.Handler.dispatchMessage(Handler.java:110)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5292)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.content.res.Resources$NotFoundException: File res/drawable/water_drop_blue.xml from drawable resource ID #0x7f08008d
at android.content.res.Resources.loadDrawable(Resources.java:2152)
at android.content.res.Resources.getDrawable(Resources.java:710)
at android.support.v4.content.ContextCompat.getDrawable(ContextCompat.java:353)
at com.example.android.mygarden.PlantWidgetProvider.updateAppWidget(PlantWidgetProvider.java:49)
at com.example.android.mygarden.PlantWidgetProvider.onUpdate(PlantWidgetProvider.java:76)
at android.appwidget.AppWidgetProvider.onReceive(AppWidgetProvider.java:66)
at android.app.ActivityThread.handleReceiver(ActivityThread.java:2573)
at android.app.ActivityThread.access$1700(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
at android.os.Handler.dispatchMessage(Handler.java:110)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5292)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640)
at dalvik.system.NativeStart.main(Native Method)
Caused by: org.xmlpull.v1.XmlPullParserException: Binary XML file line #1: invalid drawable tag vector
at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:933)
at android.graphics.drawable.Drawable.createFromXml(Drawable.java:877)
at android.content.res.Resources.loadDrawable(Resources.java:2148)
at android.content.res.Resources.getDrawable(Resources.java:710)
at android.support.v4.content.ContextCompat.getDrawable(ContextCompat.java:353)
at com.example.android.mygarden.PlantWidgetProvider.updateAppWidget(PlantWidgetProvider.java:49)
at com.example.android.mygarden.PlantWidgetProvider.onUpdate(PlantWidgetProvider.java:76)
at android.appwidget.AppWidgetProvider.onReceive(AppWidgetProvider.java:66)
at android.app.ActivityThread.handleReceiver(ActivityThread.java:2573)
at android.app.ActivityThread.access$1700(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
at android.os.Handler.dispatchMessage(Handler.java:110)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5292)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640)
at dalvik.system.NativeStart.main(Native Method)
我确实在 drawable / water_drop_blue.xml 中有我的VectorDrawable,但是它显示了资源$ NotFoundException 。 这是我的 updateAppWidget 方法:
static void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
int appWidgetId) {
// Create an Intent to launch MainActivity when clicked
Intent intent = new Intent(context, MainActivity.class);
PendingIntent pendingIntent = PendingIntent
.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
// Construct the RemoteViews object
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.plant_widget);
if (VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP) {
views.setImageViewResource(R.id.widget_water_button, R.drawable.water_drop_blue);
} else {
Drawable drawable = ContextCompat.getDrawable(context, R.drawable.water_drop_blue);
drawable = (DrawableCompat.wrap(drawable)).mutate();
Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(),
drawable.getIntrinsicHeight(),
Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(bitmap);
drawable.setBounds(0, 0, c.getWidth(), c.getHeight());
drawable.draw(c);
views.setImageViewBitmap(R.id.widget_water_button, bitmap);
}
我的应用小部件布局文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="@+id/widget_water_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal" />
<ImageView
android:id="@+id/widget_plant_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="@drawable/grass" />
</LinearLayout>
我还包含在app level build.gradle文件中
vectorDrawables.useSupportLibrary = true
请帮我在前棒棒糖设备上显示小部件中的Vector。我也尝试使用 AppCompatImageView 但它没有用。
答案 0 :(得分:1)
在我的项目中它是这样的:
remoteViews.setImageViewBitmap(R.id.widget_control, VectorUtil
.vectorToBitmap(context, R.drawable.chevron_left));
VectorUtils:
public static Bitmap vectorToBitmap(Context context, @DrawableRes int resVector) {
Drawable drawable = AppCompatResources.getDrawable(ctx, resVector);
Bitmap b = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(),
Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
drawable.setBounds(0, 0, c.getWidth(), c.getHeight());
drawable.draw(c);
return b;
}
它运作正常。
但正如我在你的堆栈跟踪中看到的那样,你的向量存在问题,因为Tag的矢量分析器无法读取它。验证你的载体。