Java ClassCastException VectorDrawable无法强制转换为可设置动画
我想动画向量绘制 switch_circle.xmlJava ClassCastException VectorDrawable无法强制转换为可设置动画,java,android,xml,vector,android-animation,Java,Android,Xml,Vector,Android Animation,我想动画向量绘制 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"> <pa
<?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>
切换\u circle\u动画\u 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>
要防止类强制转换异常,请始终按如下方式调用
start()
方法:
if (drawable instanceof Animatable)
{
((Animatable) drawable).start();
}
您需要
AnimatedVectorDrawable
来调用start()
方法,而不是VectorDrawable
@pslink AnimatedVectorDrawableCompat-drawableCompat=AnimatedVectorDrawableCompat.create(getActivity(),R.drawable.switch\u circle\u animated\u vector);drawableCompat.start();它可以毫无例外地工作,但不起作用,因为它没有连接到您的view@pskink使用这个switchCircle.setImageDrawable(drawableCompat.getCurrent());drawableCompat.start();它是有效的。谢谢)drawableCompat.getCurrent()?这是什么?这将消除异常,但不会解决他的问题。您找到问题的解决方案了吗?
<?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)
if (drawable instanceof Animatable)
{
((Animatable) drawable).start();
}