Android开发学习之Animation之Android帧动画解析

       今天给大家分享的是Android中的动画,在Android中系统为我们提供了四种动画效果,分别是:缩放、平移、旋转、渐变。下面我们通过一个简单的例子来了解Android中的动画机制。这里我们主要用到的类有ScaleAnimation、TranslateAnimation、RotateAnimation、AlphaAnimation,分别对应上面的四种动画效果。代码实现如下:

package com.Android.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.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends Activity {

	Button BtnScale,BtnMove,BtnRoate,BtnAlpha;
	ImageView iv;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		BtnScale=(Button)findViewById(R.id.BtnScale);
		BtnScale.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View arg0) 
			{
				Animation Ani_Scale=new ScaleAnimation(0f, 1f, 0f, 1f,
						             Animation.RELATIVE_TO_SELF, 0.5f,
						             Animation.RELATIVE_TO_SELF, 0.5f);
				Ani_Scale.setDuration(3000);
				iv.startAnimation(Ani_Scale);
			}
		});
		BtnMove=(Button)findViewById(R.id.BtnMove);
		BtnMove.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View arg0) 
			{
				Animation Ani_Trans = new TranslateAnimation(10,100,10,100);
				Ani_Trans.setDuration(3000);
				iv.startAnimation(Ani_Trans);
			}
		});
		BtnRoate=(Button)findViewById(R.id.BtnRoate);
		BtnRoate.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View arg0) 
			{
				Animation Ani_Rotate = new RotateAnimation(0f, -360f,
						Animation.RELATIVE_TO_SELF, 0.5f,
						Animation.RELATIVE_TO_SELF, 0.5f);
				Ani_Rotate.setDuration(3000);
				iv.startAnimation(Ani_Rotate);

			}
		});
		BtnAlpha=(Button)findViewById(R.id.BtnAlpha);
		BtnAlpha.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View arg0) 
			{
				Animation Ani_Alpha = new AlphaAnimation(0.1f, 1.0f);
				Ani_Alpha.setDuration(3000);
                iv.startAnimation(Ani_Alpha);
			}
		});
		iv=(ImageView)findViewById(R.id.ImageView);
	}

	@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之Android帧动画解析)