代码量极少抛弃使用animation,代码量减少60%,效率更高。
MainActivity
package test.ban.com.test; import android.animation.ObjectAnimator; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.animation.BounceInterpolator; import android.widget.ImageView; import android.widget.Toast; import java.util.ArrayList; import java.util.List; public class MainActivity extends Activity implements View.OnClickListener { private int[] res = {R.id.iva, R.id.ivb, R.id.ivc, R.id.ivd, R.id.ive, R.id.ivf, R.id.ivh, R.id.ivg}; private List<ImageView> imageViewList = new ArrayList<ImageView>(); private boolean flag = true;//判断是否已经打开菜单 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_my); for (int i = 0; i < res.length; i++) { ImageView imageView = (ImageView) findViewById(res[i]); imageView.setOnClickListener(this); imageViewList.add(imageView); } } @Override public void onClick(View v) { switch (v.getId()) { case R.id.iva: if (flag) { startAnim(); } else { closeAnim(); } break; default: Toast.makeText(this, "click" + v.getId(), Toast.LENGTH_SHORT).show(); break; } } //关闭菜单 private void closeAnim() { for (int i = 1; i < res.length; i++) { ObjectAnimator animator = ObjectAnimator.ofFloat(imageViewList.get(i), "translationY", i * 200, 0F); animator.setDuration(500); animator.setInterpolator(new BounceInterpolator()); animator.setStartDelay(i * 300); animator.start(); flag = true; } } //打开菜单 private void startAnim() { for (int i = 1; i < res.length; i++) { ObjectAnimator animator = ObjectAnimator.ofFloat(imageViewList.get(i), "translationY", 0F, i * 200); animator.setDuration(500); animator.setInterpolator(new BounceInterpolator()); animator.setStartDelay(i * 300); animator.start(); flag = false; } } }
activity_my.xml
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="test.ban.com.test.MainActivity"> <ImageView android:id="@+id/ivh" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingLeft="5dp" android:paddingTop="5dp" android:src="@drawable/h" /> <ImageView android:id="@+id/ivg" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingLeft="5dp" android:paddingTop="5dp" android:src="@drawable/g" /> <ImageView android:id="@+id/ivf" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingLeft="5dp" android:paddingTop="5dp" android:src="@drawable/f" /> <ImageView android:id="@+id/ive" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingLeft="5dp" android:paddingTop="5dp" android:src="@drawable/e" /> <ImageView android:id="@+id/ivd" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingLeft="5dp" android:paddingTop="5dp" android:src="@drawable/d" /> <ImageView android:id="@+id/ivc" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingLeft="5dp" android:paddingTop="5dp" android:src="@drawable/c" /> <ImageView android:id="@+id/ivb" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingLeft="5dp" android:paddingTop="5dp" android:src="@drawable/b" /> <ImageView android:id="@+id/iva" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/a" /> </FrameLayout>