animation_3.xml:
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:orientation="vertical" 4 android:padding="10dip" 5 android:layout_width="match_parent" 6 android:layout_height="wrap_content" 7 android:clipToPadding="false"> 8 9 <TextView 10 android:id="@+id/target" 11 android:layout_width="wrap_content" 12 android:layout_height="wrap_content" 13 android:textSize="26sp" 14 android:text="@string/animation_3_text"/> 15 16 <TextView 17 android:layout_width="match_parent" 18 android:layout_height="wrap_content" 19 android:layout_marginTop="20dip" 20 android:layout_marginBottom="5dip" 21 android:text="@string/animation_2_instructions" /> 22 23 <Spinner 24 android:id="@+id/spinner" 25 android:layout_width="match_parent" 26 android:layout_height="wrap_content" /> 27 28 </LinearLayout>
Animation3.java:
1 package com.example.android.apis.view; 2 3 import com.example.android.apis.R; 4 5 import android.app.Activity; 6 import android.os.Bundle; 7 import android.view.View; 8 import android.view.animation.AnimationUtils; 9 import android.view.animation.Animation; 10 import android.view.animation.TranslateAnimation; 11 import android.widget.AdapterView; 12 import android.widget.ArrayAdapter; 13 import android.widget.Spinner; 14 15 public class Animation3 extends Activity implements AdapterView.OnItemSelectedListener { 16 private static final String[] INTERPOLATORS = { 17 "Accelerate", "Decelerate", "Accelerate/Decelerate", 18 "Anticipate", "Overshoot", "Anticipate/Overshoot", 19 "Bounce" 20 }; 21 22 @Override 23 public void onCreate(Bundle savedInstanceState) { 24 super.onCreate(savedInstanceState); 25 setContentView(R.layout.animation_3); 26 27 Spinner s = (Spinner) findViewById(R.id.spinner); 28 ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, 29 android.R.layout.simple_spinner_item, INTERPOLATORS); 30 adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 31 s.setAdapter(adapter); 32 s.setOnItemSelectedListener(this); 33 } 34 35 public void onItemSelected(AdapterView<?> parent, View v, int position, long id) { 36 final View target = findViewById(R.id.target); 37 final View targetParent = (View) target.getParent(); 38 39 Animation a = new TranslateAnimation(0.0f, 40 targetParent.getWidth() - target.getWidth() - targetParent.getPaddingLeft() - 41 targetParent.getPaddingRight(), 0.0f, 0.0f); 42 a.setDuration(1000); 43 a.setStartOffset(300); 44 a.setRepeatMode(Animation.RESTART); 45 a.setRepeatCount(Animation.INFINITE); 46 47 switch (position) { 48 case 0: 49 a.setInterpolator(AnimationUtils.loadInterpolator(this, 50 android.R.anim.accelerate_interpolator)); 51 break; 52 case 1: 53 a.setInterpolator(AnimationUtils.loadInterpolator(this, 54 android.R.anim.decelerate_interpolator)); 55 break; 56 case 2: 57 a.setInterpolator(AnimationUtils.loadInterpolator(this, 58 android.R.anim.accelerate_decelerate_interpolator)); 59 break; 60 case 3: 61 a.setInterpolator(AnimationUtils.loadInterpolator(this, 62 android.R.anim.anticipate_interpolator)); 63 break; 64 case 4: 65 a.setInterpolator(AnimationUtils.loadInterpolator(this, 66 android.R.anim.overshoot_interpolator)); 67 break; 68 case 5: 69 a.setInterpolator(AnimationUtils.loadInterpolator(this, 70 android.R.anim.anticipate_overshoot_interpolator)); 71 break; 72 case 6: 73 a.setInterpolator(AnimationUtils.loadInterpolator(this, 74 android.R.anim.bounce_interpolator)); 75 break; 76 } 77 78 target.startAnimation(a); 79 } 80 81 public void onNothingSelected(AdapterView<?> parent) { 82 } 83 }
效果如下,文字会根据不同的效果执行动画显示效果: