Android动画之补间动画(TweenAnimation)

前言

之前做开发的时候也会用到动画,用搜索引擎查询基本也能满足需求,但是为了进步,是时候总结梳理一波了,于是就有了下文!

点击此处获得本文的代码github

效果图

效果图.gif

1.补间动画的分类

1.AlphaAnimation(渐变)
2.TranslateAnimation(平移)
3.ScaleAnimation(缩放)
4.RotateAnimation(旋转)
5.AnimationSet(组合)

补间动画分类.png

2.了解Interpolator

用来控制动画的变化速度,可以自行实现Interpolator接口来控制,也可以使用官方提供的接口来控制,这个我们一般都是在xml里面使用,属性是:android:interpolator,具体看下面的xml代码

  • LinearInterpolator:动画以均匀的速度改变

  • AccelerateInterpolator:在动画开始的地方改变速度较慢,然后开始加速

  • AccelerateDecelerateInterpolator:在动画开始、结束的地方改变速度较慢,中间时加速

  • CycleInterpolator:动画循环播放特定次数,变化速度按正弦曲线改变: Math.sin(2 * mCycles * Math.PI * input)

  • DecelerateInterpolator:在动画开始的地方改变速度较快,然后开始减速

  • AnticipateInterpolator:反向,先向相反方向改变一段再加速播放

  • AnticipateOvershootInterpolator:开始的时候向后然后向前甩一定值后返回最后的值

  • BounceInterpolator: 跳跃,快到目的值时值会跳跃,如目的值100,后面的值可能依次为85,77,72,83,90,100

  • OvershottInterpolator:回弹,最后超出目的值然后缓慢改变到目的值

3.实现代码

首先在项目的res创建anim文件夹,然后就是创建各个动画的xml文件(如图)

  • 第1步


    1.png
  • 第2步


    2.png

1.渐变alpha.xml

<alpha xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/accelerate_decelerate_interpolator"
    android:duration="3000"
    android:fromAlpha="1.0"
    android:toAlpha="0.1"
    />
    <!--
    duration:动画时长
    fromAlpha :起始透明度
    toAlpha:结束透明度
    透明度的范围为:0-1,完全透明-完全不透明
    -->

2.平移translate.xml

<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/accelerate_decelerate_interpolator"
    android:fromXDelta="0"
    android:toXDelta="300"
    android:fromYDelta="0"
    android:toYDelta="0"
    android:duration="3000"/>
    <!--
     duration:动画时长
     fromXDelta/fromYDelta:动画起始位置的X/Y坐标
     toXDelta/toYDelta:动画结束位置的X/Y坐标
     -->

3.缩放scale.xml

<scale xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/accelerate_interpolator"
    android:fromXScale="0.5"
    android:toXScale="1.5"
    android:fromYScale="0.5"
    android:toYScale="1.5"
    android:pivotX="50%"
    android:pivotY="50%"
    android:duration="2000"/>

    <!--
      duration:动画时长
      fromXScale/fromYScale:沿着X轴/Y轴缩放的起始比例
      toXScale/toYScale:沿着X轴/Y轴缩放的结束比例
      pivotX/pivotY:缩放的中轴点X/Y坐标,即距离自身左边缘的位置,比如50%就是以图像的 中心为中轴点
     -->

4.旋转rotate.xml

<rotate xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/accelerate_decelerate_interpolator"
    android:duration="1000"
    android:fromDegrees="0"
    android:repeatCount="1"
    android:repeatMode="reverse"
    android:toDegrees="360" />

    <!--
        duration:动画时长
        fromDegrees/toDegrees:旋转的起始/结束角度
        repeatCount:旋转的次数,默认值为0,代表一次,假如是其他值,
        比如3,则旋转4次 另外,值为-1或者infinite时,表示动画永不停止
        repeatMode:设置重复模式,默认restart,但只有当repeatCount大于0或者infinite或-1时 才有效。
        还可以设置成reverse,表示偶数次显示动画时会做方向相反的运动!
         -->

5.组合set.xml

<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/decelerate_interpolator"
    android:shareInterpolator="true" >
    <!-- 缩放-->
    <scale
        android:duration="2000"
        android:fromXScale="0.5"
        android:fromYScale="0.5"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toXScale="1.5"
        android:toYScale="1.5" />
    <!-- 旋转-->
    <rotate
        android:duration="2000"
        android:fromDegrees="0"
        android:repeatCount="1"
        android:repeatMode="reverse"
        android:toDegrees="360" />
    <!-- 平移-->
    <translate
        android:duration="2000"
        android:fromXDelta="0"
        android:fromYDelta="0"
        android:toXDelta="-300"
        android:toYDelta="0" />
    <!-- 渐变-->
    <alpha
        android:duration="2000"
        android:fromAlpha="1.0"
        android:toAlpha="0.1" />
</set>

TweenAnimation类的代码如下:

package android.chm.com.animation.Activity;

import android.chm.com.animation.R;
import android.chm.com.animation.Utils.T;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;

/**
 * Created by admin-chen on 2017/4/13.
 * 补间动画
 */
public class TweenAnimation extends AppCompatActivity implements View.OnClickListener {
    private static final String TAG="TweenAnimation";

    ImageView img;
    Button bt1, bt2, bt3, bt4,bt5;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tween);
        initview();
    }

    private void initview() {
        img = (ImageView) findViewById(R.id.img1);
        bt1 = (Button) findViewById(R.id.bt1);
        bt2 = (Button) findViewById(R.id.bt2);
        bt3 = (Button) findViewById(R.id.bt3);
        bt4 = (Button) findViewById(R.id.bt4);
        bt5 = (Button) findViewById(R.id.bt5);
        bt1.setOnClickListener(this);
        bt2.setOnClickListener(this);
        bt3.setOnClickListener(this);
        bt4.setOnClickListener(this);
        bt5.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.bt1:
                mAnimation(img,R.anim.anim_alpha);//渐变
                break;
            case R.id.bt2:
                mAnimation(img,R.anim.anim_translate);//平移
                break;
            case R.id.bt3:
                mAnimation(img,R.anim.anim_scale);//缩放
                break;
            case R.id.bt4:
                mAnimation(img,R.anim.anim_rotate);//旋转
                break;
            case R.id.bt5:
                mAnimation(img,R.anim.anim_set);//旋转
                break;
            default:
                break;
        }
    }

    public void mAnimation(final ImageView img, int anim_type) {
        Animation animation = AnimationUtils.loadAnimation(this,anim_type);
        img.startAnimation(animation);
        //动画监听
        animation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
                //动画开始
                //T.showShort(TweenAnimation.this,"动画开始");
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                //动画结束
               // T.showShort(TweenAnimation.this,"动画结束");
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
               //动画重复
                T.showShort(TweenAnimation.this,"动画重复");
            }
        });
    }
}

4.扩展

1.上面的代码对ImageView设置了动画,当然也可以对TextView、Button等view控件都设置动画

 Animation animation = AnimationUtils.loadAnimation(this,R.anim.xxxx);
 view.startAnimation(animation);

2.Fragment设置过渡动画
在Fragment的Transition里调用方法fragmentTransaction.setCustomAnimations()来设置左边的进入和退出,右边的进入和退出动画。
xml写法

    <!-- 平移有translationX和translationY平移-->
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <objectAnimator
        android:duration="@android:integer/config_mediumAnimTime"
        android:interpolator="@android:interpolator/decelerate_quint"
        android:propertyName="translationX"
        android:valueFrom="0dp"
        android:valueTo="-100dp"
        android:valueType="floatType" />
    <!-- 渐变-->
    <objectAnimator
        android:duration="@android:integer/config_mediumAnimTime"
        android:interpolator="@android:interpolator/decelerate_quint"
        android:propertyName="alpha"
        android:valueFrom="1.0"
        android:valueTo="0.0"
        android:valueType="floatType" />
</set>

java代码如下

  protected void addFragmentToStack(int index){  
        DetailFragment detail = DetailFragment.newInstance(index);
        FragmentTransaction ft = getFragmentManager().beginTransaction(); 
        fragmentTransaction.setCustomAnimations(R.animator.xxx,
        R.animator.xxx,
        R.animator.xxx,
        R.animator.xxx)//分别是左边进入、退出和右边进入、退出
        ft.replace(R.id.details, detail);        
        ft.addToBackStack("detail"); 
        ft.commit(); 
    }

以上就是Tween动画(补间动画)的介绍,如果对你有所帮助,请点喜欢哦!

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

推荐阅读更多精彩内容