0
我想動畫繪製矢量 switch_circle.xmlClassCastException異常VectorDrawable不能轉換爲可動畫
<?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>
filling.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)
你需要'AnimatedVectorDrawable'調用'開始()'方法,而不是'VectorDrawable' – pskink
@pslink AnimatedVectorDrawableCompat drawableCompat = AnimatedVectorDrawableCompat.create (getActivity(),R.drawable.switch_circle_animated_vector); drawableCompat.start(); 它沒有例外,但沒有動畫 – Plorial
,因爲它沒有附加到您的視圖 – pskink