SMR工作室
SMR工作室
  • 发布:2022-10-28 10:09
  • 更新:2022-10-28 16:08
  • 阅读:1291

安卓不支持 requestAnimationFrame() 方法?有可以代替的方案吗?

分类:uni-app


在手机中的浏览器测试是没问题的,但是运行到基座中就报错?是安卓不支持吗?如果不支持有什么相似的 api 可以在安卓中使用的吗?

2022-10-28 10:09 负责人:无 分享
已邀请:
BoredApe

BoredApe - 有问题就会有答案。

requestAnimationFrame 是操作 DOM 的,你可以在renderjs中使用,参考文档:renderjs

  • SMR工作室 (作者)

    renderjs 支持 vue3 setup 吗? 我看文档似乎只支持 vue2 ?

    2022-10-28 10:44

  • BoredApe

    回复 SMR工作室: 这个和vue版本无关。支持的

    2022-10-28 10:48

  • SMR工作室 (作者)

    回复 BoredApe: 经过学习,我写了下 renderjs 的代码,在H5一切正常,在安卓基座是不报错了,但是好像点击事件没有触发?您能帮我看下吗?

    2022-10-28 14:25

  • BoredApe

    回复 SMR工作室: 代码怎么写的。截图或者上传一个测试工程

    2022-10-28 14:28

  • SMR工作室 (作者)

    回复 BoredApe: 我把 index.vue 上传在我下面的回复里了,麻烦您了,谢谢!

    2022-10-28 14:29

  • SMR工作室 (作者)

    回复 BoredApe:大佬不好弄吗?或者 uniapp 能直接调用安卓原生的动画帧 api 吗?我查到安卓 AnimationDrawable() 函数里面有 “getNumberOfFrames() :获取帧的动画数“ 方法,这个可以用 uniapp 调用吗?

    2022-10-28 14:56

  • SMR工作室 (作者)

    回复 BoredApe: 大佬,我测试了,requestAnimationFrame 可以正常执行,没问题,现在就是 ownerInstance.callMethod() 触发无效,很奇怪~

    2022-10-28 15:24

SMR工作室

SMR工作室 (作者)

@BoredApe

SMR工作室

SMR工作室 (作者)

<template>  
    <view class="app-box">  
        <view id="test" :style="{left:x+'rpx',top:y+'rpx'}" class="box" @click="test.onclick">  
            <text>{{'y : ' + y}}</text>  
        </view>  
        <view :style="{left:xx+'rpx',top:yy+'rpx',backgroundColor: 'red'}" class="box" @click="onclick1">  
            <text>{{'y : '+ yy}}</text>  
        </view>  
    </view>  
</template>  

<script setup lang="ts">  
    import {  
        ref  
    } from "vue"  

    const x = ref(0)  
    const y = ref(0)  

    const onclickEx = (cy: number) => {  
        console.log('@@@', cy);  
        y.value = cy  
    }  

    const xx = ref(300)  
    const yy = ref(0)  

    let hh = 0  
    const ss = 4  
    let pp = ss  

    const onclick1 = () => {  
        if (hh !== 0) {  
            clearInterval(hh)  
            hh = 0  
        } else {  
            hh = setInterval(() => {  
                if (yy.value >= 1000) {  
                    pp = ss * 0 - ss  
                } else if (yy.value <= 0) {  
                    pp = ss  
                }  
                yy.value = yy.value + pp  
            }, 1000 / 60)  
        }  
    }  
</script>  

<script module="test" lang="renderjs">  
    export default {  
        mounted() {  

        },  

        data() {  
            return {  
                x: 0,  
                y: 0,  
                s: 4,  
                p: this.s  
            }  
        },  

        methods: {  
            onclick(e, o) {  
                this.updateView(() => {  
                    console.log(this.y);  

                    if (this.y >= 1000) {  
                        this.p = this.s * 0 - this.s  
                    } else if (this.y <= 0) {  
                        this.p = this.s  
                    }  
                    this.y = this.y + this.p  

                    o.callMethod('onclickEx', this.y)  

                    return false  
                })  
            },  

            updateView(fn) {  
                let lastFrameTime = 0  

                const animationFrame = (nowFrameTime) => {  

                    let stopAnimation = false  

                    if (lastFrameTime != 0) {  
                        const oneFrameTime = Math.min(nowFrameTime - lastFrameTime, 100) / 1000  
                        stopAnimation = fn(oneFrameTime) === false  
                    }  

                    lastFrameTime = nowFrameTime  

                    if (!stopAnimation) {  
                        requestAnimationFrame(animationFrame);  
                    }  

                }  
                requestAnimationFrame(animationFrame);  
            }  
        }  
    }  
</script>  

<style lang="scss" scoped>  
    .app-box {  
        width: 100vw;  
        height: 100vh;  
        display: flex;  
        flex-direction: column;  
        justify-content: center;  
        align-items: center;  
        position: relative;  

        .box {  
            width: 200rpx;  
            height: 200rpx;  
            background-color: #666;  
            position: absolute;  
        }  
    }  
</style>  

这是我修改测试后的代码,目前问题是 requestAnimationFrame 可以正常执行,没问题,现在就是ownerInstance.callMethod() 触发无效,我用夜神模拟器调试的,版本是安卓9,这个是不是也有关系?还是本身编译的BUG?

  • BoredApe

    更新HBuilderX 到最新版试试【3.6.8.20221027-alpha】

    2022-10-28 17:41

  • SMR工作室 (作者)

    回复 BoredApe: 升级了,还是没反应,我也提交BUG了,看官方怎么回复吧,谢谢大佬,已关注,已感谢!

    2022-10-28 23:21

该问题目前已经被锁定, 无法添加新回复