感谢慕课网--eclipse_xu
布局文件:activity_main.xml
1 <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 tools:context="com.example.animationapp.MainActivity" > 6 7 <ImageView 8 android:id="@+id/im0" 9 android:layout_width="wrap_content" 10 android:layout_height="wrap_content" 11 android:paddingLeft="3dp" 12 android:paddingTop="3dp" 13 android:src="@drawable/b" /> 14 15 <ImageView 16 android:id="@+id/im1" 17 android:layout_width="wrap_content" 18 android:layout_height="wrap_content" 19 android:paddingLeft="3dp" 20 android:paddingTop="3dp" 21 android:src="@drawable/c" /> 22 23 <ImageView 24 android:id="@+id/im2" 25 android:layout_width="wrap_content" 26 android:layout_height="wrap_content" 27 android:paddingLeft="3dp" 28 android:paddingTop="3dp" 29 android:src="@drawable/d" /> 30 31 <ImageView 32 android:id="@+id/im3" 33 android:layout_width="wrap_content" 34 android:layout_height="wrap_content" 35 android:paddingLeft="3dp" 36 android:paddingTop="3dp" 37 android:src="@drawable/e" /> 38 39 <ImageView 40 android:id="@+id/im4" 41 android:layout_width="wrap_content" 42 android:layout_height="wrap_content" 43 android:paddingLeft="3dp" 44 android:paddingTop="3dp" 45 android:src="@drawable/f" /> 46 47 <ImageView 48 android:id="@+id/im5" 49 android:layout_width="wrap_content" 50 android:layout_height="wrap_content" 51 android:paddingLeft="3dp" 52 android:paddingTop="3dp" 53 android:src="@drawable/g" /> 54 55 <ImageView 56 android:id="@+id/im6" 57 android:layout_width="wrap_content" 58 android:layout_height="wrap_content" 59 android:paddingLeft="3dp" 60 android:paddingTop="3dp" 61 android:src="@drawable/h" /> 62 63 <ImageView 64 android:id="@+id/im7" 65 android:layout_width="wrap_content" 66 android:layout_height="wrap_content" 67 android:src="@drawable/a" /> 68 69 </FrameLayout>
图片资源
MainActivity.java
1 package com.example.animationapp; 2 3 import java.util.ArrayList; 4 import java.util.List; 5 6 import android.support.v7.app.ActionBarActivity; 7 import android.animation.ObjectAnimator; 8 import android.os.Bundle; 9 import android.view.Menu; 10 import android.view.MenuItem; 11 import android.view.View; 12 import android.view.View.OnClickListener; 13 import android.view.animation.BounceInterpolator; 14 import android.widget.ImageView; 15 import android.widget.Toast; 16 17 public class MainActivity extends ActionBarActivity implements OnClickListener { 18 19 private int[] res = {R.id.im0, R.id.im1,R.id.im2, 20 R.id.im3,R.id.im4,R.id.im5,R.id.im6,R.id.im7}; 21 22 private List<ImageView> list = new ArrayList<ImageView>(); 23 24 private boolean flag = true; 25 26 @Override 27 protected void onCreate(Bundle savedInstanceState) { 28 super.onCreate(savedInstanceState); 29 setContentView(R.layout.activity_main); 30 31 initView(); 32 } 33 34 private void initView() { 35 36 for(int i = 0; i < res.length; i++){ 37 ImageView im = (ImageView) findViewById(res[i]); 38 im.setOnClickListener(this); 39 list.add(im); 40 } 41 } 42 43 @Override 44 public void onClick(View v) { 45 switch(v.getId()){ 46 //im7最后布局,叠加在最上面 47 case R.id.im7:{ 48 49 if(flag){ 50 startAnimation(); 51 flag = false; 52 }else{ 53 closeAnimation(); 54 flag = true; 55 } 56 break; 57 } 58 59 default: 60 Toast.makeText(MainActivity.this, v.getId()+"OK", Toast.LENGTH_SHORT).show(); 61 break; 62 } 63 } 64 65 private void closeAnimation() { 66 for(int i = 0 ;i<res.length-1; i++){ 67 ObjectAnimator oa = ObjectAnimator.ofFloat(list.get(i), 68 "translationY",i*200F, 0F ); 69 oa.setDuration(100); 70 //oa.setStartDelay(50*i); 71 oa.start(); 72 } 73 } 74 75 private void startAnimation() { 76 //此处只需要弹出其余7个图标,最上边的图标im7不动 77 for(int i = 0 ;i<res.length-1; i++){ 78 //属性动画操作类 79 ObjectAnimator oa = ObjectAnimator.ofFloat(list.get(i), 80 "translationY", 0F, i*200F); 81 oa.setDuration(200); 82 //oa.setInterpolator(new BounceInterpolator()); 83 //oa.setStartDelay(200*i); 84 oa.start(); 85 } 86 } 87 88 }