Android动画之补间动画用法最全详解

在这里插入图片描述

补间动画概述和分类

在Android动画中,补间动画一共可以分成四类即透明度动画、缩放动画、旋转动画、位移动画。
其实现方法可以通过xml来配置,也可以通过代码来实现。

各类补间动画实现

xml实现补间动画

xml实现补间动画,需要将xml放到res下的anim目录,Android工程默认是没有anim文件夹的在读文件前我们先把anim 文件夹以及文件建好

  • 点中工程的res目录 右键New -> Directory -> 弹窗中输入anim
  • 点中刚刚新建的anim目录 右键New -> Animation Resource File
  • 创建好了以后输入如下的布局文件代码

透明度动画-AlphaAnimation

在这里插入图片描述

<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/accelerate_decelerate_interpolator"
    android:fromAlpha="1.0"
    android:toAlpha="0.1"
    android:duration="2000"/>

属性值 含义
fromAlpha 起始透明度(透明度的范围为:0-1,完全透明-完全不透明)
toAlpha 结束透明度
duration 持续时间(毫秒)

缩放动画-ScaleAnimation

在这里插入图片描述

<?xml version="1.0" encoding="utf-8"?>
<scale xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/accelerate_interpolator"
    android:fromXScale="0.2"
    android:toXScale="1.5"
    android:fromYScale="0.2"
    android:toYScale="1.5"
    android:pivotX="50%"
    android:pivotY="50%"
    android:duration="2000"/>
属性值 含义
fromXScale 沿着X轴缩放的起始比例
fromYScale 沿着X轴缩放的起始比例
toXScale 沿着X轴缩放的结束比例
toYScale 沿着Y轴缩放的结束比例
pivotX 缩放的中轴点X座标,即距离自身左边缘的位置,比如50%就是以图像的 中心为中轴点
pivotY 缩放的中轴点Y座标
duration 持续时间

位移动画-TranslateAnimation

在这里插入图片描述

<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/accelerate_decelerate_interpolator"
    android:fromXDelta="0"
    android:toXDelta="320"
    android:fromYDelta="0"
    android:toYDelta="0"
    android:duration="2000"/>
属性值 含义
fromXDelta 动画起始位置的X座标
fromYDelta 动画起始位置的Y座标
toXDelta 动画结束位置的X座标
toYDelta 动画结束位置的Y座标
duration 持续时间

旋转动画-RotateAnimation

在这里插入图片描述

<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/accelerate_decelerate_interpolator"
    android:fromDegrees="0"
    android:toDegrees="360"
    android:duration="1000"
    android:repeatCount="1"
    android:repeatMode="reverse"/>
属性值 含义
fromDegrees/toDegrees 旋转的起始/结束角度
repeatCount 旋转的次数,默认值为0,代表一次,假如是其他值,比如3,则旋转4次 另外,值为-1或者infinite时,表示动画永不停止
repeatMode 设置重复模式,默认restart,但只有当repeatCount大于0或者infinite或-1时 才有效。还可以设置成reverse,表示偶数次显示动画时会做方向相反的运动
duration 持续时间

动画组合-AnimationSet

在这里插入图片描述

<?xml version="1.0" encoding="utf-8"?>
<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.2"
        android:fromYScale="0.2"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toXScale="1.5"
        android:toYScale="1.5" />

    <rotate
        android:duration="1000"
        android:fromDegrees="0"
        android:repeatCount="1"
        android:repeatMode="reverse"
        android:toDegrees="360" />

    <translate
        android:duration="2000"
        android:fromXDelta="0"
        android:fromYDelta="0"
        android:toXDelta="320"
        android:toYDelta="0" />

    <alpha
        android:duration="2000"
        android:fromAlpha="1.0"
        android:toAlpha="0.1" />

</set>

添加布局文件代码
activity_main.xml 中添加5个按钮和一张图片

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#F44336"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:text="透明度"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:text="缩放"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/button" />

    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:text="位移"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/button2" />


    <Button
        android:id="@+id/button4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:text="旋转"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/button3" />


    <Button
        android:id="@+id/button5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:text="组合"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/button4" />

    <ImageView
        android:id="@+id/image"
        android:layout_width="100dp"
        android:layout_height="100dp"
      	android:background="@drawable/animation_flower"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_bias="0.497"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/button2"
        app:layout_constraintVertical_bias="0.76" />
</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.java 中响应点击事件

public class MainActivity extends AppCompatActivity {
    Button mButton1;
    Button mButton2;
    Button mButton3;
    Button mButton4;
    Button mButton5;
    Animation animation = null;
    ImageView mImageView;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mImageView = findViewById(R.id.image);
        mButton1 = findViewById(R.id.button);
        mButton1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Animation animation = AnimationUtils.loadAnimation(MainActivity.this,R.anim.anim_alpha);
                mImageView.startAnimation(animation);
            }
        });
        mButton2 = findViewById(R.id.button2);
        mButton2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                animation = AnimationUtils.loadAnimation(MainActivity.this,R.anim.anim_scale);
                mImageView.startAnimation(animation);
            }
        });
        mButton3 = findViewById(R.id.button3);
        mButton3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                animation = AnimationUtils.loadAnimation(MainActivity.this,R.anim.anim_translate);
                mImageView.startAnimation(animation);
            }
        });
        mButton4 = findViewById(R.id.button4);
        mButton4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                animation = AnimationUtils.loadAnimation(MainActivity.this,R.anim.anim_rotate);
                mImageView.startAnimation(animation);
            }
        });
        mButton5 = findViewById(R.id.button5);
        mButton5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                animation = AnimationUtils.loadAnimation(MainActivity.this,R.anim.anim_set);
                mImageView.startAnimation(animation);
            }
        });


    }
}

代码实现补间动画

用代码实现补间动画我们主要是通过构造Animation animation = new AlphaAnimation(); 拿到响应的引用以后去设置动画的一些属性。
这些动画的公共方法如下:

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

透明度动画(AlphaAnimation)

透明度动画参数最多的构造方法如下:

  //fromAlpha   动画开始的透明度,从0.0 --1.0 ,0.0表示全透明,1.0表示完全不透明
//toAlpha       动画结束时的透明度,也是从0.0 --1.0 ,0.0表示全透明,1.0表示完全不透明
 public AlphaAnimation(float fromAlpha, float toAlpha) {
 }

使用:

animation=  new AlphaAnimation(0, 1);
animation.setDuration(2000);
mImageView.startAnimation(animation);

在这里插入图片描述

缩放动画(ScaleAnimation)

我们先来看下缩放动画参数最多的构造方法

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

构造方法参数的含义同前面的布局文件一样

属性值 含义
fromX 沿着X轴缩放的起始比例
fromY 沿着X轴缩放的起始比例
toX 沿着X轴缩放的结束比例
toY 沿着Y轴缩放的结束比例
pivotXValue 缩放的中轴点X座标,即距离自身左边缘的位置,比如50%就是以图像的 中心为中轴点
pivotXType 有2种模式,RELATIVE_TO_SELF(相对于自身)和RELATIVE_TO_PARENT(相对于父布局)pivotx,pivotY的值就应该是0-1的浮点数,分别对应xml中的%(自身)和%p(父布局)
pivotYValue 缩放的中轴点Y座标,即距离自身左边缘的位置,比如50%就是以图像的 中心为中轴点
pivotYType 同pivotXType

填入构造方法的参数

      animation = new ScaleAnimation(0, 1.4f, 0, 1.4f,
                        ScaleAnimation.RELATIVE_TO_SELF,0.5f,ScaleAnimation.RELATIVE_TO_SELF,0.5f);
     animation.setDuration(2000);
      mImageView.startAnimation(animation);

效果如下:
在这里插入图片描述

位移动画(TranslateAnimation)

参数最多的构造方法如下:

 //fromXDelta     起始点X轴座标,可以是数值、百分数、百分数p 三种样式,同scale
 //fromYDelta    起始点Y轴从标,可以是数值、百分数、百分数p 三种样式
//toXDelta         结束点X轴座标
  //toYDelta        结束点Y轴座标 
 // fromYType、toYType同ScaleAnimation,
 public TranslateAnimation(int fromXType, float fromXValue, int toXType,
  float toXValue,int fromYType, float fromYValue, int toYType, float toYValue) {
  }

使用:

animation= new TranslateAnimation(TranslateAnimation.RELATIVE_TO_SELF, 0, TranslateAnimation.RELATIVE_TO_SELF, 0.5f,
        TranslateAnimation.RELATIVE_TO_SELF, 0, TranslateAnimation.RELATIVE_TO_SELF, 0.5f);
animation.setDuration(2000);
mImageView.startAnimation(animation);

效果如下:
在这里插入图片描述

旋转动画(RotateAnimation)

参数最多的构造方法如下:

public RotateAnimation(float fromDegrees, float toDegrees, int pivotXType,
 float pivotXValue, int pivotYType, float pivotYValue) {
 }

使用:

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

效果如下:
在这里插入图片描述

动画组合(AnimationSet)

通过AnimationSet可以将前面的四种动画组合在一起,实现一些混合动画效果

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

Animation 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);

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

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


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

在这里插入图片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章