activity_main3.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.myapplication.Main3Activity"> <Button android:onClick="start5" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="开始"/> <LinearLayout android:layout_gravity="center" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_margin="10dp" android:orientation="horizontal"> <ImageView android:id="@+id/iv_1" android:layout_width="120dp" android:layout_height="150dp" android:background="@mipmap/n"/> <ImageView android:id="@+id/iv_2" android:layout_width="120dp" android:layout_height="150dp" android:background="@mipmap/n"/> <ImageView android:id="@+id/iv_3" android:layout_width="120dp" android:layout_height="150dp" android:background="@mipmap/n"/> </LinearLayout> </LinearLayout>
Main3Activity
package com.example.myapplication; import android.animation.AnimatorSet; import android.animation.ObjectAnimator; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.ImageView; public class Main3Activity extends AppCompatActivity { private ImageView iv_1; private ImageView iv_2; private ImageView iv_3; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main3); initView(); } private void initView() { //找到ID iv_1 = findViewById(R.id.iv_1); iv_2 = findViewById(R.id.iv_2); iv_3 = findViewById(R.id.iv_3); } /** * 开始 * @param view */ public void start1(View view) { ObjectAnimator iv1=ObjectAnimator.ofFloat(iv_1,View.TRANSLATION_Y,0F,400F); ObjectAnimator iv2=ObjectAnimator.ofFloat(iv_2,View.TRANSLATION_Y,0F,400F); ObjectAnimator iv3=ObjectAnimator.ofFloat(iv_3,View.TRANSLATION_Y,0F,400F); //该动画用于添加联合动画 AnimatorSet animatorSet = new AnimatorSet(); animatorSet.play(iv1) .before(iv2) .after(iv3); animatorSet.setDuration(3000).start(); } /** * 三个组件一起执行 */ public void start5(View view){ ObjectAnimator iv1=ObjectAnimator.ofFloat(iv_1,View.TRANSLATION_Y,0F,400F); ObjectAnimator iv2=ObjectAnimator.ofFloat(iv_2,View.TRANSLATION_Y,0F,400F); ObjectAnimator iv3=ObjectAnimator.ofFloat(iv_3,View.TRANSLATION_Y,0F,400F); //该对象用于添加联合动画 AnimatorSet animatorSet=new AnimatorSet(); //animatorSet.playTogether(iv1,iv2,iv3);//三个组件一起执行 animatorSet.playSequentially(iv1,iv2,iv3);//一个接着一个执行 animatorSet.setDuration(3000).start(); } }