`
Bauble
  • 浏览: 65970 次
  • 性别: Icon_minigender_1
  • 来自: Mercury
社区版块
存档分类
最新评论

Android33_Animations使用(一)

阅读更多

 

一、Animations介绍

       Animations是一个实现android UI界面动画效果的APIAnimations提供了一系列的动画效果,可以进行旋转、缩放、淡入淡出等,这些效果可以应用在绝大多数的控件中。

 

二、Animations的分类

       Animations从总体上可以分为两大类:

              1.Tweened Animations:该类Animations提供了旋转、移动、伸展和淡出等效果。Alpha——淡入淡出,Scale——缩放效果,Rotate——旋转,Translate——移动效果。

              2.Frame-by-frame Animations:这一类Animations可以创建一个Drawable序列,这些Drawable可以按照指定的时间间歇一个一个的显示。

 

三、Animations的使用方法(代码中使用)

       对象之间的关系:


  使用TweenedAnimations的步骤:

       1.创建一个AnimationSet对象(Animation子类);

       2.增加需要创建相应的Animation对象;

       3.更加项目的需求,为Animation对象设置相应的数据;

       4.Animatin对象添加到AnimationSet对象当中;

       5.使用控件对象开始执行AnimationSet

Main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
	<LinearLayout
	    android:orientation="horizontal"
	    android:layout_height="wrap_content" 
	    android:layout_width="wrap_content">
		<Button 
			android:id="@+id/rotateButton"
	    	android:layout_width="wrap_content"
	    	android:layout_height="wrap_content"
	    	android:text="旋转"/>
		<Button 
			android:id="@+id/scaleButton"
	    	android:layout_width="wrap_content"
	    	android:layout_height="wrap_content"
	    	android:text="缩放"/>
		<Button 
			android:id="@+id/alphaButton"
	    	android:layout_width="wrap_content"
	    	android:layout_height="wrap_content"
	    	android:text="淡入淡出"/>
		<Button 
			android:id="@+id/translateButton"
	    	android:layout_width="wrap_content"
	    	android:layout_height="wrap_content"
	    	android:text="移动"/>
	</LinearLayout>
	<LinearLayout
	    android:orientation="vertical"
	    android:layout_width="fill_parent"
	    android:layout_height="fill_parent">
		<ImageView 
			android:id="@+id/image"
	    	android:layout_width="wrap_content"
	    	android:layout_height="wrap_content"
	    	android:layout_centerInParent="true"
	    	android:src="@drawable/image"/>	
	</LinearLayout>
</LinearLayout>

 AnimationsActivity.java

package com.android.activity;
import android.app.Activity;
import android.os.Bundle;
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 AnimationsActivity extends Activity {
	private Button rotateButton = null;
	private Button scaleButton = null;
	private Button alphaButton = null;
	private Button translateButton = null;
	private ImageView image = null;
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        rotateButton = (Button)findViewById(R.id.rotateButton);
        scaleButton = (Button)findViewById(R.id.scaleButton);
        alphaButton = (Button)findViewById(R.id.alphaButton);
        translateButton = (Button)findViewById(R.id.translateButton);
        image = (ImageView)findViewById(R.id.image);
      
        rotateButton.setOnClickListener(new RotateButtonListener());
        scaleButton.setOnClickListener(new ScaleButtonListener());
        alphaButton.setOnClickListener(new AlphaButtonListener());
        translateButton.setOnClickListener(
			new TranslateButtonListener());
    }
	class AlphaButtonListener implements OnClickListener{
		public void onClick(View v) {
			//创建一个AnimationSet对象,参数为Boolean型,
			//true表示使用Animation的interpolator,false则是使用自己的
			AnimationSet animationSet = new AnimationSet(true);
			//创建一个AlphaAnimation对象,参数从完全的透明度,到完全的不透明
			AlphaAnimation alphaAnimation = new AlphaAnimation(1, 0);
			//设置动画执行的时间
			alphaAnimation.setDuration(500);
			//将alphaAnimation对象添加到AnimationSet当中
			animationSet.addAnimation(alphaAnimation);
			//使用ImageView的startAnimation方法执行动画
			image.startAnimation(animationSet);
		}
	}
	class RotateButtonListener implements OnClickListener{
		public void onClick(View v) {
			AnimationSet animationSet = new AnimationSet(true);
			//参数1:从哪个旋转角度开始
			//参数2:转到什么角度
			//后4个参数用于设置围绕着旋转的圆的圆心在哪里
			//参数3:确定x轴坐标的类型,有ABSOLUT绝对坐标、RELATIVE_TO_SELF相对于自身坐标、RELATIVE_TO_PARENT相对于父控件的坐标
			//参数4:x轴的值,0.5f表明是以自身这个控件的一半长度为x轴
			//参数5:确定y轴坐标的类型
			//参数6:y轴的值,0.5f表明是以自身这个控件的一半长度为x轴
			RotateAnimation rotateAnimation = new RotateAnimation(0, 360,
					Animation.RELATIVE_TO_SELF,0.5f,
					Animation.RELATIVE_TO_SELF,0.5f);
			rotateAnimation.setDuration(1000);
			animationSet.addAnimation(rotateAnimation);
			image.startAnimation(animationSet);
		}
	}
	class ScaleButtonListener implements OnClickListener{
		public void onClick(View v) {
			AnimationSet animationSet = new AnimationSet(true);
			//参数1:x轴的初始值
			//参数2:x轴收缩后的值
			//参数3:y轴的初始值
			//参数4:y轴收缩后的值
			//参数5:确定x轴坐标的类型
			//参数6:x轴的值,0.5f表明是以自身这个控件的一半长度为x轴
			//参数7:确定y轴坐标的类型
			//参数8:y轴的值,0.5f表明是以自身这个控件的一半长度为x轴
			ScaleAnimation scaleAnimation = new ScaleAnimation(
					0, 0.1f,0,0.1f,
					Animation.RELATIVE_TO_SELF,0.5f,
					Animation.RELATIVE_TO_SELF,0.5f);
			scaleAnimation.setDuration(1000);
			animationSet.addAnimation(scaleAnimation);
			image.startAnimation(animationSet);
		}
	}
	class TranslateButtonListener implements OnClickListener{
		public void onClick(View v) {
			AnimationSet animationSet = new AnimationSet(true);
			//参数1~2:x轴的开始位置
			//参数3~4:y轴的开始位置
			//参数5~6:x轴的结束位置
			//参数7~8:x轴的结束位置
			TranslateAnimation translateAnimation = 
				new TranslateAnimation(
					Animation.RELATIVE_TO_SELF,0f,
					Animation.RELATIVE_TO_SELF,0.5f,
					Animation.RELATIVE_TO_SELF,0f,
					Animation.RELATIVE_TO_SELF,0.5f);
			translateAnimation.setDuration(1000);
			animationSet.addAnimation(translateAnimation);
			image.startAnimation(animationSet);
		}
	}
}

 运行结果:

  • 大小: 7.9 KB
  • 大小: 32.4 KB
分享到:
评论
发表评论

文章已被作者锁定,不允许评论。

相关推荐

Global site tag (gtag.js) - Google Analytics