華為倉頡鴻蒙HarmonyOS NEXT原生UI之屬性動畫

組件的某些通用屬性變化時,可以通過屬性動畫實現(xiàn)漸變過渡效果,提升用戶體驗。支持的屬性包括width、height、backgroundColor、opacity、scale、rotate、translate等。布局類改變寬高的動畫,內(nèi)容都是直接到終點狀態(tài),例如文字、Canvas的內(nèi)容、linearGradient等,如果要內(nèi)容跟隨寬高變化,可以使用renderFit屬性配置。

函數(shù)

animationStart(AnimateParam)

public func animationStart(animation: AnimateParam): This

設(shè)置動畫效果。

參數(shù)名 參數(shù)類型 必填 默認值 描述
animation AnimateParam - 動畫效果相關(guān)參數(shù)。

animationEnd()

public func animationEnd(): This

設(shè)置動畫結(jié)束。

說明

animationStart 需要和 animationEnd 配合使用。animationStart作為組件的第一個屬性被調(diào)用,animationEnd作為最后一個屬性被調(diào)用。

示例代碼

import ohos.base.*
    import ohos.component.*
    import ohos.state_manage.*
    import ohos.state_macro_manage.*
    
    let animateOpt1 = AnimateParam(
        duration: 1200,
        curve: Curve.EaseOut,
        delay: 500,
        iterations: 3,
        playMode: PlayMode.Normal,
        onFinish: {
            => AppLog.info("onfinish")
        },
        expectedFrameRateRange: ExpectedFrameRateRange(
            min: 20,
            max: 120,
            expected: 90
        )
    )
    
    let animateOpt2 = AnimateParam(
        duration: 1200,
        curve: Curve.Friction,
        delay: 500,
        iterations: -1, // 設(shè)置-1表示動畫無限循環(huán)
        playMode: PlayMode.Alternate,
        onFinish: {
            => AppLog.info("onfinish")
        },
        expectedFrameRateRange: ExpectedFrameRateRange(
            min: 20,
            max: 120,
            expected: 90
        )
    )
    
    @Entry
    @Component
    class MyView {
        @State var widthSize: Length = 250.vp
        @State var heightSize: Length = 100.vp
        @State var rotateAngle: Float32 = 0.0
        @State var flag: Bool = true
        func build() {
            Column() {
                Button("change size").animationStart(animateOpt1).
                onClick{ =>
                    if (this.flag) {
                        this.widthSize = 150.vp
                        this.heightSize = 60.vp
                    } else {
                        this.widthSize = 250.vp
                        this.heightSize = 100.vp
                    }
                    this.flag = !this.flag
                }.margin(30)
                .animationEnd()
                .width(this.widthSize)
                .height(this.heightSize)
    
                Button('change rotate angle').animationStart(animateOpt2)
                .onClick { =>
                    this.rotateAngle = 90.0
                }.margin(50)
                .rotate(this.rotateAngle)
                .animationEnd()
                }.width(100.percent).
                margin(top: 20)
        }
    }

[圖片上傳失敗...(image-2800a8-1735046459874)]

如對您有幫助,幫忙點個“在看 、關(guān)注” 讓更多的人受益~!

技術(shù)交流群可加wx“LB-9191” 備注cangjie

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容