Android 动画 | 青训营笔记

192 阅读3分钟

这是我参与「第四届青训营 」笔记创作活动的第二天

本文用于记录我在青训营中了解的有关android动画的知识。

帧动画

帧动画,顾名思义就是以帧来计算的动画,由程序员定义好每一帧所需要展示的画面与展示时间,再由系统进行调用,示例如下:

<?xml version="1.0" encoding="utf-8"?> 
<animation-list 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:oneshot="false"> 
    <item android:drawable="@drawable/ic_wifi_0" android:duration="100"/>
    <item android:drawable="@drawable/ic_wifi_1" android:duration="100"/> 
    <item android:drawable="@drawable/ic_wifi_2" android:duration="100"/> 
    <item android:drawable="@drawable/ic_wifi_3" android:duration="100"/>
    <item android:drawable="@drawable/ic_wifi_4" android:duration="100"/>
    <item android:drawable="@drawable/ic_wifi_5" android:duration="100"/> 
</animation-list>

我们在资源文件中定义了一个图片列表,并且为每一张图片设置好了持续时间,这样在系统调用该资源时,会依次展示不同的图片,并在持续时间结束后展示下一张图片,那么如何在代码中调用该动画呢?

mImageView.setImageResource(R.drawable.frame_anim); 
AnimationDrawable animationDrawable = (AnimationDrawable) mImageView.getDrawable();
animationDrawable.start(); ... animationDrawable.stop();

补间动画

顾名思义,由系统补充中间状态的动画,在程序员给定了初始状态和最终状态以及持续时间,由系统自定计算得出每一帧需要展示的内容。

一般用于描述控件状态的动画,比如透明度、控件位置等

使用案例:

public void tweenedAnimation(View view) { 
    // 创建一个透明度动画,透明度从1渐变至0 
    AlphaAnimation alphaAnimation = new AlphaAnimation(1, 0); 
    alphaAnimation.setDuration(3000); 
    // 创建一个旋转动画,从0度旋转至360度 
    RotateAnimation rotateAnimation = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f,Animation.RELATIVE_TO_SELF, 0.5f); 
    rotateAnimation.setDuration(3000); 
    ScaleAnimation scaleAnimation = new ScaleAnimation(1, 0.5f, 1, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); 
    scaleAnimation.setDuration(3000); 
    TranslateAnimation translateAnimation = new TranslateAnimation( Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 1, Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 1); 
    translateAnimation.setDuration(3000);
    // 组合上述4种动画 
    AnimationSet animationSet = new AnimationSet(true); 
    animationSet.addAnimation(alphaAnimation); 
    animationSet.addAnimation(rotateAnimation); 
    animationSet.addAnimation(scaleAnimation); 
    animationSet.addAnimation(translateAnimation); 
    view.startAnimation(animationSet); 
}

image.png

属性动画

除了一些资源文件可以做动画,我们自定义的类可以不可以做动画呢?答案是,当然可以,这就是属性动画,可以为我们自定义的类设置一个动画效果,其“属性”一词就代表这对象的属性。

其实现原理就是不断地给属性进行赋值,以达到一个运动地效果。

private void startObjectAnimatorSet() {

     // 创建一个ObjectAnimator,将mImageView的scaleX属性值从1变化到0.5
    Animator scaleXAnimator = ObjectAnimator.ofFloat(mImageView, "scaleX", 1, 0.5f); 
    scaleXAnimator.setDuration(2000);  

     // 创建一个ObjectAnimator,将mImageView的scaleY属性值从1变化到0.5
    Animator scaleYAnimator = ObjectAnimator.ofFloat(mImageView, "scaleY", 1, 0.5f); 
    scaleYAnimator.setDuration(2000);  

     // 创建一个ObjectAnimator,将mImageView的rotationX属性值从0变化到360
    Animator rotationXAnimator = ObjectAnimator.ofFloat(mImageView, "rotationX", 0, 360);
    rotationXAnimator.setDuration(2000); 

     // 创建一个ObjectAnimator,将mImageView的rotationY属性值从0变化到360
    Animator rotationYAnimator = ObjectAnimator.ofFloat(mImageView, "rotationY", 0, 360);
    rotationYAnimator.setDuration(2000); 

    // 组合上述4种动画
    AnimatorSet animatorSet = new AnimatorSet(); 
    animatorSet.play(scaleXAnimator).with(scaleYAnimator)
    .before(rotationXAnimator).after(rotationYAnimator); 
    animatorSet.start(); 
}

image.png

Motion Layout

由Android提供地较为强大的管理动画的布局,其在官方文档中是这样介绍的:

MotionLayout 是一种布局类型,可帮助您管理应用中的运动和微件动画。MotionLayout 是 ConstraintLayout 的子类,在其丰富的布局功能基础之上构建而成。

通过使用Motion Layout可以非常方便的实现一些简单的动画。其使用方法可以参照官方文档:https://developer.android.com/training/constraint-layout/motionlayout

ps:如果无法科学上网,可以把链接中的.com换成.google.cn。