gpt4 book ai didi

java - ClassCastException VectorDrawable 无法转换为 Animatable

转载 作者:行者123 更新时间:2023-11-30 01:21:57 24 4
gpt4 key购买 nike

我想为 vector 图绘制动画switch_circle.xml

<?xml version="1.0" encoding="utf-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:viewportWidth="40"
android:viewportHeight="40"
android:width="22dp"
android:height="22dp">
<path
android:name="switch_circle"
android:pathData="M5 20A15 15 0 1 0 35 20A15 15 0 1 0 5 20M10 20A10 10 0 1 1 30 20A10 10 0 1 1 10 20"
android:fillColor="#ffffff" />
</vector>

当我启动动画时,它抛出类转换异常。所有教程都说它必须正常工作,但事实并非如此。我不知道为什么。

   AppCompatImageButton switchCircle = (AppCompatImageButton)    view.findViewById(R.id.switchButtonCircle);
Drawable drawable = switchCircle.getDrawable();
((Animatable) drawable).start();

switch_circle_animated_vector.xml

<animated-vector xmlns:android="http://schemas.android.com/apk/res/android"

android:drawable="@drawable/switch_circle" >
<target
android:name="switch_circle"
android:animation="@anim/filling" />

</animated-vector>

填充.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<objectAnimator
android:duration="500"
android:propertyName="pathData"
android:valueFrom="M5 20A15 15 0 1 0 35 20A15 15 0 1 0 5 20M10 20A10 10 0 1 1 30 20A10 10 0 1 1 10 20"
android:valueTo="M5 20A15 15 0 1 0 35 20A15 15 0 1 0 5 20M0 20A0 0 0 1 1 30 20A0 0 0 1 1 0 20"
android:valueType="pathType" />
</set>

图片按钮

<android.support.v7.widget.AppCompatImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/switchButtonCircle"
android:background="#0025272a"
app:srcCompat="@drawable/switch_circle"
android:layout_gravity="center_vertical"
android:layout_centerVertical="true"
android:layout_alignRight="@+id/switchButton"
android:layout_alignEnd="@+id/switchButton"
android:layout_marginRight="12dp"
android:layout_marginEnd="12dp" />

堆栈跟踪

java.lang.ClassCastException: android.graphics.drawable.VectorDrawable cannot be cast to android.graphics.drawable.Animatable
at com.plorial.telegramcamera.CameraPreviewFragment$1.onClick(CameraPreviewFragment.java:65)
at android.view.View.performClick(View.java:5198)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

最佳答案

为了防止类强制转换异常总是像这样调用start()方法:

if (drawable instanceof Animatable)
{
((Animatable) drawable).start();
}

关于java - ClassCastException VectorDrawable 无法转换为 Animatable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36955756/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com