android动画详解之一

来源:互联网 发布:淘宝dnf代练靠谱吗 编辑:程序博客网 时间:2024/05/18 13:05

1、概述
Animation的实现方式一般分为两种,一种是xml文件形式,另外一种是以代码的形式进行实现。首先,我们讲一下用代码形式实现的属性动画,Android提供了几种动画的类型,Property Animation,View Animation,以及Drawable Animation。View Animation是三种动画中最简单的,我们来看看官方的API对ViewAnimation 的描述:“You can use the view animation system to perform tweened animation on Views. Tween animation calculates the animation with information such as the start point, end point, size, rotation, and other common aspects of an animation. ”大致意思如下:在你的View中,你能够使用View Animation系统去支撑tween动画,Tween动画能够计算一些信息,例如开始点,结束点,大小,旋转度和一些其他动画共同相关的信息。总的来说,ViewAnimation相对简单,它只能支持旋转动画(RotateAnimation),缩放动画(ScaleAnimation),渐变动画(AlphaAnimation),以及平移动画(TranslateAnimation),局限性太大。

2、ViewAnimation又称做帧动画,在你的View 对象中,帧动画能够渲染出一系列的动作,如果你有一个TextView对象,你可以移动,旋转,缩放或者改变文本的透明度,如果你有一张背景图片,对这个背景图片可以做类似TextView对象的所有动画,在Animation package中,它提供了所有实现帧动画的类。

a) RotateAnimation: 旋转动画    b) AlphaAnimation:  渐变动画    c) ScaleAnimation:  缩放动画    d) TranslateAnimation:平移动画

3、先来看看RotateAnimation(旋转动画):通过查看API文档或者看源码我们可以知道,RotateAnimation有四个,这里就只说一下最长的构造方法,其他的都可以举一反三了。

 RotateAnimation rotateAnimation = new RotateAnimation(float fromDegrees, float toDegrees, int pivotXType, float pivotXValue,        int pivotYType, float pivotYValue);

参数:

  • fromDegrees: 开始度数
  • toDegrees: 结束度数
  • pivotXType :x轴上旋转的类型,值有Animation.ABSOLUTE(很少用到),Animation.RELATIVE_TO_SELF(自身旋转),Animation.RELATIVR_TO_PARENT(相对于父控件旋转)
  • pivotXValue: 指定旋转的X的坐标。
  • pivotYType:y轴上的旋转类型
  • pivotYValue:指定旋转的Y的坐标
    相关API解释:
    rotateAnimation.setDuration(1000);//设置动画持续时间    rotateAnimation.setFillAfter(true)://设置动画结束后是否需要保持动画状态

其他三个动画的具体实现和RotateAnimation大同小异,如果会用旋转动画,其他三类动画基本上也没什么问题了。

4、AnimationSet(动画插补器)
肯定有同学遇到过这样的情况,例如一款app的闪屏页面的开发,需要做动画特效,可能是一种动画,也可能是多种动画,当有多种动画的时候,我们怎样使多种动画同时生效呢?这就要用到我们的动画插补器了,

 AnimationSet set = new AnimationSet(); set.addAnimation(Anim1); set.addAnimation(Anim2); llRoot.startAnimation(set);

5、如果需要在动画结束的时候做出相应的动画,在动画监听器的回调方法中处理相应的逻辑就可以了。

set.AnimationListener(new Animation.AnimationListener()        {            @Override            public void onAnimationStart(Animation animation) {                //动画开始的回调函数            }            @Override            public void onAnimationEnd(Animation animation) {                //动画结束的回调函数            }            @Override            public void onAnimationRepeat(Animation animation) {                //动画重复的回调函数            }        });
0 0
原创粉丝点击