欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

android Tween动画四种动画效果的实现步骤教程

程序员文章站 2022-12-11 08:19:02
tween animation:通过预先定义一组指令,指令指定了图形变幻的类型、触发时间、持续时间,对场景里的对象不断平移、缩放、旋转等变幻来产生动画效果。 animation是以xml格式定义的,...

tween animation:通过预先定义一组指令,指令指定了图形变幻的类型、触发时间、持续时间,对场景里的对象不断平移、缩放、旋转等变幻来产生动画效果。

animation是以xml格式定义的,定义好的xml文件放在"res/anim"目录中。

在xml文件中,tween动画主要包括以下四种动画效果:(箭头后是与之对应的java代码方法)

alpha:渐变透明-------------->alphaanimation

scale:伸缩 --------------->scaleanimation

translate:移动--------------->translateanimation

rotate:旋转 ---------------->rotateanimation

实现步骤:

1.定义animation对象

2.设置动画的属性

3.通过startanimation()方法开始动画

例子:

1.java代码:

public class mainactivity extends appcompatactivity implements view.onclicklistener{


 /**定义alpha动画*/
 private animation animalpha=null;

 /**定义scale动画*/
 private animation animscale=null;

 /**定义translate动画*/
 private animation animtranslate=null;

 /**定义rotate动画*/
 private animation animrotate=null;

 private imageview img;
 private textview alpha,scale,translate,rotate;

 @override
 protected void oncreate(bundle savedinstancestate) {
  super.oncreate(savedinstancestate);
  setcontentview(r.layout.activity_main);

  img=findviewbyid(r.id.iv_animation);
  alpha=findviewbyid(r.id.alpha);
  scale=findviewbyid(r.id.scale);
  translate=findviewbyid(r.id.translate);
  rotate=findviewbyid(r.id.rotate);

  alpha.setonclicklistener(this);
  scale.setonclicklistener(this);
  translate.setonclicklistener(this);
  rotate.setonclicklistener(this);
 }

 @override
 public void onclick(view v) {
  switch (v.getid()){
case r.id.alpha:
 /**创建alpha动画*/
 animalpha=new alphaanimation(0.1f,1.0f);
 /**设置动画时间*/
 animalpha.setduration(3000);
 /**开始播放动画*/
 img.startanimation(animalpha);
 break;
case r.id.scale:
 /**创建scale动画*/
 animscale=new scaleanimation(0.0f,1.0f,0.0f,1.0f,
animation.relative_to_self,animation.relative_to_self);
 /**设置动画时间*/
 animscale.setduration(5000);
 /**开始播放动画*/
 img.startanimation(animscale);
 break;
case r.id.translate:
 /**创建translate动画*/
 animtranslate=new translateanimation(10,100,10,100);
 /**设置动画时间*/
 animtranslate.setduration(1000);
 /**开始播放动画*/
 img.startanimation(animtranslate);
 break;
case r.id.rotate:
 /**创建translate动画*/
 animrotate=new rotateanimation(0.0f,+360.0f,
animation.relative_to_self,0.5f,
animation.relative_to_self,0.5f);
 /**设置动画时间*/
 animrotate.setduration(1000);
 /**开始播放动画*/
 img.startanimation(animrotate);
 break;
  }
 }
}

效果:
alpha: scale: 


translate:rotate:


=======================================================================================
/**
 * constructor to use when building an alphaanimation from code
 * 
 * @param fromalpha starting alpha value for the animation, where 1.0 means
 *  fully opaque and 0.0 means fully transparent.
 * @param toalpha ending alpha value for the animation.
 */
public alphaanimation(float fromalpha, float toalpha) {
 mfromalpha = fromalpha;
 mtoalpha = toalpha;
}
/**
 * constructor to use when building a scaleanimation from code
 * 
 * @param fromx horizontal scaling factor to apply at the start of the
 *  animation
 * @param tox horizontal scaling factor to apply at the end of the animation
 * @param fromy vertical scaling factor to apply at the start of the
 *  animation
 * @param toy vertical scaling factor to apply at the end of the animation
 * @param pivotx the x coordinate of the point about which the object is
 *  being scaled, specified as an absolute number where 0 is the left
 *  edge. (this point remains fixed while the object changes size.)
 * @param pivoty the y coordinate of the point about which the object is
 *  being scaled, specified as an absolute number where 0 is the top
 *  edge. (this point remains fixed while the object changes size.)
 */
public scaleanimation(float fromx, float tox, float fromy, float toy,
  float pivotx, float pivoty) {
 mresources = null;
 mfromx = fromx;
 mtox = tox;
 mfromy = fromy;
 mtoy = toy;

 mpivotxtype = absolute;
 mpivotytype = absolute;
 mpivotxvalue = pivotx;
 mpivotyvalue = pivoty;
 initializepivotpoint();
}
=====================================================================================
/**
 * constructor to use when building a translateanimation from code
 *
 * @param fromxdelta change in x coordinate to apply at the start of the
 *  animation
 * @param toxdelta change in x coordinate to apply at the end of the
 *  animation
 * @param fromydelta change in y coordinate to apply at the start of the
 *  animation
 * @param toydelta change in y coordinate to apply at the end of the
 *  animation
 */
public translateanimation(float fromxdelta, float toxdelta, float fromydelta, float toydelta) {
 mfromxvalue = fromxdelta;
 mtoxvalue = toxdelta;
 mfromyvalue = fromydelta;
 mtoyvalue = toydelta;

 mfromxtype = absolute;
 mtoxtype = absolute;
 mfromytype = absolute;
 mtoytype = absolute;
}
/**
 * constructor to use when building a rotateanimation from code
 * 
 * @param fromdegrees rotation offset to apply at the start of the
 *  animation.
 * 
 * @param todegrees rotation offset to apply at the end of the animation.
 * 
 * @param pivotxtype specifies how pivotxvalue should be interpreted. one of
 *  animation.absolute, animation.relative_to_self, or
 *  animation.relative_to_parent.
 * @param pivotxvalue the x coordinate of the point about which the object
 *  is being rotated, specified as an absolute number where 0 is the
 *  left edge. this value can either be an absolute number if
 *  pivotxtype is absolute, or a percentage (where 1.0 is 100%)
 *  otherwise.
 * @param pivotytype specifies how pivotyvalue should be interpreted. one of
 *  animation.absolute, animation.relative_to_self, or
 *  animation.relative_to_parent.
 * @param pivotyvalue the y coordinate of the point about which the object
 *  is being rotated, specified as an absolute number where 0 is the
 *  top edge. this value can either be an absolute number if
 *  pivotytype is absolute, or a percentage (where 1.0 is 100%)
 *  otherwise.
 */
public rotateanimation(float fromdegrees, float todegrees, int pivotxtype, float pivotxvalue,
  int pivotytype, float pivotyvalue) {
 mfromdegrees = fromdegrees;
 mtodegrees = todegrees;

 mpivotxvalue = pivotxvalue;
 mpivotxtype = pivotxtype;
 mpivotyvalue = pivotyvalue;
 mpivotytype = pivotytype;
 initializepivotpoint();
}