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动画(补间动画)的介绍,如果对你有所帮助,请点喜欢哦!

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容