欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android?補間動畫及組合AnimationSet常用方法詳解

 更新時間:2022年11月01日 09:28:51   作者:_小馬快跑_  
這篇文章主要為大家介紹了Android?補間動畫及組合AnimationSet常用方法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪

補間動畫

Android常用的四種補間動畫分別為RotateAnimation、ScaleAnimationTranslateAnimation、AlphaAnimation,他們的父類為Animation,UML類圖如下:

父類通用方法有:

  • public void setFillBefore(boolean fillBefore): 動畫完成后,View是否會停留在動畫開始的狀態(tài),默認為true
  • public void setFillAfter(boolean fillAfter) :動畫完成后,View是否會停留在動畫結(jié)束的狀態(tài),優(yōu)先級大于fillBefore,默認為false
  • public void setFillEnabled(boolean fillEnabled) :控制setFillBefore的值是否生效,如果fillEnabled為false,則setFillBefore無論為何值都不會再生效。
  • public boolean isFillEnabled():返回setFillEnabled設(shè)置的值。
  • public void setStartOffset(long startOffset):設(shè)置動畫延遲啟動時間,單位是ms
  • public void setDuration(long durationMillis):設(shè)置動畫持續(xù)時間,如果設(shè)置durationMillis < 0會拋異常。
  • public void setRepeatMode(int repeatMode)RESTART:正序重新開始、REVERSE:倒序重新開始,默認是RESTART。注意:repeatCount(count)設(shè)置的count值必須>0或者是INFINITE才會生效
  • public void setRepeatCount(int repeatCount):設(shè)置動畫應該重復的次數(shù)。如果為0,則動畫只播放一次;如果設(shè)置為N(N>0),則將繼續(xù)播放N次;如果設(shè)置為INFINITE,則將無限輪播。默認為0。
  • public void setInterpolator(Interpolator i):設(shè)置差值器,影響動畫的加速曲線。默認為AccelerateDecelerateInterpolator
  • public void setAnimationListener(AnimationListener listener) : 設(shè)置動畫監(jiān)聽,AnimationListener中的幾個方法如下:
animation.setAnimationListener(object : Animation.AnimationListener {
                override fun onAnimationEnd(animation: Animation?) {
                    //動畫結(jié)束時回調(diào),注意:當repeatCount設(shè)置為Animation.INFINITE不再收到該回調(diào)
                    log("onAnimationEnd")
                }
                override fun onAnimationStart(animation: Animation?) {
                    //動畫開始時回調(diào)
                    log("onAnimationStart")
                }
                override fun onAnimationRepeat(animation: Animation?) {
                    //repeatCount設(shè)置為Animation.INFINITE時動畫每執(zhí)行一次該方法回調(diào)就會執(zhí)行一次
                    log("onAnimationRepeat")
                }
            })

RotateAnimation

旋轉(zhuǎn)動畫,通過設(shè)置目標View的旋轉(zhuǎn)中心、旋轉(zhuǎn)角度來達到旋轉(zhuǎn)目的。RotateAnimation中需要特殊設(shè)置的幾個參數(shù)如下:

// 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();
 }
  • fromDegrees:動畫開始之前的旋轉(zhuǎn)偏移量
  • toDegrees:動畫結(jié)束時的旋轉(zhuǎn)偏移量
  • pivotXType:用于設(shè)置旋轉(zhuǎn)中心X軸計量類型,可以設(shè)置成Animation.ABSOLUTE、Animation.RELATIVE_TO_SELF、Animation.RELATIVE_TO_PARENT之一,pivotXTypepivotXValue組合之后可以表示不同的X軸坐標點。

Animation.ABSOLUTE:目標View的X軸坐標 = View左上角的原點 + pivotXValue數(shù)值的點(y方向同理)

Animation.RELATIVE_TO_SELF:目標View的X軸坐標 = View左上角的原點 + View自身寬度 * pivotXValue數(shù)值

Animation.RELATIVE_TO_PARENT:目標View的X軸坐標 = View左上角的原點 + View父控件寬度 * pivotXValue

  • pivotXValue:旋轉(zhuǎn)View所圍繞的點的X坐標。如果pivotXTypeabsolute,此值可以是一個絕對數(shù)字,否則可以是一個0.1f~1.0f 的值。
  • pivotYType:同pivotXType,表示的的是y軸
  • pivotYValue:同pivotXValue,表示的的是y軸

動畫示例

方式一:代碼動態(tài)創(chuàng)建

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中設(shè)置

//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))

執(zhí)行效果:

在XML中設(shè)置pivotX、pivotY時注意:pivotX pivotY,可取值為數(shù)字,百分比,或者百分比p

  • 設(shè)置為數(shù)字時,表示px像素值。如設(shè)置為10,那么中心坐標為View的左上角的原點在x方向y方向加上10px的點。對應代碼中的Animation.ABSOLUTE單位。
  • 設(shè)置為百分比時(如50%),中心坐標為View的左上角的原點在x方向加上自身寬度50%和y方向自身高度50%的點(即自身中間的位置)。對應代碼中的Animation.RELATIVE_TO_SELF。
  • 設(shè)置為百分比p時(如50%p),中心坐標為View的左上角的原點在x方向加上父控件寬度50%和y方向父控件高度50%的點。對應代碼中的Animation.RELATIVE_TO_PARENT

ScaleAnimation

縮放動畫,設(shè)置的參數(shù)如下:

    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();
    }
  • fromX / toX:分別表示X軸方向在動畫開始時、結(jié)束時對應的縮放因子
  • fromY / toY:分別表示Y軸方向在動畫開始時、結(jié)束時對應的縮放因子
  • pivotXType、pivotXValue、pivotYType、pivotYValue具體含義在上面的RotateAnimation已經(jīng)講到,這四個值在ScaleAnimation主要是用來確定縮放中心的。

動畫示例

方式一:代碼動態(tài)創(chuàng)建

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中創(chuàng)建

// 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))

執(zhí)行結(jié)果:

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;
    }

可以看到有兩個不同的構(gòu)造方法:

  • 第一種把類型固定為ABSOLUTE,那么傳入的float fromXDelta, float toXDelta, float fromYDelta, float toYDelta都為固定數(shù)值,即(fromXDelta, fromYDelta)、(toXDelta、toYDelta)分別代表起始坐標與結(jié)束坐標。
  • 第二種傳入的類型可以為 Animation.ABSOLUTE、Animation.RELATIVE_TO_SELF、Animation.RELATIVE_TO_PARENT 的一種,那么對應的fromXValue、toXValue、fromYValue、toYValue 可以是具體數(shù)值或或者0.0~1.0(表示目標View本身或著父View的百分比)。

動畫示例

方式一:代碼動態(tài)創(chuàng)建

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中創(chuàng)建

// 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))

執(zhí)行效果:

AlphaAnimation

透明度動畫

    public AlphaAnimation(float fromAlpha, float toAlpha) {
        mFromAlpha = fromAlpha;
        mToAlpha = toAlpha;
    }
  • fromAlpha: 動畫開始時的透明度: 1.0表示完全不透明,0.0表示完全透明。
  • toAlpha: 動畫結(jié)束時透明度: 1.0表示完全不透明,0.0表示完全透明。

動畫示例

方式一:代碼動態(tài)創(chuàng)建

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

方式二:XML中創(chuàng)建

//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))

執(zhí)行效果:

AnimationSet 動畫組合

動畫組合AnimationSet,本身也繼承自Animation,可以將多個動畫組合起來使用。如下屬性在AnimationSet中設(shè)置時的注意事項:

  • duration, repeatMode, fillBefore, fillAfter: 當在AnimationSet對象上設(shè)置這些屬性時,將被下推到所有子動畫。
  • repeatCount, fillEnabled: 這些屬性在AnimationSet中被忽略。
  • startOffset, shareInterpolator: 這些屬性應用于AnimationSet本身。

動畫示例

方式1:代碼動態(tài)生成

    /**
     * 動畫組合AnimationSet
     * - duration, repeatMode, fillBefore, fillAfter:當在AnimationSet對象上設(shè)置這些屬性時,將被下推到所有子動畫。
     * - repeatCount, fillEnabled:這些屬性在AnimationSet中被忽略。
     * - startOffset, shareInterpolator: 這些屬性應用于AnimationSet本身
     */
    private fun loadAnimSet(): Animation {
        //方式1:代碼動態(tài)生成
        val rotateAnim = loadRotationAnim()
        val alphaAnim = loadAlphaAnimation()
        val translateAnim = loadTranslateAnimation()
        val scaleAnim = loadScaleAnimation()
        /**
         * shareInterpolator: 如果想讓集合中的所有動畫都使用與AnimationSet中
         * 設(shè)置的一樣的插值器,則傳true;反之,如果集合中每個動畫都使用自己的插值器,則傳false.
         */
        val animSet = AnimationSet(true).apply {
            duration = 3000
            interpolator = LinearInterpolator()
            fillAfter = true
            repeatMode = Animation.REVERSE
            startOffset = 100 //延遲執(zhí)行動畫,應用于AnimationSet本身
        }
        //animSet.cancel() //取消動畫
        //animSet.reset() //重置動畫
        animSet.addAnimation(rotateAnim)
        animSet.addAnimation(alphaAnim)
        animSet.addAnimation(translateAnim)
        animSet.addAnimation(scaleAnim)
        return animSet
    }
    mTvTarget.startAnimation(loadAnimSet())

方式2:通過XML創(chuàng)建:

//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:延遲執(zhí)行動畫-->
    <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))

執(zhí)行結(jié)果:

以上就是Android 補間動畫及組合AnimationSet常用方法詳解的詳細內(nèi)容,更多關(guān)于Android 補間動畫組合AnimationSet的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評論