AnimatedVectorDrawables pathData не работает?

На самом деле я пытался анимировать вектор, который можно нарисовать (отметьте, чтобы пересечь и наоборот), но все путевые данные для svgs, которые я нашел, не работают! даже когда я использую Android Vector Assets! Я не знаю в чем проблема? потому что я уверен, что правильно извлекать pathData, но та же проблема! Всегда не работает. Работают только два pathData:

отметка: M4,8,13,4 L9,17,6 M10,4,16,2 L19,6,7, кросс: M6,4,6,4 L17,6,17,6 M6,4,17,6 L17,6,6,4

Но я не знаю, откуда они! Я просто скопировал их из другого проекта! Найденные мной пути к данным или векторные пути к Android-ресурсам Android Studio не работают. Может кто-нибудь сказать мне, в чем проблема, и где я могу найти точные пути или как?

Проблема в том, что: при запуске приложения происходит сбой!

Сначала я создал свои векторы, используя этот код XML:

     <vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="120dp"
android:height="120dp"
android:viewportWidth="24"
android:viewportHeight="24">

    <group
        android:name="groupTickCross"
        android:pivotX="12"
        android:pivotY="12">

        <path
            android:name="tick"
            android:fillColor="@color/stroke_color"
            android:pathData="@string/path_tick"
            android:strokeWidth="2"
            android:strokeColor="@color/stroke_color"
            android:strokeLineCap="square"
            />

    </group>

</vector>

Один для галочки и один для креста, затем я создал файлы анимации: cross_to_tick.xml

 <objectAnimator
xmlns:android="http://schemas.android.com/apk/res/android"
android:propertyName="pathData"
android:valueFrom="@string/path_cross"
android:valueTo="@string/path_tick"
android:duration="450"
android:interpolator="@android:interpolator/fast_out_slow_in"
android:valueType="pathType" />

и tick_to_cross.xml

<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
    android:propertyName="pathData"
    android:valueFrom="@string/path_tick"
    android:valueTo="@string/path_cross"
    android:duration="450"
    android:interpolator="@android:interpolator/fast_out_slow_in"
    android:valueType="pathType" />

Анимация вращения в обоих случаях:

rotate_cross_to_tick.xml

<objectAnimator
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:propertyName="rotation"
    android:valueFrom="-180"
    android:valueTo="0"
    android:duration="450"
    android:interpolator="@android:interpolator/fast_out_slow_in" />

rotate_tick_to_cross.xml:

 <?xml version="1.0" encoding="utf-8"?>
<objectAnimator
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:propertyName="rotation"
    android:valueFrom="0"
    android:valueTo="180"
    android:duration="450"
    android:interpolator="@android:interpolator/fast_out_slow_in" />

Затем я создал файлы, которые будут связывать векторы с анимацией: avd_tick_to_cross.xml

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

    <target
        android:name="tick"
        android:animation="@animator/tick_to_cross" />


    <target
        android:name="groupTickCross"
        android:animation="@animator/rotate_tick_to_cross" />

</animated-vector>

и avd_cross_to_tick.xml

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

    <target
        android:name="cross"
        android:animation="@animator/cross_to_tick" />

    <target
        android:name="groupTickCross"
        android:animation="@animator/rotate_cross_to_tick" />

</animated-vector>

После этого я создал значения /tick_cross.xml

<resources>

    <!-- geometry -->
    <integer name="viewport_width">24</integer>
    <integer name="viewport_height">24</integer>
    <integer name="viewport_center_x">12</integer>
    <integer name="viewport_center_y">12</integer>
    <!--<string name="path_tick">M19.6,7 L10.4,16.2 M4.8,13.4 L9,17.6</string>-->
    <string name="path_tick">M4.8,13.4 L9,17.6 M10.4,16.2 L19.6,7</string>
    <!--<string name="path_cross">M17.6,6.4 L6.4,17.6 M6.4,6.4 L17.6,17.6</string>-->
    <string name="path_cross">M6.4,6.4 L17.6,17.6 M6.4,17.6 L17.6,6.4</string>
    <integer name="stroke_width">2</integer>

    <!-- names -->
    <string name="tick">tick</string>
    <string name="cross">cross</string>
    <string name="groupTickCross">groupTickCross</string>

    <!-- drawing -->
    <color name="stroke_color">#999</color>

    <!-- timing -->
    <integer name="duration">450</integer>

</resources>

И main_activity.xml

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:onClick="animate"
    tools:context="com.example.asma.animatedvectorsdrawables.MainActivity">

    <ImageView
        android:id="@+id/tick_cross"
        android:layout_centerInParent="true"
        android:src="@drawable/ic_tick"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</RelativeLayout>

Наконец, конечно, класс Java:

public class MainActivity extends Activity {

    private ImageView tickCross;
    private AnimatedVectorDrawable tickToCross;
    private AnimatedVectorDrawable crossToTick;
    private boolean tick = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tickCross = (ImageView) findViewById(R.id.tick_cross);

        tickToCross = (AnimatedVectorDrawable) getDrawable(R.drawable.avd_tick_to_cross);
        crossToTick = (AnimatedVectorDrawable) getDrawable(R.drawable.avd_cross_to_tick);

    }

    public void animate(View view) {
        //if it is a tick the animate from tick to cross else? animate from cross to tick
        AnimatedVectorDrawable drawable = tick ? tickToCross : crossToTick;
        tickCross.setImageDrawable(drawable);
        drawable.start();
        tick = !tick;
    }
}

Этот код работает отлично. Но когда я изменяю pathData на другой tick/cross pathData, извлеченный из другого файла svg, приложение внезапно останавливается во время его работы (только если я использую другой pathData, отличный от того, который показан в коде выше!)

Вот pathData тика и кросса, которые я использовал (из AndroidStudio VectorAssets)

Данные о тиках: M9,16.17L4.83,12l-1,42,1.41L9,19 21,7l-1,41,-1,41z

Перекрестный путьДанные: M19,6.41L17.59,5 12,10,59 6,41,5 5,6,41 10,59,12 5,17,59 6,41,19 12,13,41 17,59,19 19,17,59 13,41,12z

Я только изменяю pathData, но сохраняю весь остальной код как есть.

Спасибо

0 ответов

Другие вопросы по тегам