Interpolators插值器:可以定义动画变换速率
LinearInterpolator:恒定变化的插值器
MainAc
import android.animation.AnimatorSet; import android.animation.ObjectAnimator; import android.os.Bundle; import android.os.PersistableBundle; import android.support.annotation.Nullable; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.view.animation.Animation; import android.view.animation.BounceInterpolator; import android.view.animation.LinearInterpolator; import android.widget.ImageView; import android.widget.Toast; import java.util.ArrayList; import java.util.List; /** * Created by YRC on 2017/10/27. */ public class anim extends AppCompatActivity implements View.OnClickListener{ private List<ImageView> imageViewList=new ArrayList<>(); private int[] mIv={R.id.imageView_a,R.id.imageView_b,R.id.imageView_c,R.id.imageView_d,R.id.imageView_e}; private boolean mFlag=true; @Override public void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.anim); for (int i=0;i<5;i++){ ImageView iv= (ImageView) findViewById(mIv[i]); iv.setOnClickListener(this); imageViewList.add(iv); } } @Override public void onClick(View v) { switch (v.getId()){ case R.id.imageView_a: if (mFlag){ startAnim(); }else { closeAnim(); } break; default: closeAnim(); Toast.makeText(anim.this,""+v.getId(),Toast.LENGTH_SHORT).show(); break; } } private void closeAnim() { ObjectAnimator objectAnimator1=ObjectAnimator.ofFloat(imageViewList.get(0),"Alpha",0.5F,1F); ObjectAnimator objectAnimator2=ObjectAnimator.ofFloat(imageViewList.get(1),"translationX",200F,0); ObjectAnimator objectAnimator3=ObjectAnimator.ofFloat(imageViewList.get(2),"translationY",200F,0); ObjectAnimator objectAnimator4=ObjectAnimator.ofFloat(imageViewList.get(3),"translationX",-200F,0); ObjectAnimator objectAnimator5=ObjectAnimator.ofFloat(imageViewList.get(4),"translationY",-200F,0); AnimatorSet animatorSet=new AnimatorSet(); animatorSet.setDuration(500); animatorSet.setInterpolator(new LinearInterpolator()); animatorSet.playTogether(objectAnimator1,objectAnimator2,objectAnimator3,objectAnimator4,objectAnimator5); animatorSet.start(); mFlag=true; } private void startAnim() { ObjectAnimator objectAnimator1=ObjectAnimator.ofFloat(imageViewList.get(0),"Alpha",1F,0.5F); ObjectAnimator objectAnimator2=ObjectAnimator.ofFloat(imageViewList.get(1),"translationX",200F); ObjectAnimator objectAnimator3=ObjectAnimator.ofFloat(imageViewList.get(2),"translationY",200F); ObjectAnimator objectAnimator4=ObjectAnimator.ofFloat(imageViewList.get(3),"translationX",-200F); ObjectAnimator objectAnimator5=ObjectAnimator.ofFloat(imageViewList.get(4),"translationY",-200F); AnimatorSet animatorSet=new AnimatorSet(); animatorSet.setDuration(500); //LinearInterpolator:恒定变化的插值器 animatorSet.setInterpolator(new LinearInterpolator()); animatorSet.playTogether(objectAnimator1,objectAnimator2,objectAnimator3,objectAnimator4,objectAnimator5); animatorSet.start(); mFlag=false; } }
xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/imageView_b" android:src="@drawable/b" android:layout_centerVertical="true" android:layout_centerHorizontal="true" /> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/imageView_c" android:src="@drawable/c" android:layout_centerVertical="true" android:layout_centerHorizontal="true" /> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/imageView_d" android:src="@drawable/d" android:layout_centerVertical="true" android:layout_centerHorizontal="true" /> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/imageView_e" android:src="@drawable/e" android:layout_centerVertical="true" android:layout_centerHorizontal="true" /> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/imageView_a" android:src="@drawable/a" android:layout_centerVertical="true" android:layout_centerHorizontal="true" /> </RelativeLayout>