9

I animierte Vektoren von Support Library 23.2.0, wie dies bin mit:AnimatedVectorDrawable in Support Bibliothek und Animation von "PathData"

compile 'com.android.support:support-vector-drawable:23.2.0' 
compile 'com.android.support:animated-vector-drawable:23.2.0' 

ich versuche zu animieren "PathData" (Linien zu einander morphend). Mein Code sieht so aus.

ziehbar/ic_done.xml:

<?xml version="1.0" encoding="utf-8"?> 
<vector xmlns:android="http://schemas.android.com/apk/res/android" 
    android:width="24dp" 
    android:height="24dp" 
    android:viewportHeight="24.0" 
    android:viewportWidth="24.0"> 
    <path 
     android:name="tick" 
     android:pathData="M4.8,12L9,16.2L20,8" 
     android:strokeColor="#FF000000" /> 
</vector> 

ziehbar/ic_done_animated.xml:

<?xml version="1.0" encoding="utf-8"?> 
<animated-vector xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:drawable="@drawable/ic_done"> 
    <target 
     android:name="tick" 
     android:animation="@animator/tick_path_animation" /> 
</animated-vector> 

Animator/tick_path_animation.xml:

<?xml version="1.0" encoding="utf-8"?> 
<set xmlns:android="http://schemas.android.com/apk/res/android" 
    android:ordering="sequentially"> 
    <objectAnimator 
     android:duration="200" 
     android:propertyName="pathData" 
     android:valueFrom="M4.8,12L4.8,12L4.8,12" 
     android:valueTo="M4.8,12L9,16.2L9,16.2" 
     android:valueType="pathType" /> 
    <objectAnimator 
     android:duration="200" 
     android:propertyName="pathData" 
     android:valueFrom="M4.8,12L9,16.2L9,16.2" 
     android:valueTo="M4.8,12L9,16.2L20,8" 
     android:valueType="pathType" /> 
</set> 

Java-Code:

ImageView vImgAnimated = findByViewId(R.id.img); 
AnimatedVectorDrawableCompat animatedVector = AnimatedVectorDrawableCompat.create(getContext(), R.drawable.ic_done_animated); 
vImgAnimated.setImageDrawable(animatedVector); 
animatedVector.start(); 

Es funktioniert gut auf neueres Gerät mit API-Ebene 21 aber ich habe ein Problem auf dem Gerät mit API-Ebene 16:

java.lang.NumberFormatException: Invalid int: "M4.8,12L4.8,12L4.8,12" 
    at java.lang.Integer.invalidInt(Integer.java:138) 
    at java.lang.Integer.parse(Integer.java:375) 
    at java.lang.Integer.parseInt(Integer.java:366) 
    at com.android.internal.util.XmlUtils.convertValueToInt(XmlUtils.java:123) 
    at android.content.res.TypedArray.getInt(TypedArray.java:254) 
    at android.animation.AnimatorInflater.loadAnimator(AnimatorInflater.java:258) 
    at android.animation.AnimatorInflater.loadObjectAnimator(AnimatorInflater.java:161) 
    at android.animation.AnimatorInflater.createAnimatorFromXml(AnimatorInflater.java:117) 
    at android.animation.AnimatorInflater.createAnimatorFromXml(AnimatorInflater.java:126) 
    at android.animation.AnimatorInflater.createAnimatorFromXml(AnimatorInflater.java:93) 
    at android.animation.AnimatorInflater.loadAnimator(AnimatorInflater.java:72) 
    at android.support.graphics.drawable.AnimatedVectorDrawableCompat.inflate(AnimatedVectorDrawableCompat.java:377) 
    at android.support.graphics.drawable.AnimatedVectorDrawableCompat.createFromXmlInner(AnimatedVectorDrawableCompat.java:162) 
    at android.support.graphics.drawable.AnimatedVectorDrawableCompat.create(AnimatedVectorDrawableCompat.java:142) 

Laut einem Artikel android-support-library-232 der animierten Vektoren (AnimatedVectorDrawableCompat) unterstützt werden soll zurück zu API-Ebene 11.

Es sieht aus wie es während versagtanfangswert Attribut aus tick_path_animation.xml lesen. Dieser Attributtyp "pathType" wird wahrscheinlich (noch?) Nicht unterstützt. Irgendeine Idee, wie man das löst?

+0

versuchen, Vector-Compat von wnafee https://github.com/wnafee/vector-compat zu verwenden, habe ich Path-Morphing getestet, funktioniert gut –

+1

Ihre Frage ist in Support-Bibliothek 25.4.0 gelöst: https: // Entwickler. android.com/de/topic/libraries/support-library/revisions.html#25-4-0 – Frank

+0

Für Path Morphing auf Plattformen Tarek360

Antwort

23

Entschuldigung, das wird nicht mit der aktuellen Version der Support Library (23.2.0) funktionieren.

Siehe Chris Banes article.

Darüber hinaus gibt es einige Einschränkungen, welche Art von Dingen animiert Vektoren tun können, wenn sie auf Plattformen laufen < API 21. Im Folgenden sind die Dinge, die auf diesen Plattformen derzeit nicht arbeiten:

Weg Morphing (PathType Evaluator). Dies wird verwendet, um einen Pfad in einen anderen Pfad umzuwandeln.

Pfadinterpolation. Dies wird verwendet, um einen flexiblen Interpolator (dargestellt als ein Pfad) anstelle der systemdefinierten wie LinearInterpolator zu definieren.

Weg entlang gehen. Dies wird selten verwendet. Das Geometrieobjekt kann sich entlang eines beliebigen Pfads bewegen.

So animiert das animieren der pathData oder "Path Morphing" wird derzeit nicht unterstützt.

Update:
Frank Kommentar:

Dies wird schließlich zur Unterstützung lib 25.4.0 (Juni 2017) festgelegt: "Path Morphing und Bahninterpolation werden unterstützt in AnimatedVectorDrawableCompat"

+0

Vielen Dank, ich konnte diese Einschränkungen nicht finden. –

+0

@Lewis McGeary: finden Sie Informationen darüber, welche Version den Pfad unterstützt –

+7

funktioniert immer noch nicht in 24.2.0. Es macht irgendwie den ganzen Compat-Teil der Support-Bibliothek nutzlos für mich! Ziemlich nervig nach der Angabe der Kompatibilität zurück zu API11, aber die 'relevanten' Details verbergen was nicht funktioniert. Sollte AnimatedVectorDrawablePartiallyCompat aufgerufen werden .. –

4

API 16 animation
der kreisförmige "Flash" in der obigen Animation (in der Mitte des Bildes) ist mir vor Den Bildschirm starten, um den Morph zu starten.

  • Aufruf statisch getDrawable() Methoden:
//This will only inflate a drawable with <vector> as the root element 
VectorDrawable.getDrawable(context, R.drawable.ic_arrow_vector); 

//This will only inflate a drawable with <animated-vector> as the root element 
AnimatedVectorDrawable.getDrawable(context, R.drawable.ic_arrow_to_menu_animated_vector); 

// This will inflate any drawable and will auto-fallback to the lollipop implementation on api 21+ devices 
ResourcesCompat.getDrawable(context, R.drawable.any_drawable);

Inflating Drawable des

`VectorDrawable` und `AnimatedVectorDrawable` in diesem Support-Bibliothek (`Vektor-compat`) können auf diese Art und Weise aufgeblasen werden

Wenn Sie Drawable in Java-Code aufblasen, wird empfohlen, immerzu verwendenwie dies Lollipop Fallback behandelt, wenn anwendbar. Dadurch kann das System Drawable ConstantState zwischenspeichern und ist daher effizienter.
Die Bibliothek (`Vektor-compat`) die folgende Morph (bi-direktional) Animationen:

  • Wiedergabe-Pause Morph-Animation
  • Wiedergabe-Stop Morph Animation
  • Pfeil-Hamburger-Menü Morph Animation

  • Wie Sie sehen können, produzierte ich das obige Bild auf meinem API 16 Telefon:

    import com.wnafee.vector.compat.AnimatedVectorDrawable; 
    mdrawable = (AnimatedVectorDrawable) AnimatedVectorDrawable.getDrawable(this.getApplicationContext(), R.drawable.consolidated_animated_vector); 
    
    README 210

    Blick auf die GitHub für vector-compat hier: https://github.com/wnafee/vector-compat
    Dies Ihr Problem beheben (bis API 14), wenn Sie es mit Ihrer App Moduls build.gradledependencies (in der Regel am Ende der Datei) fusionieren:

    dependencies { 
    compile fileTree(dir: 'libs', include: ['*.jar']) 
    //Trying to FIX Binary XML file line #2: invalid drawable tag animated-vector 
        compile 'com.android.support:appcompat-v7:25.0.0' 
        compile 'com.android.support:design:25.0.0' 
    //not needed 
    // compile 'com.android.support:support-vector-drawable:25.0.0' 
        compile 'com.wnafee:vector-compat:1.0.5'//*******holy grail *******https://github.com/wnafee/vector-compat 
    // Failed to resolve: com.android.support:support-animated-vector-drawable:25.0.0 
    //not needed 
    // compile 'com.android.support:support-animated-vector-drawable:25.0.0' 
    } 
    
    Verwandte Themen