2016-04-30 9 views
0

I-Vektor ziehbar switch_circle.xmlClasscast VectorDrawable kann nicht auf Anima gegossen werden

<?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> 

Wenn ich starten Animation animieren wollen, es wirft Klasse Guss Ausnahme. Alle Tutorial sagt, dass es richtig funktionieren muss, aber es tut es nicht. Ich weiß nicht warum.

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> 

Bildknopf

<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" /> 

Stacktrace

+0

Sie müssen '' AnimatedVectorDrawable' beginnen zu nennen() 'Methode, nicht' VectorDrawable' – pskink

+0

@pslink AnimatedVectorDrawableCompat drawableCompat = AnimatedVectorDrawableCompat.create (getActivity(), R.drawable.switch_circle_animated_vector); drawableCompat.start(); Es funktioniert ohne Ausnahmen, aber – Plorial

+0

wird nicht animiert, weil es nicht an Ihre Ansicht angeschlossen ist – pskink

Antwort

0

Um zu verhindern, class cast exception immer rufen Sie die start() Methode wie folgt:

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