android三大动画的基本使用

来源:互联网 发布:淘宝直通车定向推广 编辑:程序博客网 时间:2024/05/29 20:00

逐帧动画

1.最重要:自定义一个drawable->animation.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>

2.创建AnimationDrawable 对象

//XML里联系刚创建的drawable   <ImageView android:id="@+id/animationIV"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:padding="5px"        android:src="@drawable/animation1"/>//java代码AnimationDrawable anim=(AnimationDrawable)imageview.getDrawable();

注意:
代码使用imageview.getDrawable获取AnimationDrawable 对象
layout中属性就需要使用android:src=”@drawable/animation”
代码使用imageview.getBackground获取AnimationDrawable 对象
layout中属性就需要使用android:background=”@drawable/animation”

3.启动动画

anim.start();//启动需要在某个监听器内,不能放在onCreate方法内执行,因为AnimationDrawable 类在onCreate方法前还没加载完

4.代码中添加帧可用addFrame()


补间动画

  • 移动补间动画:TranslateAnimation
TranslateAnimation animTran=new TranslateAnimation(currX,nextX,currY,nextY);animTran.setDuration(200);imageView.startAnimation(animTran);

ps: 补间动画只是将view绘制在目标位置,并不是将view真实移动到目标位置,所以监听器什么的响应不了

  • 透明补间动画: AlphaAnimation
Animation animation = new AlphaAnimation(fromAlpha,toAlpha);//fromAlpha起始透明度,toAlpha目标透明度
  • 旋转补间动画:RotateAnimation
 Animation animation  = new RotateAnimation(360,0,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f); /*参数1:旋转的起始角度参数2:旋转的终止角度参数3:旋转中心的x轴取值参照方式参数4:中心点x轴的取值参数5:旋转中心的y轴取值参照方式参数6:中心点y轴的取值*/
  • 缩放补间动画:ScaleAnimation
Animation   animation = new ScaleAnimation(1f,0.2f,1f,0.2f,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f); /*参数1:x方向起始大小(1f表示原图大小)参数2:x方向终止大小(0.2f表示原图的0.2倍)参数3:y方向起始大小(1f表示原图大小)参数4:y方向终止大小(0.2f表示原图的0.2倍)参数5:缩放中心点x轴取值的参照方式参数6:中心点x轴的取值(0.5f表示相对与原图的0.5倍)参数7:缩放中心点y轴取值参照方式参数8:中心点y轴的取值(0.5f表示相对与原图的0.5倍)*/

XML方法定义补间动画
anim_alpha.xml

1.<?xml version="1.0" encoding="utf-8"?>  2.<set xmlns:android="http://schemas.android.com/apk/res/android"  3.android:fillEnabled="true"  4.android:fillAfter="true"  5.   >    6.    <alpha  7.        android:duration="2000"  8.        android:fromAlpha="1"  9.        android:repeatCount="1"  10.        android:repeatMode="reverse"  11.        android:toAlpha="0" />  12.</set> 

java代码联系xml

1.    Animation rotate = AnimationUtils.loadAnimation(this, R.anim.anim_rotate);

属性动画

比较常用的几个动画类是:ValueAnimator,ObjectAnimator和AnimatorSet,其中ObjectAnimator继承自ValueAnimator

以下举三个例子简单使用

  • 1.改变一个对象(myObject)的translationY属性,让其沿着Y轴向上平移一段距离:它的高度,改动画在默认时间完成,可
    自定义时间,还可以自定义插值器和估值算法。
ObjectAnimator.ofFloat(myObject,"translationY",-myObject.getHeight()).start();
  • 2.改变一个对象的背景色属性。下面动画让背景色在3秒内实现0xffff8080到0xff8080ff的渐变,动画会无限循环且反转
ValueAnimator colorAnim=ObjectAnimator.ofInt(myObject,"backgroundColor",/*Red*/0xffff8080,/*Blue*/0xff8080ff);colorAnim.setDuration(3000);colorAnim.setEvaluator(new ArgbEvaluator());colorAnim.setRepeatCount(ValueAnimator.INFINITE);//无限colorAnim.setRepeatMode(ValueAnimator.REVERSE);//相反colorAnim.start();
  • 3.动画集合,5秒对View的旋转,平移,缩放和透明度都进行改变。
AnimatorSet set=new AnimatorSet();set.playTogether(        ObjectAnimator.ofFloat(myView,"rotationX",0,360),        ObjectAnimator.ofFloat(myView,"rotationY",0,180),        ObjectAnimator.ofFloat(myView,"rotation",0,360),        ObjectAnimator.ofFloat(myView,"translationX",0,90),        ObjectAnimator.ofFloat(myView,"translationY",0,90),        ObjectAnimator.ofFloat(myView,"scaleX",1,1.5f),        ObjectAnimator.ofFloat(myView,"scaleY",1,0.5f),        ObjectAnimator.ofFloat(myView,"alpha",1,0.25f,1));set.setDuration(5*1000).start();

XML实现属性动画
在animator目录创建property_animator.xml文件

<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android"    android:ordering="together">    <objectAnimator        android:propertyName="x"        android:duration="300"        android:valueTo="200"        android:valueType="intType"/>    <objectAnimator        android:propertyName="y"        android:duration="300"        android:valueTo="300"        android:valueType="intType"/></set>

java代码

      AnimatorSet set1=(AnimatorSet) AnimatorInflater.loadAnimator(AttrAnimaActvity.this,R.animator.property_animator);        set1.setTarget(myView);        set1.start();

在实际开发中建议使用代码来实现属性动画,因为用代码实现更简单

原创粉丝点击