index.vue 6.8 KB
Newer Older
打打卡夫卡's avatar
打打卡夫卡 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
<template>
    <view class="defaultStyles">

    </view>
</template>
<script lang="ts">
    import Animator from 'android.animation.Animator'
    import TextUtils from 'android.text.TextUtils'
    import View from 'android.view.View'
    import LottieAnimationView from 'com.airbnb.lottie.LottieAnimationView'
    import LottieDrawable from 'com.airbnb.lottie.LottieDrawable'


    class CustomAnimListener extends Animator.AnimatorListener {

        comp: UTSComponent < LottieAnimationView >
            constructor(com: UTSComponent < LottieAnimationView > ) {
                super();
                this.comp = com
            }

        override onAnimationStart(animation: Animator | null) {}

        override onAnimationEnd(animation: Animator | null, isReverse: Boolean) {
            this.comp.emit("bindended")
        }

        override onAnimationEnd(animation: Animator | null) {}

        override onAnimationCancel(animation: Animator | null) {}

        override onAnimationRepeat(animation: Animator | null) {}
    }

    //原生提供以下属性或方法的实现  
    export default {
        name: "animation-view",
        /**
         * 当播放到末尾时触发 ended 事件(自然播放结束会触发回调,循环播放结束及手动停止动画不会触发)
         */
        emits: ['bindended'],
        props: {
            /**
             * 动画资源地址,目前只支持绝对路径
             */
            "path": {
                type: String,
                default: ""
            },
            /**
             * 动画是否循环播放
             */
            "autoplay": {
                type: Boolean,
                default: false
            },
            /**
             * 动画是否自动播放
             */
            "loop": {
                type: Boolean,
                default: false
            },
            /**
             * 是否隐藏动画
             */
            "hidden": {
                type: Boolean,
                default: false
            },
            /**
             * 动画操作,可取值 play、pause、stop
             */
            "action": {
                type: String,
                default: "stop"
            }

        },
        data() {
            return {

            }
        },
        watch: {
            "path": {
                handler(newPath: string) {

打打卡夫卡's avatar
打打卡夫卡 已提交
89 90
                    
					if(this.$el != null){
91
						let lottieAnimationView = this.$el!
打打卡夫卡's avatar
打打卡夫卡 已提交
92 93 94 95 96 97 98 99 100 101 102 103
						if (!TextUtils.isEmpty(newPath)) {
						    if (newPath.startsWith("http://") || newPath.startsWith("https://")) {
						        lottieAnimationView.setAnimationFromUrl(newPath)
						    } else {
						        // 默认是asset了
						        lottieAnimationView.setAnimation(newPath)
						    }
						}
						if (this.autoplay) {
						    lottieAnimationView.playAnimation()
						}
					}
打打卡夫卡's avatar
打打卡夫卡 已提交
104 105 106 107 108
                },
                immediate: false //创建时是否通过此方法更新属性,默认值为false  
            },
            "loop": {
                handler(newLoop: Boolean) {
打打卡夫卡's avatar
打打卡夫卡 已提交
109 110
					if(this.$el != null){
						if (newLoop) {
111
						    this.$el!.repeatCount = Int.MAX_VALUE
打打卡夫卡's avatar
打打卡夫卡 已提交
112 113
						} else {
						    // 不循环则设置成1次
114
						    this.$el!.repeatCount = 0
打打卡夫卡's avatar
打打卡夫卡 已提交
115 116 117
						}
						
						if (this.autoplay) {
118
						    this.$el!.playAnimation()
打打卡夫卡's avatar
打打卡夫卡 已提交
119 120 121
						}
					}
                    
打打卡夫卡's avatar
打打卡夫卡 已提交
122 123 124 125 126 127
                },
                immediate: false //创建时是否通过此方法更新属性,默认值为false  
            },

            "autoplay": {
                handler(newValue: boolean) {
打打卡夫卡's avatar
打打卡夫卡 已提交
128 129
					if(this.$el != null){
						if (newValue) {
130
						    this.$el!.playAnimation()
打打卡夫卡's avatar
打打卡夫卡 已提交
131 132 133
						}
					}
                    
打打卡夫卡's avatar
打打卡夫卡 已提交
134 135 136 137 138 139 140 141 142
                },
                immediate: false //创建时是否通过此方法更新属性,默认值为false  
            },

            "action": {
                handler(newAction: string) {

                    if (newAction == "play" || newAction == "pause" || newAction == "stop") {

打打卡夫卡's avatar
打打卡夫卡 已提交
143 144
						if(this.$el != null){
							if (this.action == "play") {
145
							    this.$el!.playAnimation()
打打卡夫卡's avatar
打打卡夫卡 已提交
146
							} else if (this.action == "pause") {
147
							    this.$el!.pauseAnimation()
打打卡夫卡's avatar
打打卡夫卡 已提交
148
							} else if (this.action == "stop") {
149 150
							    this.$el!.cancelAnimation()
							    this.$el!.clearAnimation()
打打卡夫卡's avatar
打打卡夫卡 已提交
151 152 153
							}
						}
                        
打打卡夫卡's avatar
打打卡夫卡 已提交
154 155 156 157 158 159 160 161 162 163

                    } else {
                        // 非法入参,不管
                    }
                },
                immediate: false //创建时是否通过此方法更新属性,默认值为false  
            },

            "hidden": {
                handler(newValue: boolean) {
打打卡夫卡's avatar
打打卡夫卡 已提交
164 165
					if(this.$el != null){
						if (newValue) {
166
						    this.$el!.visibility = View.GONE
打打卡夫卡's avatar
打打卡夫卡 已提交
167
						} else {
168
						    this.$el!.visibility = View.VISIBLE
打打卡夫卡's avatar
打打卡夫卡 已提交
169 170
						}
					}
打打卡夫卡's avatar
打打卡夫卡 已提交
171 172 173 174 175 176 177
                },
                immediate: false //创建时是否通过此方法更新属性,默认值为false  
            },

        },
        methods: {
            setRepeatMode(repeat: string) {
打打卡夫卡's avatar
打打卡夫卡 已提交
178 179
				if(this.$el != null){
					if ("RESTART" == repeat) {
180
					    this.$el!.repeatMode = LottieDrawable.RESTART
打打卡夫卡's avatar
打打卡夫卡 已提交
181
					} else if ("REVERSE" == repeat) {
182
					    this.$el!.repeatMode = LottieDrawable.RESTART
打打卡夫卡's avatar
打打卡夫卡 已提交
183 184
					}
				}
打打卡夫卡's avatar
打打卡夫卡 已提交
185 186 187 188 189 190 191 192 193 194 195 196
            },
            privateMethod() { //如何定义不对外暴露的API? 暂不支持,需在export外写  
            }
        },
        created() { //创建组件,替换created  

        },
        NVBeforeLoad() { //组件将要创建,对应前端beforeMount  
            //可选实现,这里可以提前做一些操作  
        },
        NVLoad(): LottieAnimationView { //创建原生View,必须定义返回值类型(Android需要明确知道View类型,需特殊校验)  
            //必须实现  
打打卡夫卡's avatar
打打卡夫卡 已提交
197
            let lottieAnimationView = new LottieAnimationView($androidContext)
打打卡夫卡's avatar
打打卡夫卡 已提交
198 199
            return lottieAnimationView
        },
200
		
打打卡夫卡's avatar
打打卡夫卡 已提交
201
        NVLoaded() { //原生View已创建  
打打卡夫卡's avatar
打打卡夫卡 已提交
202 203
			//可选实现,这里可以做后续操作
			if(this.$el != null){
204 205 206 207
				this.$el!.repeatMode = LottieDrawable.RESTART;
				this.$el!.visibility = View.GONE
				this.$el!.repeatCount = 0
				this.$el!.addAnimatorListener(new CustomAnimListener(this))
打打卡夫卡's avatar
打打卡夫卡 已提交
208 209
			}
           
打打卡夫卡's avatar
打打卡夫卡 已提交
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
        },
        NVLayouted() { //原生View布局完成  
            //可选实现,这里可以做布局后续操作  
        },
        NVBeforeUnload() { //原生View将释放  
            //可选实现,这里可以做释放View之前的操作  
        },
        NVUnloaded() { //原生View已释放  
            //可选实现,这里可以做释放View之后的操作  
        },
        unmounted() { //组件销毁  
            //可选实现  
        }
    }
</script>
<style>
    /* 定义默认样式值, 组件使用者没有配置时使用 */
    .defaultStyles {
        width: 750rpx;
        height: 240rpx;
    }
</style>