Animations

1.什么是Animations

  Animations提供了一系列的动画效果,这些效果可以应用在绝大多数的控件

2.Animations的分类

   a:TweenedAnimations:该类Animations提供了旋转(Rotate),移动(Translate),伸展(Scale)和淡出(Aplpha)等等效果

     (一) 使用TweenedAnimations的步骤

      (1)创建一个AnimationSet对象

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

      (3)根据软件动画的需求,为Animation对象设置相应的数据

      (4)将Animation对象添加到AnimationSet对象当中

      (5)使用控件对象开始执行AnimationSet;  

    (二)TweenedAnimations的通用属性

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

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

      (3)setFillBefore(boolean fillAfter):如果fillBefore为true,则动画执行后,控件将会回到执行之前的状态

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

      (5)setRepateCount(int repeatCount):设置动画重复执行的次数

    (三)编写程序:

       (1)布局文件

<?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" >
    
    <ImageView
       android:src="@drawable/r"
       android:id="@+id/myImage"
        android:layout_width="fill_parent"
        android:contentDescription="@string/des"
        android:layout_height="130pt"/>

    <Button
        android:id="@+id/rotateButton"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/rotateButton" />
    <Button
        android:id="@+id/scaleButton"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/scaleButton" />
    <Button
        android:id="@+id/alphaButton"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/alphaButton" />
    <Button
        android:id="@+id/translateButton"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/translateButton" />

</LinearLayout>

 (2)AnimationsActivity.java

package wei.cao.test;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
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 {
    /** Called when the activity is first created. */
	
	private ImageView imageView=null;
	private Button rotateButton=null;
	private Button scaleButton=null;
	private Button alphaButton=null;
	private Button translateButton=null;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        rotateButton=(Button)this.findViewById(R.id.rotateButton);
        scaleButton=(Button)this.findViewById(R.id.scaleButton);
        alphaButton=(Button)this.findViewById(R.id.alphaButton);
        translateButton=(Button)this.findViewById(R.id.translateButton);
        imageView=(ImageView)this.findViewById(R.id.myImage);
        
       rotateButton.setOnClickListener(new View.OnClickListener() {
		
		@Override
		public void onClick(View arg0) {

   			AnimationSet animationSet=new AnimationSet(true);
   		     RotateAnimation rotateAnimation=new RotateAnimation(0, 360,
   		    		 Animation.RELATIVE_TO_PARENT, 1,
   		    		 Animation.RELATIVE_TO_PARENT,0);
   		    rotateAnimation.setDuration(1000);
   			animationSet.addAnimation(rotateAnimation);
   			imageView.startAnimation(animationSet);
			
		}
	});
       scaleButton.setOnClickListener(new View.OnClickListener() {
   		
   		@Override
   		public void onClick(View arg0) {
   			AnimationSet animationSet=new AnimationSet(true);
  		     ScaleAnimation scaleAnimation=new ScaleAnimation(0, 0.1f,1,0.1f,
  		    		 Animation.RELATIVE_TO_SELF, 0.5f,
  		    		 Animation.RELATIVE_TO_SELF,0.5f);
  		     scaleAnimation.setDuration(1000);
  			animationSet.addAnimation(scaleAnimation);
  			imageView.startAnimation(animationSet);
   			
   		}
   	});
       alphaButton.setOnClickListener(new View.OnClickListener() {
   		
   		@Override
   		public void onClick(View arg0) {
   			//创建一个AnimationSet对象
   			AnimationSet animationSet=new AnimationSet(true);
   			//创建一个Animation对象
   			AlphaAnimation alphaAnimation=new AlphaAnimation(1, 0);
   			//设置动画执行时间(单位:毫秒)
   			alphaAnimation.setDuration(1000);
   			//将AlphaAnimation对象添加到AnimationSet对象
   			animationSet.addAnimation(alphaAnimation);
   			//使用ImageView的starAnimation方法开始执行动画
   			imageView.startAnimation(animationSet);
   			
   		}
   	});
       translateButton.setOnClickListener(new View.OnClickListener() {
   		
   		@Override
   		public void onClick(View arg0) {
 			AnimationSet animationSet=new AnimationSet(true);
 			TranslateAnimation translateAnimation=new TranslateAnimation(
 					 Animation.RELATIVE_TO_SELF, 0f,
 					 Animation.RELATIVE_TO_SELF, 0.5f,
 		    		 Animation.RELATIVE_TO_SELF, 0f,
 		    		 Animation.RELATIVE_TO_SELF,1.0f);
 		    translateAnimation.setDuration(1000);
 			animationSet.addAnimation(translateAnimation);
 			imageView.startAnimation(animationSet);
   			
   		}
   	});
    }
}

(三)执行效果

 
(四)在xml中的使用步骤

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

    (2)创建xml文件,首先加入set标签,该标签如下

        <set xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/accelerate_interpolator"></set>

   (3)在set标签下中加入rolate,alpha,scale或者translate标签

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

    b:Frame-by-Frame Animations这一类Animations可以创建一个Drawable序列,这些Drawable可以按照指定的时间间隔一个一个的显示出来

3.AnimationSet使用方法  

   (1)AnimationSet是Animation的子类

   (2)一个AnimationSet包括了一系列的Animation对象

   (3)针对AnimationSet设置了一些Animation的常见属性(如startOffSet,duration等等),可以被包含在AnimationSet当中的Animation集成

4.Interpolator的使用方法

    Interpolator定义了动画变化的速率,在Animations框架当中定义了几种Interpolator(AccelerateDecelerateInterpolator,AccelerateInterpolator,CycleInterpolator,DecelerateInterpolator)

5.Frema-By-Frame Animation的使用方法

6.LayoutAnimationController的使用方法

   (1)LayoutAnimationController用于一个layout里面的控件,或者是一个ViewGroup里面的控件设置动画效果

   (2)每一个控件都有相同的动画效果

   (3)这些控件的效果可以以不同的方式显示出来

   (4)LayoutAnimationController可以在xml当中设置,也可以在代码当中设置
7.ListView与Animations结合使用
8.AnimationListener的使用方法

   (1)AnimationListener是一个监听器

   (2)该监听器在动画执行的各个阶段会得到通知,从而调用相应的方法

   (3)主要包含一下三种方法

        1.onAnimationEnd(Animation animation)

        2.onAnimationRepeat(Animation animation)

        3.onAnimationStart(Animation animation)

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值