android中的animations的用法 (一)

Animations 可分为两大类:

一 Tweened Animations,该类Animations提供了旋转,移动,伸展,和淡入淡出等效果

二 Frame-by-Frame Animations ,该类Animations 可以创建一个Drawable序列,这些Drawable可能按照指定的时间一个一个的显示,类似于电影.

Tweened Animations 有这4种分类

1 Alpha         淡入淡出

2 Rotate       旋转

3 Scale          缩放

4 Translate     移动

使用Tweened Animations 的步骤

1.  创建一个Animations 对象

2.  根据需要创建相应的Animation对象

3.  按照需求,为Animation 对象设置相应的数据

4.  将Animation对象添加到AnimationSet对象当中

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

各种Animation 的主要属性

Animation type           |             attributes                 |               valid  values               

     Alpha                             fromAlpha/toAlpha                       Float  from 0  to  1

     Scale                            fromXScale/toXScale                      Float  from 0 to   1

                                        fromYScale/toYScale                      Float  from 0 to    1

                                        pivotX/pivotY                                String of the percentage of graphic

                                                                                           width/height from 0% to 100%

   Translate                        fromX/to X                                   Float from 0 to 1

                                        from Y/to Y                                   Float from 0 to 1

   Rotate                          fromDegrees/toDegrees                  Float from 0 to 360

                                       pivotX/pivotY                                String of the percentage of graphic

                                                                                           width/height from 0% to 100%

---------------------------

layout布局文件mian.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"
    >
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello"
    />
    <ImageView
    android:id="@+id/imageViewId"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/icon"
    />
    <Button 
    android:id="@+id/alphaID"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" 
    android:text="淡入淡出效果"
    />
    <Button 
    android:id="@+id/scaleID"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" 
    android:text="缩放效果"
    />
    <Button 
    android:id="@+id/rotateID"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" 
    android:text="旋转效果"
    />
    <Button 
    android:id="@+id/translateID"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" 
    android:text="移动效果"
    />
</LinearLayout>

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 TestAnimation01Activity extends Activity {
    /** Called when the activity is first created. */
	private ImageView imageView = null;
	private Button alphaButton = null;
	private Button scaleButton = null;
	private Button rotateButton = null;
	private Button translateButton = null;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        imageView = (ImageView)findViewById(R.id.imageViewId);
        alphaButton = (Button)findViewById(R.id.alphaID);
        alphaButton.setOnClickListener(new AlphaButtonListener());
        scaleButton = (Button)findViewById(R.id.scaleID);
        scaleButton.setOnClickListener(new ScaleButtonListener());
        rotateButton = (Button)findViewById(R.id.rotateID);
        rotateButton.setOnClickListener(new RotateButtonListener());
        translateButton = (Button)findViewById(R.id.translateID);
        translateButton.setOnClickListener(new TranslateButtonListener());
    }
    private class AlphaButtonListener implements OnClickListener
    {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			AnimationSet animationSet = new AnimationSet(true);
			AlphaAnimation alpha = new AlphaAnimation(1,0);
			alpha.setDuration(1000);
			animationSet.addAnimation(alpha);
			imageView.startAnimation(animationSet);
			
		}
    	
    }
    private class ScaleButtonListener implements OnClickListener
    {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			AnimationSet animationSet = new AnimationSet(true);
			ScaleAnimation scale = new ScaleAnimation(1,0.1f,1,0.1f,Animation.RELATIVE_TO_SELF,05f,Animation.RELATIVE_TO_SELF,0.5f);
			scale.setDuration(2000);
			animationSet.addAnimation(scale);
			imageView.startAnimation(scale);
		}
    	
    }
    private class RotateButtonListener implements OnClickListener
    {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			AnimationSet animationSet = new AnimationSet(true);
			RotateAnimation rotate = new RotateAnimation(0,360,Animation.RELATIVE_TO_PARENT,1f,Animation.RELATIVE_TO_PARENT,0f);
			rotate.setDuration(5000);
			animationSet.addAnimation(rotate);
			imageView.startAnimation(animationSet);
		}
    	
    }
    private class TranslateButtonListener implements OnClickListener
    {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			AnimationSet animationSet = new AnimationSet(true);
			TranslateAnimation translate = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 0.5f);
			translate.setDuration(2000);
			animationSet.addAnimation(translate);
			imageView.startAnimation(animationSet);
			
		}
    	
    }
    
}

Tween Animations 的通用属性

1. setDuration(long durationMills)  设置动画持续时间(单位:毫秒)

2. setFillAfter(boolean fillAfter)  如果fillAfter为true,表示动画执行后,控件将停留在执行结束的状态

3. setFillBefore(boolean fillBefore) 如果fillBefore 的值为true,表示动画执行后,控件将回到动画执行之前的状态

4. setStartOffSet(long startOffSet)  设置动画执行之前的等待时间

5. setRepeatCount(int count)  设置动画重复执行的次数

--------------------

下面再介绍Animations 另一种使用方法

1.  在res文件夹下面新建一个名为anim的文件夹;

2.  创建xml文件,并首先加入set标签,改标签如下:

     <set xmlns:android=http://schemas.android.com/apk/res/android"

           android:interpolator="@android:anim/acccelerate_interpolator" >

     </set>

3. 在该标签当中加入rotate,alpha,scale,translate标签

4. 在代码当中使用AnimationUtils当中装载xml文件,并生成   Animation  对象

在rotate.xml文件中

android:pivotX的值共有三种设置方法:

1.  android:pivotX="50"  使用绝对位置定位

2.  android:pivotX="50%"  使用相对于控件本身定位

3.  android:pivotX="50%p"  使用相对于控件的父控件定位



你可能感兴趣的:(android,animations)