Android 动画汇总-自定义动画

android自定义动画步骤

继承Animation,重写里面的initialize和applyTransformation,在initialize方法做一些初始化的工作,在applyTransformation方法进行相应的轨阵变化即可!

参数讲解

initialize(int width, int height, int parentWidth, int parentHeight) width和height代表指定播放动画的View控件宽高,parentWidth和parentHeight代表该View控件所在的父控件宽高。

applyTransformation(float interpolatedTime, Transformation t) 其中的interpolatedTime参数都会改变一次,值从0到1递增,当interpolatedTime的值为1时则动画结束。 Transformatio类是一个变换的矩阵,通过改变该矩阵就可以实现各种复杂的效果。

applyTransformation()方法是动画具体的实现方法,在系统绘制动画时会反复调用这个方法,每调用一次

实例代码

在**自定义折叠布局**中就是用了自定义动画。

下面我们看一个实现左右旋转摇摆的例子

/**
 * 左右摇摆动画
 */
public class RotateAnim extends Animation {
 
  /** 控件宽 */
  private int mWidth;
 
  /** 控件高 */
  private int mHeight;
 
  /** 实例 */
  private static RotateAnim rotateAnim;
 
  /**
   * 获取动画实例
   * @return 实例
   */
  public static RotateAnim getRotateAnim() {
    if (null == rotateAnim) {
      rotateAnim = new RotateAnim();
    }
    return rotateAnim;
  }
 
//获取视图的宽高
  @Override
  public void initialize(int width, int height, int parentWidth, int parentHeight) {
    this.mWidth = width;
    this.mHeight = height;
    super.initialize(width, height, parentWidth, parentHeight);
  }
 
  @Override
  protected void applyTransformation(float interpolatedTime, Transformation t) {
    // 左右摇摆
    t.getMatrix().setRotate((float)(Math.sin(interpolatedTime*Math.PI*2)*50), mWidth/2, mHeight/2);
    super.applyTransformation(interpolatedTime, t);
  }
}
 

外部调用

			RotateAnim rotateAnim = new RotateAnim.getRotateAnim();
			rotateAnim .setDuration(3000); 		
		       startAnimation(rotateAnim );

转载于:https://my.oschina.net/fltsp/blog/1528560

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值