2016-11-05 11 views
3

expandable recycler viewが必要です。私は、折りたたみと矢印のアニメーションを展開するためのアニメーション化されたベクトルdrawableを使用する予定です。矢印のクリックでアニメーション化されたベクトル描画可能

私は回転を開始することができるsetExpandCollapseListenerがあります。 Animated Vector Drawableを使って、以下に示すような効果を得るには?崩壊のボタンの

<?xml version="1.0" encoding="utf-8"?> 
<vector xmlns:android="http://schemas.android.com/apk/res/android" 
android:viewportWidth="306" 
android:viewportHeight="306" 
android:width="306dp" 
android:height="306dp"> 
<path 
    android:pathData="M153 247.35L306 94.35 270.3 58.65 153 175.95 35.7 58.65 0 94.35Z" 
    android:fillColor="#000000" /> 
</vector> 

ベクトル描画可能:

ベクトル描画可能なボタンを広げるため

<?xml version="1.0" encoding="utf-8"?> 
<vector xmlns:android="http://schemas.android.com/apk/res/android" 
android:viewportWidth="306" 
android:viewportHeight="306" 
android:width="306dp" 
android:height="306dp"> 
<path 
    android:pathData="M270.3 247.35L306 211.65 153 58.65 0 211.65 35.7 247.35 153 130.05Z" 
    android:fillColor="#000000" /> 
</vector> 

enter image description here

答えて

3

1.ベクトル画像を追加

我々はまた、する必要がラップgroupは、アニメーション化されるベクトルリソースの一部であるため、はgroupにあります。グループには任意の名前を設定できます。

res/drawable/ic_arrow_down.xml

<?xml version="1.0" encoding="utf-8"?> 
<vector xmlns:android="http://schemas.android.com/apk/res/android" 
    android:viewportWidth="306" 
    android:viewportHeight="306" 
    android:width="306dp" 
    android:height="306dp"> 

    <group 
    android:name="arrow" 
    android:pivotX="153" 
    android:pivotY="153"> 
     <path 
      android:pathData="M153 247.35L306 94.35 270.3 58.65 153 175.95 35.7 58.65 0 94.35Z" 
      android:fillColor="#000000" /> 
    </group> 
</vector> 

res/drawable/ic_arrow_top.xml

<?xml version="1.0" encoding="utf-8"?> 
<vector xmlns:android="http://schemas.android.com/apk/res/android" 
    android:viewportWidth="306" 
    android:viewportHeight="306" 
    android:width="306dp" 
    android:height="306dp"> 

    <group 
    android:name="arrow" 
    android:pivotX="153" 
    android:pivotY="153"> 
     <path 
      android:pathData="M270.3 247.35L306 211.65 153 58.65 0 211.65 35.7 247.35 153 130.05Z" 
      android:fillColor="#000000" /> 
    </group> 
</vector> 

2.追加アニメーター

res/animator/animation_arrow_rotation.xml

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

3.アニメーションのベクトルを追加しgroupの名前に

<target>参照をドローアブル。

res/drawable/ic_animated_arrow_down.xml

<?xml version="1.0" encoding="utf-8"?> 
<animated-vector xmlns:android="http://schemas.android.com/apk/res/android" 
    android:drawable="@drawable/ic_arrow_down"> 

    <target 
     android:name="arrow" 
     android:animation="@animator/animation_arrow_rotation" /> 
</animated-vector> 

res/drawable/ic_animated_arrow_up.xml

<?xml version="1.0" encoding="utf-8"?> 
<animated-vector xmlns:android="http://schemas.android.com/apk/res/android" 
    android:drawable="@drawable/ic_arrow_up"> 

    <target 
     android:name="arrow" 
     android:animation="@animator/animation_arrow_rotation" /> 
</animated-vector> 

4.アダプターにいくつかのコードを追加します。

ParentViewHolderの相続で

のInit ImageViewの:

void bind(UiModel uiModel) { 
    arrowImageView.setImageResource(isExpanded() ? R.drawable.ic_animated_arrow_down : 
      R.drawable.ic_animated_arrow_up); 

    // some other code 
} 

と上書きonExpansionToggled:ところで

@Override 
public void onExpansionToggled(boolean expanded) { 
    super.onExpansionToggled(expanded); 

    arrowImageView.setImageResource(expanded ? R.drawable.ic_animated_arrow_down : 
      R.drawable.ic_animated_arrow_up); 
    AnimatedVectorDrawableCompat drawable = (AnimatedVectorDrawableCompat) arrowImageView.getDrawable(); 

    drawable.registerAnimationCallback(new Animatable2Compat.AnimationCallback() { 
     @Override 
     public void onAnimationEnd(Drawable ignored) { 
      drawable.clearAnimationCallbacks(); 

      arrowImageView.setImageResource(expanded ? R.drawable.ic_animated_arrow_up : 
        R.drawable.ic_animated_arrow_down); 
     } 
    }); 

    drawable.start(); 
} 

、あなたは1つのXMLファイルにAnimatedVectorDrawable XMLリソースを定義することができます。詳細here

1

必要なのは、コードのちょうど1行である -

view.animate().rotationBy(degree_of_rotation).setDuration(duration im milliseconds).start(); 
関連する問題