0

アニメーションのベクタードローラーを使用して、クリックするとアニメーションを一時停止および逆転させる簡単な再生を作成しようとしています。何とかアニメーションが始まらない。AnimatedVectorDrawableアニメーションが開始されない

私のコードは以下の通りである:

MainActivity.java

public class MainActivity extends AppCompatActivity { 

private static final String TAG = "MainActivity"; 

private ImageView playPauseImgVw; 
private AnimatedVectorDrawable playToPauseAvDrwble; 
private AnimatedVectorDrawable pauseToPlayAvDrwble; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    playPauseImgVw = (ImageView) findViewById(R.id.activity_main_play_pause_btn_imgVw_id); 
    playToPauseAvDrwble = (AnimatedVectorDrawable) getDrawable(R.drawable.animated_vector_play_to_pause); 
    pauseToPlayAvDrwble = (AnimatedVectorDrawable) getDrawable(R.drawable.animated_vector_pause_to_play); 

    playPauseImgVw.setImageDrawable(playToPauseAvDrwble); 
    playPauseImgVw.setOnClickListener(newOnClickListener()); 
} 

private View.OnClickListener newOnClickListener() { 
    return new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      morph(); 
     } 
    }; 
} 

public void morph() { 

    Log.d(TAG, "start animation"); 
    playToPauseAvDrwble.start(); 
} 
} 

transition_pause_to_play.xml

<set xmlns:android="http://schemas.android.com/apk/res/android"> 
<objectAnimator android:duration="1000" 
    android:propertyName="pathData" 
    android:valueFrom="@string/path_pause_button" 
    android:valueTo="@string/path_play_button" 
    android:valueType="pathType" 
    android:interpolator="@android:interpolator/fast_out_slow_in" /> 

</set> 

transition_play_to_pause.xml

<set xmlns:android="http://schemas.android.com/apk/res/android"> 
<objectAnimator android:duration="1000" 
    android:propertyName="pathData" 
    android:valueFrom="@string/path_play_button" 
    android:valueTo="@string/path_pause_button" 
    android:valueType="pathType" 
    android:interpolator="@android:interpolator/fast_out_slow_in" /> 

</set> 

animated_vector_pause_to_play.xml

<animated-vector xmlns:android="http://schemas.android.com/apk/res/android" 
android:drawable="@drawable/vector_pause_button"> 
<target 
    android:animation="@animator/transition_pause_to_play" 
    android:name="path_pause_button" /> 
</animated-vector> 

animated_vector_play_to_pause.xml

<animated-vector xmlns:android="http://schemas.android.com/apk/res/android" 
android:drawable="@drawable/vector_play_button"> 
<target 
    android:animation="@animator/transition_play_to_pause" 
    android:name="path_play_button" /> 

vector_pause_button.xml

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

<path 
    android:name="path_name_pause_button" 
    android:fillAlpha="1" 
    android:fillColor="@color/color_pause_button" 
    android:pathData="@string/path_pause_button" 
    android:strokeColor="@color/color_stroke_pause_button" /> 

vector_play_button.xml

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

<path 
    android:name="path_name_play_button" 
    android:fillAlpha="1" 
    android:fillColor="@color/color_play_button" 
    android:pathData="@string/path_play_button" 
    android:strokeColor="@color/color_stroke_play_button" /> 

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:paddingBottom="@dimen/activity_vertical_margin" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
tools:context="com.merryapps.vectoranimationpoc.MainActivity"> 

<ImageView 
    android:id="@+id/activity_main_play_pause_btn_imgVw_id" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:clickable="true" 
    android:scaleType="fitCenter" 
    android:layout_centerHorizontal="true"/> 

のstrings.xml

<resources> 
<string name="app_name">VectorAnimationPoc</string> 
<string name="path_play_button">M300,70 l 0,-70 95,70 0,0 M300,70 l 95,0 -95,70 0,0 z</string> 
<string name="path_pause_button">M365,140 l 30,0 0,-140, -30,0 M300,140 l 30,0 0,-140 -30,0 z</string> 
</resources> 

私はここで何かが足りないのですか?

答えて

2

あなたが投稿したコードから、これは重要なタイプミスを除いてほとんど正しいと思われます。

AnimatedVectorDrawable <target>タグは "path_play_button"と "path_pause_button"という名前のターゲットを探していますが、VectorDrawableのパスは実際には "path_name_play_button"と "path_name_pause button"という名前です。

あなたは実際のpathDataを投稿していませんが、パスが互換性があると仮定して、これらの名前が正しく一致することを確認すると修正する必要があります。

+1

ありがとうございます。それは今働く。私はまた、パスで質問を更新しました。 Btw、私はあなたから、この素晴らしい[投稿](https://lewismcgeary.github.io/posts/animated-vector-drawable-pathMorphing/)から学んでいます。 :) –

+0

、優秀! –

関連する問題