1、程序结构
2、MainActivity.java程序
package com.example.objectanimator1; import java.util.ArrayList; import java.util.List; import android.animation.ObjectAnimator; import android.animation.TimeInterpolator; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.view.animation.AccelerateInterpolator; import android.view.animation.BounceInterpolator; import android.widget.ImageView; import android.widget.Toast; public class MainActivity extends Activity implements OnClickListener { private int[] ivId = { R.id.iv_a, R.id.iv_b, R.id.iv_c, R.id.iv_d, R.id.iv_e, R.id.iv_f, R.id.iv_g, R.id.iv_h }; private List<ImageView> imageInfos = new ArrayList<ImageView>(); private Boolean flag = true; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); for (int i = 0; i < ivId.length; i++) { ImageView iv = (ImageView) findViewById(ivId[i]); imageInfos.add(iv); iv.setOnClickListener(this); } } @Override public void onClick(View v) { // TODO Auto-generated method stub switch (v.getId()) { case R.id.iv_a: if (flag) { startAnima(); flag = false; } else { closeAnima(); flag = true; } break; default: Toast.makeText(MainActivity.this, "clicked:"+ v.getId() , 0).show(); break; } } private void startAnima() { // TODO Auto-generated method stub for (int i = 0; i < ivId.length; i++) { ObjectAnimator oa = ObjectAnimator.ofFloat(imageInfos.get(i), "translationY", 0, i * 100); oa.setInterpolator(new BounceInterpolator()); oa.setDuration(1000); oa.setStartDelay(300); oa.start(); } } private void closeAnima() { // TODO Auto-generated method stub for (int i = 0; i < ivId.length; i++) { ObjectAnimator oa = ObjectAnimator.ofFloat(imageInfos.get(i), "translationY", i * 100, 0); oa.setInterpolator(new AccelerateInterpolator()); oa.setDuration(1000); oa.setStartDelay(300); oa.start(); } } }
2、activity_main.xml的代码
<FrameLayout 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.example.objectanimator1.MainActivity" > <ImageView android:id="@+id/iv_a" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="20dp" android:src="@drawable/a" /> <ImageView android:id="@+id/iv_b" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/b" /> <ImageView android:id="@+id/iv_c" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/c" /> <ImageView android:id="@+id/iv_d" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/d" /> <ImageView android:id="@+id/iv_e" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/e" /> <ImageView android:id="@+id/iv_f" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/f" /> <ImageView android:id="@+id/iv_g" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/g" /> <ImageView android:id="@+id/iv_h" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/h" /> </FrameLayout>