Animation的使用

一、Animations介绍

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

二、Animations的分类

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

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

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

 

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

对象之间的关系:

[转载]Android学习二_八:Animation的使用(一)

使用TweenedAnimations的步骤:

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

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

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

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

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

[转载]Android学习二_八:Animation的使用(一)

四、具体实现

1main.xml

<?xml version="1.0" encoding="utf-8"?>

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

   android:layout_width="fill_parent"

   android:layout_height="fill_parent"

   android:orientation="vertical" >

 

   <LinearLayout

       android:layout_width="wrap_content"

       android:layout_height="wrap_content"

       android:orientation="horizontal" >

 

       <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:layout_width="fill_parent"

       android:layout_height="fill_parent"

       android:orientation="vertical" >

 

       <ImageView

           android:id="@+id/image"

           android:layout_width="wrap_content"

           android:layout_height="wrap_content"

           android:layout_centerInParent="true"

           android:src="@drawable/an" />

   </LinearLayout>

 

</LinearLayout>

2.java文件

importandroid.app.Activity;

importandroid.os.Bundle;

importandroid.view.View;

importandroid.view.View.OnClickListener;

import android.view.animation.AlphaAnimation;

import android.view.animation.Animation;

importandroid.view.animation.AnimationSet;

importandroid.view.animation.RotateAnimation;

importandroid.view.animation.ScaleAnimation;

import android.view.animation.TranslateAnimation;

importandroid.widget.Button;

importandroid.widget.ImageView;

public class Animation1Activityextends 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(newRotateButtonListener());

       scaleButton.setOnClickListener(newScaleButtonListener());

       alphaButton.setOnClickListener(newAlphaButtonListener());

       translateButton.setOnClickListener(

          new TranslateButtonListener());

   }

   class AlphaButtonListenerimplementsOnClickListener{

      public void onClick(View v) {

          //创建一个AnimationSet对象,参数为Boolean型,

          //true表示使用Animationinterpolatorfalse则是使用自己的

          AnimationSet animationSet = new AnimationSet(true);

          //创建一个AlphaAnimation对象,参数从完全的透明度,到完全的不透明

          AlphaAnimation alphaAnimation = new AlphaAnimation(1, 0);

          //设置动画执行的时间

          alphaAnimation.setDuration(500);

          //alphaAnimation对象添加到AnimationSet当中

          animationSet.addAnimation(alphaAnimation);

          //使用ImageViewstartAnimation方法执行动画

          image.startAnimation(animationSet);

      }

   }

   class RotateButtonListenerimplementsOnClickListener{

      public void onClick(View v) {

          AnimationSet animationSet = new AnimationSet(true);

          //参数1:从哪个旋转角度开始

          //参数2:转到什么角度

          //4个参数用于设置围绕着旋转的圆的圆心在哪里

          //参数3:确定x轴坐标的类型,有ABSOLUT绝对坐标、RELATIVE_TO_SELF相对于自身坐标、RELATIVE_TO_PARENT相对于父控件的坐标

          //参数4x轴的值,0.5f表明是以自身这个控件的一半长度为x

          //参数5:确定y轴坐标的类型

          //参数6y轴的值,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 ScaleButtonListenerimplementsOnClickListener{

      public void onClick(View v) {

          AnimationSet animationSet = new AnimationSet(true);

          //参数1x轴的初始值

          //参数2x轴收缩后的值

          //参数3y轴的初始值

          //参数4y轴收缩后的值

          //参数5:确定x轴坐标的类型

          //参数6x轴的值,0.5f表明是以自身这个控件的一半长度为x

          //参数7:确定y轴坐标的类型

          //参数8y轴的值,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 TranslateButtonListenerimplementsOnClickListener{

      public void onClick(View v) {

          AnimationSet animationSet = new AnimationSet(true);

          //参数12x轴的开始位置

          //参数34y轴的开始位置

          //参数56x轴的结束位置

          //参数78x轴的结束位置

          TranslateAnimationtranslateAnimation =

             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);

      }

   }

}

[转载]Android学习二_八:Animation的使用(一)

 五、Tween Animations的通用方法

[转载]Android学习二_八:Animation的使用(一)




------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
android.view.animation. TranslateAnimation.TranslateAnimation(int fromXType, float fromXValue, int toXType, float toXValue, int fromYType, float fromYValue, int toYType, float toYValue)

Constructor to use when building a TranslateAnimation from code

Parameters:
fromXType Specifies how fromXValue should be interpreted. One of Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF, or Animation.RELATIVE_TO_PARENT.
fromXValue Change in X coordinate to apply at the start of the animation. This value can either be an absolute number if fromXType is ABSOLUTE, or a percentage (where 1.0 is 100%) otherwise.
toXType Specifies how toXValue should be interpreted. One of Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF, or Animation.RELATIVE_TO_PARENT.
toXValue Change in X coordinate to apply at the end of the animation. This value can either be an absolute number if toXType is ABSOLUTE, or a percentage (where 1.0 is 100%) otherwise.
fromYType Specifies how fromYValue should be interpreted. One of Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF, or Animation.RELATIVE_TO_PARENT.
fromYValue Change in Y coordinate to apply at the start of the animation. This value can either be an absolute number if fromYType is ABSOLUTE, or a percentage (where 1.0 is 100%) otherwise.
toYType Specifies how toYValue should be interpreted. One of Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF, or Animation.RELATIVE_TO_PARENT.
toYValue Change in Y coordinate to apply at the end of the animation. This value can either be an absolute number if toYType is ABSOLUTE, or a percentage (where 1.0 is 100%) otherwise.
从sdk方法声明来看,这个方法的参数作者写错了 纠正下。

另外android_SDK提供了很好的动画demo


有时间可以详细看看。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值