android动画的用法


帧动画(Frame Animation)

顺序显示动画文件:animation1.xml

<?xml version="1.0" encoding="utf-8"?>
<!-- 
	根标签为animation-list,其中oneshot代表着是否只展示一遍,设置为false会不停的循环播放动画
	根标签下,通过item标签对动画中的每一个图片进行声明
	android:duration 表示展示所用的该图片的时间长度
 -->
<animation-list
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:oneshot="true"
  >
  	<item android:drawable="@drawable/icon1" android:duration="150"></item>
  	<item android:drawable="@drawable/icon2" android:duration="150"></item>
  	<item android:drawable="@drawable/icon3" android:duration="150"></item>
  	<item android:drawable="@drawable/icon4" android:duration="150"></item>
  	<item android:drawable="@drawable/icon5" android:duration="150"></item>
  	<item android:drawable="@drawable/icon6" android:duration="150"></item>
</animation-list>
  1.                 animationIV.setImageResource(R.drawable.animation1);  
  2.                 animationDrawable = (AnimationDrawable) animationIV.getDrawable();  
  3.                 animationDrawable.start(); //启动帧动画
  4.                  animationDrawable.stop(); //停止帧动画

补间动画(Tween Animation)

补间动画与逐帧动画在本质上是不同的,逐帧动画通过连续播放图片来模拟动画的效果,而补间动画则是通过在两个关键帧之间补充渐变的动画效果来实现的。补间动画的优点是可以节省空间。 目前Android应用框架支持的补间动画效果有以下5种。具体实现在android.view.animation类库中。

AlphaAnimation:透明度(alpha)渐变效果,对应<alpha/>标签。

TranslateAnimation:位移渐变,需要指定移动点的开始和结束坐标,对应<translate/>标签。

ScaleAnimation:缩放渐变,可以指定缩放的参考点,对应<scale/>标签。

RotateAnimation:旋转渐变,可以指定旋转的参考点,对应<rotate/>标签。

AnimationSet:组合渐变,支持组合多种渐变效果,对应<set/>标签。

补间动画的效果同样可以使用XML语言来定义,这些动画模板文件通常会被放在Android项目的res/anim/目录下。

下面是具体实现源码:

MainActivity.java封装实现的方法:

[java]  view plain  copy
  1. package com.example.lesson19_tween;  
  2.   
  3. import com.example.lesson19_tween.R;  
  4.   
  5. import android.app.Activity;  
  6. import android.os.Bundle;  
  7. import android.view.Menu;  
  8. import android.view.View;  
  9. import android.view.animation.Animation;  
  10. import android.view.animation.AnimationUtils;  
  11. import android.view.animation.ScaleAnimation;  
  12. import android.view.animation.TranslateAnimation;  
  13. import android.widget.ImageView;  
  14.   
  15. public class MainActivity extends Activity {  
  16.   
  17.     private ImageView imageView;  
  18.   
  19.     @Override  
  20.     protected void onCreate(Bundle savedInstanceState) {  
  21.         super.onCreate(savedInstanceState);  
  22.         setContentView(R.layout.activity_main);  
  23.   
  24.         imageView = (ImageView) findViewById(R.id.imageView1);  
  25.     }  
  26.   
  27.     @Override  
  28.     public boolean onCreateOptionsMenu(Menu menu) {  
  29.         // Inflate the menu; this adds items to the action bar if it is present.  
  30.         getMenuInflater().inflate(R.menu.main, menu);  
  31.         return true;  
  32.     }  
  33.   
  34.     // 透明动画  
  35.     public void alphaImpl(View v) {  
  36.   
  37.         Animation animation = AnimationUtils.loadAnimation(this,  
  38.                 R.anim.alpha_demo);  
  39.         imageView.startAnimation(animation);  
  40.     }  
  41.   
  42.     // 旋转动画  
  43.     public void rotateImpl(View v) {  
  44.         Animation animation = AnimationUtils.loadAnimation(this,  
  45.                 R.anim.rotate_demo);  
  46.         imageView.startAnimation(animation);  
  47.     }  
  48.   
  49.     // 缩放动画  
  50.     public void scaleImpl(View v) {  
  51.         Animation animation = AnimationUtils.loadAnimation(this,  
  52.                 R.anim.scale_demo);  
  53.         imageView.startAnimation(animation);  
  54.     }  
  55.   
  56.     // 移动效果  
  57.     public void translateImpl(View v) {  
  58.         // XML文件  
  59.         Animation animation = AnimationUtils.loadAnimation(this,  
  60.                 R.anim.translate_demo);  
  61.   
  62.         animation.setRepeatCount(Animation.INFINITE);//循环显示  
  63.         imageView.startAnimation(animation);  
  64.   
  65.         /* 
  66.          * 第一种 imageView.setAnimation(animation); animation.start(); 
  67.          */  
  68.         // 第二种  
  69.   
  70.         // Java代码  
  71.         /* 
  72.          * TranslateAnimation translateAnimation = new TranslateAnimation(0, 
  73.          * 200, 0, 0); translateAnimation.setDuration(2000); 
  74.          * imageView.startAnimation(translateAnimation); 
  75.          */  
  76.     }  
  77.   
  78.     // 综合实现set_demo.xml中的动画  
  79.     public void setAll(View v) {  
  80.         Animation animation = AnimationUtils.loadAnimation(this,  
  81.                 R.anim.set_demo);  
  82.         imageView.startAnimation(animation);  
  83.     }  
  84.       
  85. }  

alpha_demo.xml

[html]  view plain  copy
  1. <alpha xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     android:interpolator="@android:anim/accelerate_decelerate_interpolator"  
  3.     android:fromAlpha="1.0"  
  4.     android:toAlpha="0.1"  
  5.     android:duration="2000"/>  
  6.  <!--   
  7.  fromAlpha :起始透明度  
  8.  toAlpha:结束透明度  
  9.  1.0表示完全不透明  
  10.  0.0表示完全透明  
  11.   -->  

rotate_demo.xml

[html]  view plain  copy
  1. <rotate xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     android:interpolator="@android:anim/accelerate_decelerate_interpolator"  
  3.     android:fromDegrees="0"  
  4.     android:toDegrees="360"  
  5.     android:duration="1000"  
  6.     android:repeatCount="1"  
  7.     android:repeatMode="reverse"/>  
  8. <!--   
  9. fromDegrees:表示旋转的起始角度  
  10. toDegrees:表示旋转的结束角度  
  11. repeatCount:旋转的次数  默认值是0 代表旋转1次  如果值是repeatCount=4 旋转5次,值为-1或者infinite时,表示补间动画永不停止  
  12. repeatMode 设置重复的模式。默认是restart。当repeatCount的值大于0或者为infinite时才有效。  
  13. repeatCount=-1 或者infinite 循环了  
  14. 还可以设成reverse,表示偶数次显示动画时会做与动画文件定义的方向相反的方向动行。  
  15.  -->  

scale_demo.xml

[html]  view plain  copy
  1. <scale xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     android:interpolator="@android:anim/accelerate_interpolator"  
  3.     android:fromXScale="0.2"  
  4.     android:toXScale="1.5"  
  5.     android:fromYScale="0.2"  
  6.     android:toYScale="1.5"  
  7.     android:pivotX="50%"  
  8.     android:pivotY="50%"  
  9.     android:duration="2000"/>  
  10.   
  11. <!--   
  12. fromXScale:表示沿着x轴缩放的起始比例  
  13. toXScale:表示沿着x轴缩放的结束比例  
  14.   
  15. fromYScale:表示沿着y轴缩放的起始比例  
  16. toYScale:表示沿着y轴缩放的结束比例  
  17.   
  18. 图片中心点:  
  19.   android:pivotX="50%"   
  20.     android:pivotY="50%"  
  21.   
  22.  -->  

translate_demo.xml

[html]  view plain  copy
  1. <translate xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     android:interpolator="@android:anim/accelerate_decelerate_interpolator"  
  3.     android:fromXDelta="0"  
  4.     android:toXDelta="320"  
  5.     android:fromYDelta="0"  
  6.     android:toYDelta="0"  
  7.     android:duration="2000"/>   
  8.       
  9. <!--   
  10.   android:interpolator 动画的渲染器  
  11.   1、accelerate_interpolator(动画加速器) 使动画在开始的时候 最慢,然后逐渐加速  
  12.   2、decelerate_interpolator(动画减速器)使动画在开始的时候 最快,然后逐渐减速  
  13.   3、accelerate_decelerate_interpolator(动画加速减速器)  
  14.            中间位置分层:  使动画在开始的时候 最慢,然后逐渐加速           
  15.           使动画在开始的时候 最快,然后逐渐减速  结束的位置最慢  
  16.  fromXDelta  动画起始位置的横坐标  
  17.  toXDelta    动画起结束位置的横坐标  
  18.  fromYDelta  动画起始位置的纵坐标  
  19.  toYDelta   动画结束位置的纵坐标  
  20.  duration 动画的持续时间  
  21.  -->  

set_demo.xml

[html]  view plain  copy
  1. <set xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     android:interpolator="@android:anim/decelerate_interpolator"  
  3.     android:shareInterpolator="true" >  
  4.   
  5.     <scale  
  6.         android:duration="2000"  
  7.         android:fromXScale="0.2"  
  8.         android:fromYScale="0.2"  
  9.         android:pivotX="50%"  
  10.         android:pivotY="50%"  
  11.         android:toXScale="1.5"  
  12.         android:toYScale="1.5" />  
  13.   
  14.     <rotate  
  15.         android:duration="1000"  
  16.         android:fromDegrees="0"  
  17.         android:repeatCount="1"  
  18.         android:repeatMode="reverse"  
  19.         android:toDegrees="360" />  
  20.   
  21.     <translate  
  22.         android:duration="2000"  
  23.         android:fromXDelta="0"  
  24.         android:fromYDelta="0"  
  25.         android:toXDelta="320"  
  26.         android:toYDelta="0" />  
  27.   
  28.     <alpha  
  29.         android:duration="2000"  
  30.         android:fromAlpha="1.0"  
  31.         android:toAlpha="0.1" />  
  32.   
  33.       
  34.   
  35. </set>  

布局文件:

[html]  view plain  copy
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:paddingBottom="@dimen/activity_vertical_margin"  
  6.     android:paddingLeft="@dimen/activity_horizontal_margin"  
  7.     android:paddingRight="@dimen/activity_horizontal_margin"  
  8.     android:paddingTop="@dimen/activity_vertical_margin"  
  9.     tools:context=".MainActivity" >  
  10.   
  11.     <Button  
  12.         android:id="@+id/button1"  
  13.         android:layout_width="wrap_content"  
  14.         android:layout_height="wrap_content"  
  15.         android:layout_alignParentLeft="true"  
  16.         android:layout_alignParentTop="true"  
  17.         android:layout_marginLeft="23dp"  
  18.         android:layout_marginTop="15dp"  
  19.          android:onClick="translateImpl"  
  20.         android:text="@string/text_translate" />  
  21.   
  22.     <Button  
  23.         android:id="@+id/button2"  
  24.         android:layout_width="wrap_content"  
  25.         android:layout_height="wrap_content"  
  26.         android:layout_alignBottom="@+id/button1"  
  27.         android:layout_marginLeft="21dp"  
  28.          android:onClick="rotateImpl"  
  29.         android:layout_toRightOf="@+id/imageView1"  
  30.         android:text="@string/text_rotate" />  
  31.   
  32.     <Button  
  33.         android:id="@+id/button3"  
  34.         android:layout_width="wrap_content"  
  35.         android:layout_height="wrap_content"  
  36.         android:layout_alignRight="@+id/button1"  
  37.         android:layout_below="@+id/button1"  
  38.         android:layout_marginTop="32dp"  
  39.         android:onClick="scaleImpl"  
  40.         android:text="@string/text_scale" />  
  41.   
  42.     <Button  
  43.         android:id="@+id/button4"  
  44.         android:layout_width="wrap_content"  
  45.         android:layout_height="wrap_content"  
  46.         android:layout_alignBaseline="@+id/button3"  
  47.         android:layout_alignBottom="@+id/button3"  
  48.         android:layout_alignLeft="@+id/button2"  
  49.         android:onClick="alphaImpl"  
  50.         android:text="@string/text_alpha" />  
  51.   
  52.     <Button  
  53.         android:id="@+id/button5"  
  54.         android:layout_width="wrap_content"  
  55.         android:layout_height="wrap_content"  
  56.         android:layout_below="@+id/button3"  
  57.         android:layout_centerHorizontal="true"  
  58.         android:onClick="setAll"  
  59.         android:text="@string/text_set" />  
  60.   
  61.     <ImageView  
  62.         android:id="@+id/imageView1"  
  63.         android:layout_width="wrap_content"  
  64.         android:layout_height="wrap_content"  
  65.         android:layout_below="@+id/button5"  
  66.         android:layout_marginTop="48dp"  
  67.         android:layout_toRightOf="@+id/button3"  
  68.         android:src="@drawable/ic_launcher" />  
  69.   
  70. </RelativeLayout>  
上面的补间动画是通过xml文件进行动画的设置,下面使用的是代码设置动画

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

        scaleButton.setOnClickListener(newScaleButtonListener());

        alphaButton.setOnClickListener(newAlphaButtonListener());

        translateButton.setOnClickListener(

           new TranslateButtonListener());

    }

    class AlphaButtonListener implementsOnClickListener{

       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 RotateButtonListener implementsOnClickListener{

       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 ScaleButtonListener implementsOnClickListener{

       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 TranslateButtonListener implementsOnClickListener{

       public void onClick(View v) {

           AnimationSet animationSet = new AnimationSet(true);

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

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

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

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

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

       }

    }

}



从右往左进入(配置文件)

right_to_left_in.xml

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

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


    <translate
        android:duration="500"
        android:fromXDelta="100%p"
        android:toXDelta="0" />


    <alpha
        android:duration="500"
        android:fromAlpha="1.0"
        android:toAlpha="1.0" />


</set>

从左往右离开(配置文件)

left_to_right_out.xml

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


    <translate
        android:duration="500"
        android:fromXDelta="0"
        android:toXDelta="100%p" />


    <alpha
        android:duration="500"
        android:fromAlpha="1.0"
        android:toAlpha="1.0" />


</set>

//android:fromXDelt代表水平,把x换成Y就是垂直的动画


/ 使用AnimationUtils装载动画配置文件

           Animation animation = AnimationUtils.loadAnimation(

                  Animation1Activity.this, R.anim.alpha);

           // 启动动画

           image.startAnimation(animation);

//监听动画的结束

animation .setAnimationListener(new AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationRepeat(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
doXXXX();
}
});
}


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值