当前位置: 移动技术网 > IT编程>移动开发>Android > android 帧动画,补间动画,属性动画的简单总结

android 帧动画,补间动画,属性动画的简单总结

2019年07月24日  | 移动技术网IT编程  | 我要评论

伽利略的故事,吴俐璇,爱魔卡

帧动画——frameanimation

将一系列图片有序播放,形成动画的效果。其本质是一个drawable,是一系列图片的集合,本身可以当做一个图片一样使用

在drawable文件夹下,创建animation-list为根节点的资源文件

<animation-list android:oneshot="false">
 <item android:drawable="@drawable/img1" android:duration="100"/>
 <item android:drawable="@drawable/img2" android:duration="100"/>
 <item android:drawable="@drawable/img3" android:duration="100"/>
 <item android:drawable="@drawable/img4" android:duration="100"/>
</animation-list>

oneshot:是否只播放一次     

drawable:一帧引用的图片

duration:一帧播放的时间

播放动画

将动画作为控件的背景

((animationdrawable)view.getbackground()).start();

animation常用属性

duration:动画时间                  

repeatcount:重复次数 infinite无限次

fillafter:是否停止在最后一帧

repeatmode:重复模式     值:restart重新开始,reserve反复

startoffset:开始延迟时间

补间动画 tween animation

只能应用于view对象,只支持部分属性,view animation值改变了view绘制的位置,并没有改变对象本身的真实位置

可以使用xml定义也可以使用代码定义     xml定义的动画放在/res/anim/文件夹内

开始动画 通过view的startanimation(animation a)  参数定义的动画

四种补间动画通过xml定义

alphaanimation:透明度动画

<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
 android:fromalpha="0"
 android:toalpha="1"
 android:duration="2000">
 <!--
 fromalpha 起始透明度 0为完全透明 1为不透明 0~1之间的浮点值
 toalpha 结束透明度
 duration 动画运行时间 单位毫秒
 -->
</alpha>
alphaanimation alphaanimation=null;
 //加载xml中的动画xml文件
 alphaanimation= (alphaanimation) animationutils.loadanimation(mainactivity.this, r.anim.anim_alpha);
 //常用属性设置 各种动画通用
 alphaanimation.setrepeatcount(3);//执行动画效果结束后重复执行3次 一共4次
 alphaanimation.setrepeatmode(animation.reverse);//重复模式
 //动画结束是否停止在最后一帧
 alphaanimation.setfillafter(true);
 //动画结束是否停止在第一帧
 alphaanimation.setfillbefore(false);
 //设置插值器 动画执行速度 变速 加减速。。
 //accelerateinterpolator减速
 //decelerateinterpolator加速
 alphaanimation.setinterpolator(new acceleratedecelerateinterpolator());

scaleanimation:缩放动画

代码加载的方式和方法的使用与alphaanimation一样

<?xml version="1.0" encoding="utf-8"?>
<scale xmlns:android="http://schemas.android.com/apk/res/android"
 android:toxscale="1"
 android:toyscale="1"
 android:fromxscale="0.1"
 android:fromyscale="0.1"
 android:pivoty="0%"
 android:pivotx="0%"
 android:duration="2000">
 <!--
  浮点值 表示倍数 自身几倍
  fromxscale 动画在x轴以自身几倍伸缩开始
  toxscale 动画在x轴以自身几倍伸缩结束
  fromyscale 动画在y轴以自身几倍伸缩开始
  toyscale 动画在y轴以自身几倍伸缩结束
  pivotx 动画相对于控件自身的x坐标的开始位置
  pivoty 动画相对于控件自身的y坐标的开始位置
  0% 0% 表示控件左上角 为0,0原点坐标
 -->
</scale>

translateanimation:平移动画

<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
 android:fromxdelta="-100%p"
 android:fromydelta="0"
 android:toxdelta="100%p"
 android:toydelta="0"
 android:duration="2000">
 <!--
  fromxdelta x轴起始位置
  toxdelta x轴结束位置
  fromydelta y轴起始位置
  toydelta y轴结束位置
  100%p 表示相对于父级
  100%相对于自身
-->
</translate>

rotateanimation:旋转动画

<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
 android:fromdegrees="0"
 android:todegrees="360"
 android:duration="2000"
 android:pivotx="50%"
 android:pivoty="50%"
android:interpolator="@android:anim/accelerate_decelerate_interpolator">
 <!--
 interpolator 指定动画的插值器
 accelerate_decelerate_interpolator 加速-减速
 accelerate_interpolator  加速
 decelerate_interpolator  减速
 fromdegrees 动画起始角度
 todegrees 动画结束旋转的角度 可以大于360度
 负数表示逆时针旋转 正数表示顺时针旋转
 pivotx相对于view的x坐标的开始位置
 pivoty相对于view的y坐标的开始位置
绝对尺寸 100px
 50% 相对尺寸 相对于自身的50%
 50%p 相对尺寸 相对于父容器的50%
 50%为物件的x或y方向坐标上的中点位置
 duration 动画播放时间 单位毫秒
-->
</rotate>

通过构造方法创建 

构造参数详解  此段内容选自  

//在代码中定义 动画实例对象
private animation myanimation_alpha;
private animation myanimation_scale;
private animation myanimation_translate;
private animation myanimation_rotate;
 
 //根据各自的构造方法来初始化一个实例对象
myanimation_alpha=new alphaanimation(0.1f, 1.0f);
 
myanimation_scale =new scaleanimation(0.0f, 1.4f, 0.0f, 1.4f,
  animation.relative_to_self, 0.5f, animation.relative_to_self, 0.5f);
 
myanimation_translate=new translateanimation(30.0f, -80.0f, 30.0f, 300.0f);
 
myanimation_rotate=new rotateanimation(0.0f, +350.0f,
  animation.relative_to_self,0.5f,animation.relative_to_self, 0.5f);

alphaanimation

animationalphaanimation(float fromalpha, float toalpha)
//第一个参数fromalpha为 动画开始时候透明度
//第二个参数toalpha为 动画结束时候透明度
myanimation_alpha=new alphaanimation(0.1f, 1.0f);
//说明:
//  0.0表示完全透明
//  1.0表示完全不透明
myanimation_alpha.setduration(5000);
//设置时间持续时间为 5000毫秒

scaleanimation

scaleanimation(float fromx, float tox, float fromy, float toy,
   int pivotxtype, float pivotxvalue, int pivotytype, float pivotyvalue) 
 //第一个参数fromx为动画起始时 x坐标上的伸缩尺寸 
 //第二个参数tox为动画结束时 x坐标上的伸缩尺寸 
 //第三个参数fromy为动画起始时y坐标上的伸缩尺寸 
 //第四个参数toy为动画结束时y坐标上的伸缩尺寸 
 /*说明:
    以上四种属性值 
    0.0表示收缩到没有 
    1.0表示正常无伸缩 
    值小于1.0表示收缩 
    值大于1.0表示放大
 */
 //第五个参数pivotxtype为动画在x轴相对于物件位置类型 
 //第六个参数pivotxvalue为动画相对于物件的x坐标的开始位置
 //第七个参数pivotxtype为动画在y轴相对于物件位置类型 
 //第八个参数pivotyvalue为动画相对于物件的y坐标的开始位置
 myanimation_scale =new scaleanimation(0.0f, 1.4f, 0.0f, 1.4f,
   animation.relative_to_self, 0.5f, animation.relative_to_self, 0.5f);
 myanimation_scale.setduration(700);
 //设置时间持续时间为 700毫秒

translateanimation

translateanimation(float fromxdelta, float toxdelta,
    float fromydelta, float toydelta) 
 //第一个参数fromxdelta为动画起始时 x坐标上的移动位置 
 //第二个参数toxdelta为动画结束时 x坐标上的移动位置 
 //第三个参数fromydelta为动画起始时y坐标上的移动位置 
 //第四个参数toydelta为动画结束时y坐标上的移动位置 

rotateanimation

rotateanimation(float fromdegrees, float todegrees, 
   int pivotxtype, float pivotxvalue, int pivotytype, float pivotyvalue)
 //第一个参数fromdegrees为动画起始时的旋转角度 
 //第二个参数todegrees为动画旋转到的角度 
 //第三个参数pivotxtype为动画在x轴相对于物件位置类型 
 //第四个参数pivotxvalue为动画相对于物件的x坐标的开始位置
 //第五个参数pivotxtype为动画在y轴相对于物件位置类型 
 //第六个参数pivotyvalue为动画相对于物件的y坐标的开始位置
 myanimation_rotate=new rotateanimation(0.0f, +350.0f, animation.relative_to_self,0.5f,animation.relative_to_self, 0.5f);

属性动画

相对补间动画  属性动画会真正的使目标对象的属性值发生改变,不像补间动画只是影像的改变    只能修改具有get/set方法的属性值

因为可以修改对象的属性,属性动画可以做到更多的效果,改变文本大小,背景颜色等等

属性动画创建在 res/animator

valueanimator

包含属性动画的所有核心功能,动画时间,开始、结束属性值,属性值计算方法等。

valuanimiator设置开始结束值 实现valueanimator.onupdatelistener接口,

这个接口只有一个函数onanimationupdate(),在这个函数中会传入valueanimator对象做为参数,通过这个valueanimator对象的getanimatedvalue()函数可以得到当前的属性值

把属性值设置给某个控件的某个属性

使用xml

<?xml version="1.0" encoding="utf-8"?>
<animator xmlns:android="http://schemas.android.com/apk/res/android"
 android:duration="1000"
 android:valuefrom="0"
 android:valueto="300"
 android:valuetype="inttype"
android:interpolator="@android:interpolator/overshoot">
 <!--
 valuefrom 起始值
 valueto 结束值
 valuetype 值的类型
 inttype整数值、floattype浮点值、colortype颜色值
 interpolator插值器
 -->
</animator>
valueanimator valueanimator=null;
 //通过animatorinflater.loadanimator()加载xml 创建valueanimator
 valueanimator= (valueanimator) animatorinflater.loadanimator(this,r.animator.animator_value);
 //动画执行时间
 valueanimator.setduration(3000);
 //值改变监听
 valueanimator.addupdatelistener(listener);
 //开始动画
 valueanimator.start();
private valueanimator.animatorupdatelistener listener=new valueanimator.animatorupdatelistener() {
 @override
 public void onanimationupdate(valueanimator animation) {
       //获取值
  int value= (int) animation.getanimatedvalue();
       //btnvalueanimator为测试控件
        //设置控件x轴平移
        btnvalueanimator.settranslationx(value);
 }
 };

使用代码

/**
  * valueanimator 单个值
  */
 //代码创建 valueanimator类自身的方法
 //offloat值类型float
 valueanimator valueanimator=valueanimator.offloat(0,1);
 //ofint值类型int 从0~300
 valueanimator=valueanimator.ofint(0,300);
 //也可以用来设置颜色 在颜色改变过程中会将颜色变化情况显示出来
 //红色到蓝色的改变过程 显示n种颜色
 valueanimator=valueanimator.ofint(color.red,color.blue);
 //ofargb设置颜色 如果无法使用 是的sdk版本低了
 //这个方法改变颜色过程中只显示红色和蓝色
 //valueanimator=valueanimator.ofargb(color.red,color.blue);
 //设置插值器
 valueanimator.setinterpolator(new cycleinterpolator());
 /**
  *
  * valueanimator.ofpropertyvaluesholder 设置多个值
  */
 //设置动画属性 参数1:名字 参数2,3值的变化区间
 propertyvaluesholder alphaholder=propertyvaluesholder.offloat("alpha",0f,1f);
 propertyvaluesholder widthholder=propertyvaluesholder.ofint("width",0,300);
 //valueanimator.ofpropertyvaluesholder 添加holder 创建动画
valueanimator=valueanimator.ofpropertyvaluesholder(alphaholder,widthholder);
 //动画执行时间
 valueanimator.setduration(3000);
 //值改变监听
 valueanimator.addupdatelistener(listener);
private valueanimator.animatorupdatelistener listener=new valueanimator.animatorupdatelistener() {
 @override
 public void onanimationupdate(valueanimator animation) {
  /**
  * 单个值获取 getanimatedvalue取出变化值 根据设置类型强转
  * btnvalueanimator 测试用的button
  */
//  int value= (int) animation.getanimatedvalue();
//  btnvalueanimator.settranslationx(value);横坐标平移
//  float value= (float) valueanimator.getanimatedvalue();
//  btnvalueanimator.setalpha(value);透明度改变
//  int value= (int) animation.getanimatedvalue();
//  btnvalueanimator.settextcolor(value);文字颜色改变
  /**
  * propertyvaluesholder存了多个值 通过名字获取 强制转换
  */
  float alpha= (float) valueanimator.getanimatedvalue("alpha");
  int width= (int) valueanimator.getanimatedvalue("width");
  btnvalueanimator.setalpha(alpha);//改变透明度
  //图像绘制 左边不变从右边慢慢增加
  //修改控件的width height不能使用setwidth或setheight
  btnvalueanimator.setright(width);
  //btnvalueanimator.setbottom(width);

 }
 };

objectanimator:

继承自valueanimator,要指定一个对象及该对象的一个属性,当属性值计算完成时自动设置为该对象的相应属性,不需要设置监听,底层自动完成,一般会用objectanimator来改变某一对象的某一属性

//用来测试的button
 button btnobjectanimator= (button) findviewbyid(r.id.btn_object_animator);
 //加载动画
 objectanimator objectanimator= (objectanimator) animatorinflater.loadanimator(this,r.animator.animator_object);
 //绑定控件
 objectanimator.settarget(btnobjectanimator);
 //参数1 绑定控件 参数2 设置 属性 参数3 设置值
 objectanimator=objectanimator.ofint(btnobjectanimator,"textcolor",color.red);
 //propertyvaluesholder设置多个属性
 propertyvaluesholder translationxholder=propertyvaluesholder.offloat("translationx",0,300);
 propertyvaluesholder translationyholder=propertyvaluesholder.offloat("translationy",0,200);
objectanimator=objectanimator.ofpropertyvaluesholder(btnobjectanimator,translationxholder,translationyholder);
 objectanimator.setduration(3000);
     //开始动画
     objectanimator.start();

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持移动技术网!

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网