Android

关注公众号 jb51net

关闭
首页 > 软件编程 > Android > Android 补间动画组合AnimationSet

Android 补间动画及组合AnimationSet常用方法详解

作者:_小马快跑_

这篇文章主要为大家介绍了Android 补间动画及组合AnimationSet常用方法详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪

补间动画

Android常用的四种补间动画分别为RotateAnimationScaleAnimationTranslateAnimationAlphaAnimation,他们的父类为AnimationUML类图如下:

父类通用方法有:

animation.setAnimationListener(object : Animation.AnimationListener {
                override fun onAnimationEnd(animation: Animation?) {
                    //动画结束时回调,注意:当repeatCount设置为Animation.INFINITE不再收到该回调
                    log("onAnimationEnd")
                }
                override fun onAnimationStart(animation: Animation?) {
                    //动画开始时回调
                    log("onAnimationStart")
                }
                override fun onAnimationRepeat(animation: Animation?) {
                    //repeatCount设置为Animation.INFINITE时动画每执行一次该方法回调就会执行一次
                    log("onAnimationRepeat")
                }
            })

RotateAnimation

旋转动画,通过设置目标View的旋转中心、旋转角度来达到旋转目的。RotateAnimation中需要特殊设置的几个参数如下:

// RotateAnimation.java
public RotateAnimation(float fromDegrees, float toDegrees, int pivotXType, float pivotXValue,
            int pivotYType, float pivotYValue) {
    mFromDegrees = fromDegrees;
    mToDegrees = toDegrees;
    mPivotXValue = pivotXValue;
    mPivotXType = pivotXType;
    mPivotYValue = pivotYValue;
    mPivotYType = pivotYType;
    initializePivotPoint();
 }

Animation.ABSOLUTE:目标View的X轴坐标 = View左上角的原点 + pivotXValue数值的点(y方向同理)

Animation.RELATIVE_TO_SELF:目标View的X轴坐标 = View左上角的原点 + View自身宽度 * pivotXValue数值

Animation.RELATIVE_TO_PARENT:目标View的X轴坐标 = View左上角的原点 + View父控件宽度 * pivotXValue

动画示例

方式一:代码动态创建

val rotateAnim: Animation = RotateAnimation(
            0f,
            360f,
            Animation.RELATIVE_TO_SELF,
            0.5f,
            Animation.RELATIVE_TO_SELF,
            0.5f
        )
rotateAnim.duration = 2000
rotateAnim.repeatCount = Animation.INFINITE
rotateAnim.interpolator = LinearInterpolator()
rotateAnim.fillAfter = true
mTvTarget.animation = rotateAnim

方式二:XML中设置

//view_rotate.xml中:
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="2000"
    android:fillAfter="true"
    android:fromDegrees="0"
    android:interpolator="@android:anim/linear_interpolator"
    android:pivotX="50%"
    android:pivotY="50%"
    android:repeatCount="infinite"
    android:repeatMode="restart"
    android:toDegrees="360" />

代码中引用:

mTvTarget.startAnimation(AnimationUtils.loadAnimation(this, R.anim.view_rotate))

执行效果:

在XML中设置pivotX、pivotY时注意:pivotX pivotY,可取值为数字,百分比,或者百分比p

ScaleAnimation

缩放动画,设置的参数如下:

    public ScaleAnimation(float fromX, float toX, float fromY, float toY,
            int pivotXType, float pivotXValue, int pivotYType, float pivotYValue) {
        mResources = null;
        mFromX = fromX;
        mToX = toX;
        mFromY = fromY;
        mToY = toY;
        mPivotXValue = pivotXValue;
        mPivotXType = pivotXType;
        mPivotYValue = pivotYValue;
        mPivotYType = pivotYType;
        initializePivotPoint();
    }

动画示例

方式一:代码动态创建

val scaleAnim = ScaleAnimation(
                1.0f, 0.5f, 1.0f, 0.5f,
                Animation.RELATIVE_TO_SELF, 0.5f,
                Animation.RELATIVE_TO_SELF, 0.5f).apply {
            duration = 1000
            repeatCount = Animation.INFINITE
            repeatMode = Animation.REVERSE
            fillAfter = true
        }
mTvTarget.animation = scaleAnim

方式二:XML中创建

// view_scale.xml
<scale xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="1000"
    android:fillAfter="true"
    android:fromXScale="1.0"
    android:fromYScale="1.0"
    android:interpolator="@android:anim/linear_interpolator"
    android:pivotX="50%"
    android:pivotY="50%"
    android:repeatCount="infinite"
    android:repeatMode="reverse"
    android:toXScale="0.5"
    android:toYScale="0.5" />

代码中引入:

mTvTarget.startAnimation(AnimationUtils.loadAnimation(this, R.anim.view_scale))

执行结果:

TranslateAnimation

平移动画,是指对目标View进行平移操作。

    public TranslateAnimation(float fromXDelta, float toXDelta, float fromYDelta, float toYDelta) {
        mFromXValue = fromXDelta;
        mToXValue = toXDelta;
        mFromYValue = fromYDelta;
        mToYValue = toYDelta;
        mFromXType = ABSOLUTE;
        mToXType = ABSOLUTE;
        mFromYType = ABSOLUTE;
        mToYType = ABSOLUTE;
    }
    public TranslateAnimation(int fromXType, float fromXValue, int toXType, float toXValue,
            int fromYType, float fromYValue, int toYType, float toYValue) {
        mFromXValue = fromXValue;
        mToXValue = toXValue;
        mFromYValue = fromYValue;
        mToYValue = toYValue;
        mFromXType = fromXType;
        mToXType = toXType;
        mFromYType = fromYType;
        mToYType = toYType;
    }

可以看到有两个不同的构造方法:

动画示例

方式一:代码动态创建

val translateAnim = TranslateAnimation(
            Animation.ABSOLUTE, 0f, Animation.RELATIVE_TO_SELF, 0.5f,
            Animation.ABSOLUTE, 0f, Animation.RELATIVE_TO_SELF, 0.5f)
 .apply {
            duration = 2000
            fillAfter = false
            interpolator = LinearInterpolator()
            repeatMode = Animation.REVERSE
            repeatCount = Animation.INFINITE
        }
mTvTarget.animation = translateAnim

方式二:XML中创建

// view_translate.xml
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="2000"
    android:fillAfter="true"
    android:fromXDelta="0"
    android:fromYDelta="0"
    android:interpolator="@android:anim/linear_interpolator"
    android:repeatCount="infinite"
    android:repeatMode="reverse"
    android:toXDelta="50%"
    android:toYDelta="50%" />

代码中引入:

mTvTarget.startAnimation(AnimationUtils.loadAnimation(this, R.anim.view_translate))

执行效果:

AlphaAnimation

透明度动画

    public AlphaAnimation(float fromAlpha, float toAlpha) {
        mFromAlpha = fromAlpha;
        mToAlpha = toAlpha;
    }

动画示例

方式一:代码动态创建

val alphaAnim = AlphaAnimation(1.0f, 0.2f).apply {
            duration = 1000
            fillAfter = true
            interpolator = LinearInterpolator()
            repeatMode = Animation.REVERSE
            repeatCount = Animation.INFINITE
        }
mTvTarget.animation = alphaAnim

方式二:XML中创建

//view_alpha.xml
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="1000"
    android:fillAfter="true"
    android:fromAlpha="1.0"
    android:interpolator="@android:anim/linear_interpolator"
    android:repeatCount="infinite"
    android:repeatMode="reverse"
    android:toAlpha="0.0" />

代码中引入:

mTvTarget.startAnimation(AnimationUtils.loadAnimation(this, R.anim.view_alpha))

执行效果:

AnimationSet 动画组合

动画组合AnimationSet,本身也继承自Animation,可以将多个动画组合起来使用。如下属性在AnimationSet中设置时的注意事项:

动画示例

方式1:代码动态生成

    /**
     * 动画组合AnimationSet
     * - duration, repeatMode, fillBefore, fillAfter:当在AnimationSet对象上设置这些属性时,将被下推到所有子动画。
     * - repeatCount, fillEnabled:这些属性在AnimationSet中被忽略。
     * - startOffset, shareInterpolator: 这些属性应用于AnimationSet本身
     */
    private fun loadAnimSet(): Animation {
        //方式1:代码动态生成
        val rotateAnim = loadRotationAnim()
        val alphaAnim = loadAlphaAnimation()
        val translateAnim = loadTranslateAnimation()
        val scaleAnim = loadScaleAnimation()
        /**
         * shareInterpolator: 如果想让集合中的所有动画都使用与AnimationSet中
         * 设置的一样的插值器,则传true;反之,如果集合中每个动画都使用自己的插值器,则传false.
         */
        val animSet = AnimationSet(true).apply {
            duration = 3000
            interpolator = LinearInterpolator()
            fillAfter = true
            repeatMode = Animation.REVERSE
            startOffset = 100 //延迟执行动画,应用于AnimationSet本身
        }
        //animSet.cancel() //取消动画
        //animSet.reset() //重置动画
        animSet.addAnimation(rotateAnim)
        animSet.addAnimation(alphaAnim)
        animSet.addAnimation(translateAnim)
        animSet.addAnimation(scaleAnim)
        return animSet
    }
    mTvTarget.startAnimation(loadAnimSet())

方式2:通过XML创建:

//view_animation_set.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="3000"
    android:fillAfter="true"
    android:fillBefore="false"
    android:interpolator="@android:anim/linear_interpolator"
    android:repeatMode="reverse"
    android:shareInterpolator="true">
    <rotate
        android:fromDegrees="0"
        android:pivotX="50%"
        android:pivotY="50%"
        android:repeatCount="infinite"
        android:toDegrees="360" />
    <scale
        android:fromXScale="1.0"
        android:fromYScale="1.0"
        android:pivotX="50%"
        android:pivotY="50%"
        android:repeatCount="infinite"
        android:toXScale="0.5"
        android:toYScale="0.5" />
    <!-- startOffset:延迟执行动画-->
    <set
        android:fillAfter="true"
        android:interpolator="@android:anim/linear_interpolator"
        android:repeatMode="reverse"
        android:shareInterpolator="true"
        android:startOffset="100">
        <alpha
            android:fromAlpha="1.0"
            android:repeatCount="infinite"
            android:toAlpha="0.0" />
        <translate
            android:fromXDelta="0"
            android:fromYDelta="0"
            android:repeatCount="infinite"
            android:toXDelta="50%"
            android:toYDelta="50%" />
    </set>
</set>

代码中引入:

mTvTarget.startAnimation(AnimationUtils.loadAnimation(this, R.anim.view_animation_set))

执行结果:

以上就是Android 补间动画及组合AnimationSet常用方法详解的详细内容,更多关于Android 补间动画组合AnimationSet的资料请关注脚本之家其它相关文章!

您可能感兴趣的文章:
阅读全文