1. 程式人生 > 其它 >Android 動畫基礎:View 動畫

Android 動畫基礎:View 動畫

技術標籤:AndRoid開發android

前言

View 動畫的作用物件是View,支援4種動畫效果,分別是平移動畫、縮放動畫、旋轉動畫和透明度動畫,對應著Animation 的四個子類:TranslateAnimation、ScaleAnimation、RotateAnimation 和AlphaAnimation。

平移動畫

private fun translateAnimEx(btn: Button){
        btn.text="平移動畫"
        // xml
        val transAnimation = AnimationUtils.loadAnimation(this, R.anim.anim_translate_ex)
        transAnimation.duration = 3000
        btn.startAnimation(transAnimation)
        // 程式碼
        val transAnimation=TranslateAnimation(0f,180f,0f,180f)
        transAnimation.duration = 3000
        btn.startAnimation(transAnimation)
    }
anim_translate_ex.xml檔案
<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromXDelta="0"
    android:fromYDelta="0"
    android:toXDelta="180"
    android:toYDelta="180" />

效果:

縮放動畫

private fun scaleAnimEx(btn: Button){
        btn.text="縮放動畫"

        // xml
        val scaleAnimation = AnimationUtils.loadAnimation(this, R.anim.anim_scale_ex)
        scaleAnimation.duration = 3000
        btn.startAnimation(scaleAnimation)

        // 程式碼
        val scaleAnimation=ScaleAnimation(0f,120f,0f,120f,
               50f, 50f)
        scaleAnimation.duration = 3000
        btn.startAnimation(scaleAnimation)
    }
anim_scale_ex.xml
<scale xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromXScale="0"
    android:toXScale="20"
    android:fromYScale="0"
    android:toYScale="20"
    android:pivotX="50"
    android:pivotY="50"/>

效果:

旋轉動畫

 private fun rotateAnimEx(btn: Button){
        btn.text="旋轉動畫"

        // xml
        val rotateAnimation = AnimationUtils.loadAnimation(this, R.anim.anim_rotate_ex)
        rotateAnimation.duration = 3000
        btn.startAnimation(rotateAnimation)

        // 程式碼
        val rotateAnimation=RotateAnimation(0f,360f,100f,100f)
        rotateAnimation.duration = 3000
        btn.startAnimation(rotateAnimation)
    }
anim_rotate_ex.xml
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromDegrees="0"
    android:pivotX="100"
    android:pivotY="100"
    android:toDegrees="360" />

效果

透明度動畫

private fun alphaAnimEx(btn: Button){
        btn.text="透明動畫"
        // xml
        val alphaAnimation = AnimationUtils.loadAnimation(this, R.anim.anim_alpha_ex)
        alphaAnimation.duration=3000
        btn.startAnimation(alphaAnimation)

        // 程式碼
        val alphaAnimation = AlphaAnimation(0f, 1f)
        alphaAnimation.duration = 3000

        btn.startAnimation(alphaAnimation)
    }
anim_alpha_ex.xml
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromAlpha="0"
    android:toAlpha="1" />

效果

動畫集合

private fun setAnimEx(btn: Button){
        // xml
        val setAnimation = AnimationUtils.loadAnimation(this, R.anim.animation_set)
        setAnimation.duration = 3000
        btn.startAnimation(setAnimation)

        // 程式碼
        val rotateAnimation=RotateAnimation(0f,360f)
        val transAnimation=TranslateAnimation(0f,180f,0f,180f)

        val setAnimation=AnimationSet(false)
        setAnimation.addAnimation(rotateAnimation)
        setAnimation.addAnimation(transAnimation)

        setAnimation.duration = 350
        setAnimation.fillAfter=true
        setAnimation.interpolator=LinearInterpolator()
        setAnimation.zAdjustment=AnimationSet.ZORDER_NORMAL
        btn.startAnimation(setAnimation)
    }
animation_set.xml
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:fillAfter="true"
    android:interpolator="@android:anim/linear_interpolator"
    android:duration="350"
    android:zAdjustment="normal">

    <translate

        android:fromXDelta="0"
        android:fromYDelta="0"
        android:toXDelta="100"
        android:toYDelta="100" />

    <rotate
        android:duration="100"
        android:fromDegrees="0"
        android:toDegrees="360" />
</set>

動畫 xml 相關屬性

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    //動畫持續的時間
    android:duration="{int}"
    //動畫結束以後View是否停留在結束位置,true表示View停留在結束位置,false則不停留
    android:fillAfter="{true|false}"
    //動畫結束以後View是否停留在開始位置,true表示View停留在結束位置,false則不停留
    android:fillBefore="{true|false}"
    // 表示集合中的動畫是否和集合共享同一個插值器
    android:shareInterpolator="{true|false}"
    // 重複模式
    android:repeatMode="{restart|reverse}"
    // 延時開始動畫
    android:startOffset="{int}"
    // 設定插值器,
    android:interpolator="@{package:}anim/interpolator_resource">

    <rotate
        //旋轉開始的角度
        android:fromDegrees="float"
        //旋轉的**軸點**的x座標
        android:pivotX="float"
        // 旋轉的**軸點**的y座標
        android:pivotY="float"
        // 旋轉結束的角度
        android:toDegrees="float" />

    <alpha
        //表示透明度的起始值
        android:fromAlpha="float"
        //表示透明度的結束值
        android:toAlpha="float" />
    <scale
        //水平方向縮放的起始值
        android:fromXScale="float"
        //豎直方向縮放的起始值
        android:fromYScale="float"
        //旋轉的**軸點**的x座標
        android:pivotX="float"
        // 旋轉的**軸點**的y座標
        android:pivotY="float"
        //水平方向縮放的結束值
        android:toXScale="float"
        //豎直方向縮放的結束值
        android:toYScale="float" />
    <translate
        //表示x的起始值
        android:fromXDelta="float"
        //表示y的起始值
        android:fromYDelta="float"
        //水平方向縮放的結束值
        android:toXDelta="float"
        // 表示y的結束值
        android:toYDelta="float" />

</set>

自定義動畫

只需要繼承Animation這個抽象類,然後重寫它的initialize 和applyTransformation方法,在initialize方法中做些初始化工作,在applyTransformation 中進行相應的矩陣變換即可,很多時候需要採用Camera來簡化矩陣變換的過程。

示例:3D 旋轉

// 自定義View 動畫
    private fun animatonView() {
        val rotate3DAnimation = Rotate3DAnimation(0f, 360f, 30f, 180f, true, 6f)
        rotate3DAnimation.interpolator = AccelerateDecelerateInterpolator()
        rotate3DAnimation.fillAfter = true
        rotate3DAnimation.duration = 3500
        btn_anim_set.startAnimation(rotate3DAnimation)
    }


class Rotate3DAnimation : Animation {


    private var mFromDegree: Float
    private var mToDegree: Float
    private var mCenterX: Float
    private var mCenterY: Float
    private var mDepthZ: Float
    private var mReverse: Boolean
    private lateinit var mCamera: Camera

    constructor(mFromDegree: Float, mToDegree: Float, mCenterX: Float,mCenterY: Float,   mReverse: Boolean, mDepthZ: Float) : super() {
        this.mFromDegree = mFromDegree
        this.mCenterY = mCenterY
        this.mCenterX = mCenterX
        this.mToDegree = mToDegree
        this.mReverse = mReverse
        this.mDepthZ = mDepthZ
    }


    // 初始化
    override fun initialize(width: Int, height: Int, parentWidth: Int, parentHeight: Int) {
        super.initialize(width, height, parentWidth, parentHeight)
        this.mCamera = Camera()
    }

    // 矩陣變化
    override fun applyTransformation(interpolatedTime: Float, t: Transformation?) {
        super.applyTransformation(interpolatedTime, t)
        var fromDegree=mFromDegree
        var degree=fromDegree+(mToDegree-mFromDegree)*interpolatedTime

        var centerX=mCenterX
        var centerY=mCenterY

        val  camera=mCamera
        val matrix=t?.matrix
        //將當前的攝像頭位置儲存下來,以便變換進行完成後恢復成原位
        camera.save()
        // camera.translate,這個方法接受3個引數,分別是x,y,z三個軸的偏移量,我們這裡只將z軸進行了偏移
        if (mReverse){
            // z的偏移會越來越大。這就會形成這樣一個效果,view從近到遠
            camera.translate(0f,0f,mDepthZ*interpolatedTime)
        }else{
            // z的偏移會越來越小。這就會形成這樣一個效果,我們的View從一個很遠的地方向我們移過來,越來越近,
                // 最終移到了我們的視窗上面
            camera.translate(0f,0f,mDepthZ*(1f-interpolatedTime))
        }
//        View加上旋轉效果,在移動的過程中,檢視還會移Y軸為中心進行旋轉
        camera.rotateY(degree)
//        將我們剛才定義的一系列變換應用到變換矩陣上面,呼叫完這句之後,我們就可以將camera的位置恢復了,以便下一次再使用
        camera.getMatrix(matrix)
        // camera位置恢復
        camera.restore()
// 以View的中心點為旋轉中心,如果不加這兩句,就是以(0,0)點為旋轉中心
        matrix?.preTranslate(-centerX,-centerY)
        matrix?.postTranslate(centerX,centerY)
    }
}

效果:

VIew 動畫原理

每次繪製檢視時View所在的ViewGroup中的drawChild函式獲取該View的Animation的Transformation值,然後呼叫canvas.contact(transformToApply.getMatrix()),通過矩陣運算完成動畫幀。如果動畫沒有完成,就繼續呼叫invalidate()函式,啟動下次繪製來驅動動畫,從而完成整個動畫的繪製。