四月份來了,我也要加緊學習了,下面的動畫類型android
- package com.smart;
- import android.app.Activity;
- import android.os.Bundle;
- import android.view.View;
- import android.view.animation.Animation;
- import android.view.animation.AnimationUtils;
- import android.view.animation.TranslateAnimation;
- import android.widget.AdapterView;
- import android.widget.ArrayAdapter;
- import android.widget.Spinner;
- public class Main extends Activity implements AdapterView.OnItemSelectedListener
- {
- private static final String[] INTERPOLATORS={
- "加快", "減速", "加快/減速", "預料",
- "過沖", "預計/過沖", "彈跳"
- };//類型
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- Spinner s=(Spinner)findViewById(R.id.spinner);
- ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,INTERPOLATORS);
- adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
- s.setAdapter(adapter);//獲得控件
- s.setOnItemSelectedListener(this);
- }
- @Override
- public void onItemSelected(AdapterView<?> parent, View v, int position,
- long id) {
- final View target=(View)findViewById(R.id.target);
- final View targetParent=(View)target.getParent();//獲得動畫
- Animation animation= new TranslateAnimation(0.0f, targetParent
- .getWidth()
- - target.getWidth()
- - targetParent.getPaddingLeft()
- - targetParent.getPaddingRight(), 0.0f, 0.0f);
- animation.setDuration(1000);
- animation.setStartOffset(300);
- animation.setRepeatMode(Animation.RESTART);
- animation.setRepeatCount(Animation.INFINITE);
- //選擇動來類型
- switch (position) {
- case 0:
- animation.setInterpolator(AnimationUtils.loadInterpolator(this, android.R.anim.accelerate_interpolator));
- break;
- case 1:
- animation.setInterpolator(AnimationUtils.loadInterpolator(this, android.R.anim.decelerate_interpolator));
- break;
- case 2:
- animation.setInterpolator(AnimationUtils.loadInterpolator(this, android.R.anim.accelerate_decelerate_interpolator));
- break;
- case 3:
- animation.setInterpolator(AnimationUtils.loadInterpolator(this, android.R.anim.anticipate_interpolator));
- break;
- case 4:
- animation.setInterpolator(AnimationUtils.loadInterpolator(this, android.R.anim.overshoot_interpolator));
- break;
- case 5:
- animation.setInterpolator(AnimationUtils.loadInterpolator(this, android.R.anim.anticipate_overshoot_interpolator));
- break;
- case 6:
- animation.setInterpolator(AnimationUtils.loadInterpolator(this, android.R.anim.bounce_interpolator));
- break;
- }
- target.startAnimation(animation);
- }
- @Override
- public void onNothingSelected(AdapterView<?> arg0) {
- }
- }
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:padding="10dip"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:clipToPadding="false"
- >
- <TextView
- android:id="@+id/target"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:textSize="16sp"
- android:text="朋友,看看我,跑得多快!"
- />
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:textSize="15dip"
- android:layout_marginBottom="5dip"
- android:text="請選擇動畫類型"
- />
- <Spinner
- android:id="@+id/spinner"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- />
- </LinearLayout>