【Android 动画】动画详解之补间动画(一)

前言

之前很早就想写写Android 的动画,最近刚好有时间,大概聊一聊安卓动画。

个人习惯将动画分为:补间动画(透明度、旋转、位移、缩放)、帧动画、和属性动画,这一篇,我们先说说补间动画。
补间动画这个词出于flash,在两个关键帧(可以理解成动画开始和结束)中间需要做“补间动画”,才能实现图画的运动;插入补间动画后两个关键帧之间的插补帧是由计算机自动运算而得到的。
实际上,Android 的补间动画也是由我们指定动画开始、动画结束2个关键点,中间部分的动画由系统完成

基础坐标

在正式开始之前,我们先说下Android 系统的坐标系,屏幕左上角为坐标原点,假如屏幕为1080*1980,那么左上角为(0,0),右上角为(1080,0),左下角为(0,1980),右下角为(1080,1980)


image.png

1.公共属性

所有动画有以下公共属性,注释比较详细,这里就不在详述了

               //动画持续时间
                scaleAnimation.setDuration(2000);
                //如果设置为true,控件动画结束时,将保持动画最后时的状态
                scaleAnimation.setFillAfter(true);
                //如果设置为true,控件动画结束时,还原到开始动画前的状态
                scaleAnimation.setFillBefore(false);
                //重复次数
                scaleAnimation.setRepeatCount(2);
                //重复类型,有reverse和restart两个值,reverse表示倒序回放,restart表示重新放一遍,必须与repeatCount一起使用
                scaleAnimation.setRepeatMode(Animation.RESTART);

2.缩放动画(ScaleAnimation)

ScaleAnimation有3种构造方法

 public ScaleAnimation(float fromX, float toX, float fromY, float toY) {
  }
 public ScaleAnimation(float fromX, float toX, float fromY, float toY,
            float pivotX, float pivotY) {
  }
public ScaleAnimation(float fromX, float toX, float fromY, float toY,
            int pivotXType, float pivotXValue, int pivotYType, float pivotYValue) {
  }

我们先看第一种,其起始比例为0,缩放比例为1.4,即放大到1.4倍

                //fromXScale    起始的X方向上相对自身的缩放比例,浮点值,比如1.0代表自身无变化,0.5代表起始时缩小一倍,2.0代表放大一倍;
                //toXScale        结尾的X方向上相对自身的缩放比例,浮点值;
                //fromYScale    起始的Y方向上相对自身的缩放比例,浮点值
                //toYScale        结尾的Y方向上相对自身的缩放比例,浮点值
                scaleAnimation = new ScaleAnimation(0, 1.4f, 0, 1.4f);
                //动画持续时间
                scaleAnimation.setDuration(2000);
            
                tvDemo.startAnimation(scaleAnimation);

效果如下:


scale放大1.4倍.gif

第二种,pivotx,pivotY分别代表起始位置的x、y方向的坐标,我们设置为(100,100)

               //pivotx,pivotY 动画起始位置+100
                scaleAnimation = new ScaleAnimation(0, 1.4f, 0, 1.4f, 100, 100);
                scaleAnimation.setDuration(2000);
                tvDemo.startAnimation(scaleAnimation);

效果如下:


scale起始位置+100.gif

第三种,pivotXType和pivotYType有2种模式,RELATIVE_TO_SELF(相对于自身)和RELATIVE_TO_PARENT(相对于父布局),如果设置这个,pivotx,pivotY的值就应该是0-1的浮点数,这里分别对应xml中的%(自身)和%p(父布局)

  • 1.设置为RELATIVE_TO_SELF
             //如果是50%(代码0.5),表示在当前控件的左上角加上自己宽度的50%做为起始点
                scaleAnimation = new ScaleAnimation(0, 1.4f, 0, 1.4f, ScaleAnimation.RELATIVE_TO_SELF,
                        0.5f, ScaleAnimation.RELATIVE_TO_SELF, 0.5f);
                scaleAnimation.setDuration(2000);
                tvDemo.startAnimation(scaleAnimation);
scale自身50.gif
  • 2.设置为RELATIVE_TO_PARENT
 //如果是50%p(代码0.5),那么就是表示在当前的左上角加上父控件宽度的50%做为起始点x轴坐标。
                scaleAnimation = new ScaleAnimation(0, 1.4f, 0, 1.4f, ScaleAnimation.RELATIVE_TO_PARENT,
                        0.5f, ScaleAnimation.RELATIVE_TO_PARENT, 0.5f);
                scaleAnimation.setDuration(2000);
                tvDemo.startAnimation(scaleAnimation);
scale父布局50.gif

3.位移动画(TranslateAnimation)

TranslateAnimation有2种构造方法,和ScaleAnimation类似

 public TranslateAnimation(float fromXDelta, float toXDelta, float fromYDelta, float toYDelta) {
  }
 public TranslateAnimation(int fromXType, float fromXValue, int toXType, float toXValue,
            int fromYType, float fromYValue, int toYType, float toYValue) {
  }
  • 1.fromXDelta为起始点X轴坐标;fromYDelta 为起始点Y轴从标;toXDelta为结束点X轴坐标;toYDelta为 结束点Y轴坐标
                //fromXDelta     起始点X轴坐标,可以是数值、百分数、百分数p 三种样式,同scale
                //fromYDelta    起始点Y轴从标,可以是数值、百分数、百分数p 三种样式
                //toXDelta         结束点X轴坐标
                //toYDelta        结束点Y轴坐标   
                translateAnimation = new TranslateAnimation(0, 100, 0, 100);
                translateAnimation.setDuration(2000);
                tvDemo.startAnimation(translateAnimation);

效果如下:


translate100.gif
  • 2.fromYType、toYType同ScaleAnimation,先来看RELATIVE_TO_SELF
 translateAnimation = new TranslateAnimation(TranslateAnimation.RELATIVE_TO_SELF, 0, TranslateAnimation.RELATIVE_TO_SELF, 0.5f,
                        TranslateAnimation.RELATIVE_TO_SELF, 0, TranslateAnimation.RELATIVE_TO_SELF, 0.5f);
                translateAnimation.setDuration(2000);
                tvDemo.startAnimation(translateAnimation);

效果如下:


translate自身50.gif

RELATIVE_TO_PARENT

 translateAnimation = new TranslateAnimation(TranslateAnimation.RELATIVE_TO_PARENT, 0, TranslateAnimation.RELATIVE_TO_PARENT, 0.5f,
                        TranslateAnimation.RELATIVE_TO_PARENT, 0, TranslateAnimation.RELATIVE_TO_PARENT, 0.5f);
                translateAnimation.setDuration(2000);
                tvDemo.startAnimation(translateAnimation);

效果如下:


translate父布局50.gif

4.旋转动画(RotateAnimation)

RotateAnimation有3种构造方法

 public RotateAnimation(float fromDegrees, float toDegrees) {
  }
  public RotateAnimation(float fromDegrees, float toDegrees, float pivotX, float pivotY) {
  }
  public RotateAnimation(float fromDegrees, float toDegrees, int pivotXType, float pivotXValue,
            int pivotYType, float pivotYValue) {
  }
  • 1.fromDegrees 为开始旋转的角度,正值代表顺时针方向度数,负值代码逆时针方向度数,toDegrees为结束时旋转角度,取值同fromDegrees

顺时针720度

                rotateAnimation = new RotateAnimation(0, 720, 0, 0);
                rotateAnimation.setDuration(2000);
                tvDemo.startAnimation(rotateAnimation);

效果如下:


rotate顺720.gif

逆时针720度

                 rotateAnimation = new RotateAnimation(0, -720, 0, 0);
                rotateAnimation.setDuration(2000);
                tvDemo.startAnimation(rotateAnimation);

效果如下:


rotate逆720.gif
  • 2.pivotXType、pivotYType同ScaleAnimation,先看RELATIVE_TO_SELF
             rotateAnimation = new RotateAnimation(0, -720, RotateAnimation.RELATIVE_TO_SELF, 0.5f,
                        RotateAnimation.RELATIVE_TO_SELF, 0.5f);
                rotateAnimation.setDuration(2000);
                tvDemo.startAnimation(rotateAnimation);

效果如下:


rotate自身720.gif

再来RELATIVE_TO_PARENT

              rotateAnimation = new RotateAnimation(0, -720, RotateAnimation.RELATIVE_TO_PARENT, 0.5f,
                        RotateAnimation.RELATIVE_TO_PARENT, 0.5f);
                rotateAnimation.setDuration(2000);
                tvDemo.startAnimation(rotateAnimation);

效果如下:


rotate父布局720.gif

这是什么鬼???怎么跑到屏幕外面去了?
原来设置为RELATIVE_TO_PARENT时,旋转中心x方向应该为该空间离左边的边距+父布局宽度/2,y方向同理,而此时,我们布局中红色的Textview为居中状态,所以旋转中心为屏幕右下角。让我们来看个例子
修改布局如下:

 <TextView
        android:id="@+id/tv_demo"
        android:layout_width="10dp"
        android:layout_height="160dp"
        android:layout_marginLeft="100dp"
        app:layout_constraintLeft_toLeftOf="parent"
        android:background="@color/colorAccent" />


    <TextView
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="@color/colorAccent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:layout_width="1dp"
        android:layout_height="match_parent"
        android:layout_marginLeft="100dp"
        android:background="@color/colorAccent"
        app:layout_constraintLeft_toLeftOf="parent" />
                rotateAnimation = new RotateAnimation(0, -720, RotateAnimation.RELATIVE_TO_PARENT, 0f,
                        RotateAnimation.RELATIVE_TO_PARENT, 0.5f);
                rotateAnimation.setDuration(2000);
                tvDemo.startAnimation(rotateAnimation);

效果如下:


rotate100dp.gif

这时,我们看到旋转中心x方向为离左边100dp处

5.透明度动画(AlphaAnimation)

AlphaAnimation只有1种构造方法

 public AlphaAnimation(float fromAlpha, float toAlpha) {
  }

其中fromAlpha为动画开始的透明度;toAlpha为动画结束的透明度

                //fromAlpha   动画开始的透明度,从0.0 --1.0 ,0.0表示全透明,1.0表示完全不透明
                //toAlpha       动画结束时的透明度,也是从0.0 --1.0 ,0.0表示全透明,1.0表示完全不透明
                alphaAnimation = new AlphaAnimation(0, 1);
                alphaAnimation.setDuration(2000);
                tvDemo.startAnimation(alphaAnimation);

效果如下:


alpha渐显.gif
                alphaAnimation = new AlphaAnimation(1, 0);
                alphaAnimation.setDuration(2000);
                tvDemo.startAnimation(alphaAnimation);

效果如下:


alpha渐消失.gif

6.动画集合(AnimationSet)

AnimationSet是一个动画的集合,可以按照添加的顺序播放动画,让我们来看个例子,通过组合动画,实现旋转渐入动画

 rotateAnimation = new RotateAnimation(0, -720, RotateAnimation.RELATIVE_TO_SELF, 0.5f,
                        RotateAnimation.RELATIVE_TO_SELF, 0.5f);
                rotateAnimation.setDuration(2000);

                translateAnimation = new TranslateAnimation(TranslateAnimation.RELATIVE_TO_PARENT, 0, TranslateAnimation.RELATIVE_TO_PARENT, 0.5f,
                        TranslateAnimation.RELATIVE_TO_PARENT, 0, TranslateAnimation.RELATIVE_TO_PARENT, 0.5f);
                translateAnimation.setDuration(2000);

                scaleAnimation = new ScaleAnimation(0, 1.4f, 0, 1.4f, ScaleAnimation.RELATIVE_TO_SELF,
                        0.5f, ScaleAnimation.RELATIVE_TO_SELF, 0.5f);
                scaleAnimation.setDuration(2000);

                alphaAnimation = new AlphaAnimation(0, 1);
                alphaAnimation.setDuration(2000);


                animationSet = new AnimationSet(true);
                animationSet.addAnimation(rotateAnimation);
                animationSet.addAnimation(translateAnimation);
                animationSet.addAnimation(scaleAnimation);
                animationSet.addAnimation(alphaAnimation);
                animationSet.setDuration(4000);
                animationSet.setFillAfter(true);


                tvDemo.startAnimation(animationSet);

效果如下:


旋转渐入.gif

总结

到这里,补间动画就介绍完了

最后献上源码 github

参考资料:自定义控件三部曲之动画篇

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,847评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,208评论 1 292
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,587评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,942评论 0 205
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,332评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,587评论 1 218
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,853评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,568评论 0 198
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,273评论 1 242
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,542评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,033评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,373评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,031评论 3 236
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,073评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,830评论 0 195
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,628评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,537评论 2 269

推荐阅读更多精彩内容