Android开发之Animation简单实现(源代码分享)

    以下代码简单实现了Android的animation的四种基本方法,rotate,translate,scale,alpha!


package com.example.soeb08_animation;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends Activity {
	private Button rotate, translate, scale, alpha;
	private ImageView imageView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		rotate = (Button) this.findViewById(R.id.button1);
		translate = (Button) this.findViewById(R.id.button2);
		scale = (Button) this.findViewById(R.id.button3);
		alpha = (Button) this.findViewById(R.id.button4);
		imageView = (ImageView) this.findViewById(R.id.imageView1);
		AnimationListenr listenr = new AnimationListenr();
		rotate.setOnClickListener(listenr);
		translate.setOnClickListener(listenr);
		scale.setOnClickListener(listenr);
		alpha.setOnClickListener(listenr);
	}

	class AnimationListenr implements OnClickListener {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			AnimationSet animationSet = new AnimationSet(true);
			animationSet.setDuration(1000);
			switch (v.getId()) {
			case R.id.button1:
				RotateAnimation rotateAnimation = new RotateAnimation(0, 360,
						Animation.RELATIVE_TO_SELF, 0.5f,
						Animation.RELATIVE_TO_SELF, 0.5f);
				
				animationSet.addAnimation(rotateAnimation);
				imageView.startAnimation(animationSet);
				break;
			case R.id.button2:
				TranslateAnimation translateAnimation = new TranslateAnimation(
						Animation.RELATIVE_TO_SELF, 0f,
						Animation.RELATIVE_TO_SELF, 0f,
						Animation.RELATIVE_TO_SELF, 0f,
						Animation.RELATIVE_TO_SELF, 0.8f);
				animationSet.addAnimation(translateAnimation);
				imageView.startAnimation(animationSet);
				break;
			case R.id.button3:
				ScaleAnimation scaleAnimation=new ScaleAnimation(1f, 0.5f, 1f, 0.5f);
				animationSet.addAnimation(scaleAnimation);
				imageView.startAnimation(animationSet);
				break;
			case R.id.button4:
                AlphaAnimation alphaAnimation=new AlphaAnimation(1f, 0.1f);
                animationSet.addAnimation(alphaAnimation);
                animationSet.setFillAfter(true);
                imageView.startAnimation(animationSet);
				break;
			default:
				break;
			}
		}

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}


Android开发之Animation简单实现(源代码分享),,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。