在加载我的Lottie的JSON之后未显示的图像在Surfach Holder中可以亮点

发布于 2025-02-03 02:18:23 字数 2962 浏览 3 评论 0原文

我有一个Lottie JSON文件,在SurfaceView的LottiedRawable中加载后,我会用ValueAnimator播放它。但是图像没有显示,动画在没有图像的情况下运行!

但是,当我在Lottieanimationview中测试了这个JSON时,它的工作正常。那么我该怎么做才能在Lottiedrawable中玩? Thx

Lottiedrawable,播放没有图像显示:

        if (lottieDrawable.composition == null) {
            //val url = "https://assets4.lottiefiles.com/packages/lf20_dkzdaf1z.json"
            //val task = LottieCompositionFactory.fromUrl(applicationContext, url)

            val input = applicationContext.assets.open("wallpaper_lottie.json")
            val task = LottieCompositionFactory.fromJsonInputStream(input, null)

            //val input = applicationContext.assets.open("wallpaper_lottie.zip")
            //val zip = ZipInputStream(input)
            //val task = LottieCompositionFactory.fromZipStream(zip, null)

            task.addFailureListener {
                Log.i(TAG, "LOT::addFailureListener, " + it.message)
            }
                task.addListener { comp ->

                    // lottie drawable
                    lottieDrawable.composition = comp
                    Log.i(TAG, "LOT::lottieDrawable.composition, 1 = ${lottieDrawable.composition.toString().subSequence(0, 15)}")

                    val lotDrawableWidth = (screenWidth * 90F / 100F).toInt()
                    val lotDrawableHeight = (screenWidth * 80F / 100F).toInt()
                    val statusBarHeight = 65
                    val lotDrawableLeft = ((screenWidth - lotDrawableWidth) / 2).toInt()
                    val lotDrawableTop = (screenHeight - statusBarHeight).toInt()
                    lottieDrawable.bounds = Rect(
                        lotDrawableLeft,
                        lotDrawableTop,
                        lotDrawableLeft + lotDrawableWidth,
                        lotDrawableTop + lotDrawableHeight,
                    )

                    // lottie animator
                    lottieAnimator.cancel()
                    lottieAnimator.setIntValues(0, lottieDrawable.maxFrame.toInt())
                    lottieAnimator.repeatCount = LottieDrawable.INFINITE
                    lottieAnimator.duration = 6000
                    lottieAnimator.addUpdateListener { anim ->
                        val frame = anim.animatedValue as Int
                        lottieDrawable.frame = frame
                        // lottieDrawable.draw(canvas)
                        Log.i(TAG, "LOT::addUpdateListener, frame = ${lottieDrawable.frame}")
                    }
                    lottieAnimator.start()
                }
            }

Lottieanimationview,播放图像显示:

    <com.airbnb.lottie.LottieAnimationView
        android:id="@+id/iv_scanning"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        app:lottie_fileName="wallpaper_lottie.json"
        app:lottie_loop="true"
        />

I have a Lottie json file, I play it with ValueAnimator after loaded in SurfaceView's LottieDrawable. But images are not showing, the animation is running without images!

But when I tested this json in LottieAnimationView, It just works fine. So what should I do to play in LottieDrawable? thx

LottieDrawable, play without images showing:

        if (lottieDrawable.composition == null) {
            //val url = "https://assets4.lottiefiles.com/packages/lf20_dkzdaf1z.json"
            //val task = LottieCompositionFactory.fromUrl(applicationContext, url)

            val input = applicationContext.assets.open("wallpaper_lottie.json")
            val task = LottieCompositionFactory.fromJsonInputStream(input, null)

            //val input = applicationContext.assets.open("wallpaper_lottie.zip")
            //val zip = ZipInputStream(input)
            //val task = LottieCompositionFactory.fromZipStream(zip, null)

            task.addFailureListener {
                Log.i(TAG, "LOT::addFailureListener, " + it.message)
            }
                task.addListener { comp ->

                    // lottie drawable
                    lottieDrawable.composition = comp
                    Log.i(TAG, "LOT::lottieDrawable.composition, 1 = ${lottieDrawable.composition.toString().subSequence(0, 15)}")

                    val lotDrawableWidth = (screenWidth * 90F / 100F).toInt()
                    val lotDrawableHeight = (screenWidth * 80F / 100F).toInt()
                    val statusBarHeight = 65
                    val lotDrawableLeft = ((screenWidth - lotDrawableWidth) / 2).toInt()
                    val lotDrawableTop = (screenHeight - statusBarHeight).toInt()
                    lottieDrawable.bounds = Rect(
                        lotDrawableLeft,
                        lotDrawableTop,
                        lotDrawableLeft + lotDrawableWidth,
                        lotDrawableTop + lotDrawableHeight,
                    )

                    // lottie animator
                    lottieAnimator.cancel()
                    lottieAnimator.setIntValues(0, lottieDrawable.maxFrame.toInt())
                    lottieAnimator.repeatCount = LottieDrawable.INFINITE
                    lottieAnimator.duration = 6000
                    lottieAnimator.addUpdateListener { anim ->
                        val frame = anim.animatedValue as Int
                        lottieDrawable.frame = frame
                        // lottieDrawable.draw(canvas)
                        Log.i(TAG, "LOT::addUpdateListener, frame = ${lottieDrawable.frame}")
                    }
                    lottieAnimator.start()
                }
            }

LottieAnimationView, play with image showed:

    <com.airbnb.lottie.LottieAnimationView
        android:id="@+id/iv_scanning"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        app:lottie_fileName="wallpaper_lottie.json"
        app:lottie_loop="true"
        />

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

淡看悲欢离合 2025-02-10 02:18:23

最终,我使用ValueAnimator播放它,Fakeanimview做到了这一点。希望提供帮助:

    fun draw() {
        val screenWidth = canvas.width.toFloat()
        val screenHeight = canvas.height.toFloat()

        // draw lottie drawable
        // https://github.com/airbnb/lottie-android/issues/1177
        if (lottieDrawable.composition == null) {

            val input = applicationContext.assets.open("anim/lottie.json")
            val task = LottieCompositionFactory.fromJsonInputStream(input, null)

            task.addFailureListener {
                if (BuildConfig.DEBUG) {
                    Log.i(TAG, "LOT::drawPreview, addFailureListener, " + it.message)
                }
            }
            task.addListener { comp ->

                // lottie drawable
                lottieDrawable.composition = comp

                val lotDrawableWidth = (screenWidth * 80F / 100F).toInt()
                val lotDrawableHeight = (screenWidth * 60F / 100F).toInt()
                val statusBarHeight = if (Sdk30PermissionUtil.hasSdk30()) 0F else 65F
                val lotDrawableLeft = ((screenWidth - lotDrawableWidth) / 2).toInt()
                val lotDrawableTop = (screenHeight - statusBarHeight).toInt()
                lottieDrawable.bounds = Rect(
                    lotDrawableLeft,
                    lotDrawableTop,
                    lotDrawableLeft + lotDrawableWidth,
                    lotDrawableTop + lotDrawableHeight,
                )

                // lottie animator
                lottieAnimator.cancel()
                lottieAnimator.setIntValues(0, lottieDrawable.maxFrame.toInt())
                lottieAnimator.repeatCount = LottieDrawable.INFINITE
                lottieAnimator.duration = 6000
                lottieAnimator.addUpdateListener { anim ->
                    val frame = anim.animatedValue as Int
                    lottieDrawable.frame = frame
                    if (BuildConfig.DEBUG) {
                        Log.i(TAG, "LOT::lottieAnimator.addUpdateListener, frame = ${lottieDrawable.frame}")
                    }
                }
                lottieAnimator.start()

                ReleaseUtil.release(input) //! prevent OOM
            }
        }

        if (lottieDrawable.composition != null) {
            if (BuildConfig.DEBUG) {
                Log.i(TAG, "drawPreview, setDelegate")
            }
            WallpaperUtil.setDelegate(applicationContext, lottieDrawable)
            // draw lottie drawable
            val lotDrawableWidth = (screenWidth * 80F / 100F).toInt()
            val lotDrawableHeight = lotDrawableWidth
            val transX = (screenWidth - lotDrawableWidth) / 2
            val transY = 365F
            canvas.translate(transX, transY)
            lottieDrawable.draw(canvas)

        }
    }



    fun setDelegate(context: Context, lottieDrawable: LottieDrawable) {
        try {
            if (BuildConfig.DEBUG) {
                Log.i(TAG, "setDelegate")
                Log.i(TAG, "setDelegate, lottieDrawable.getCallback = " + lottieDrawable.callback)
            }

            if (lottieDrawable.callback == null) {
                lottieDrawable.callback = FakeAnimView(context, null)
            }
            lottieDrawable.setImageAssetDelegate { asset: LottieImageAsset ->
                var bitmap: Bitmap? = null

                try {
                    val filePath = "anim/images/"
                    val fileName = asset.fileName
                    bitmap = getBitmapFromAsset(context, filePath + fileName)

                    if (BuildConfig.DEBUG) {
                        Log.i(TAG, "setDelegate, fileName=$fileName")
                        Log.i(TAG, "setDelegate, bitmap=$bitmap")
                    }

                } catch (t: Throwable) {
                    t.printStackTrace()
                    Log.i(TAG, "setDelegate, Throwable = $t")
                }

                bitmap
            }
        } catch (t: Throwable) {
            t.printStackTrace()
            Log.i(TAG, "setDelegate, Throwable = $t")
        }
    }




/* loaded from: classes2.dex */
public class FakeAnimView extends View {
    public FakeAnimView(Context context, @Nullable AttributeSet attributeSet) {
        super(context, null);
    }

    @Override // android.view.View
    public int getVisibility() {
        return VISIBLE;
    }

    @Override // android.view.View
    protected void onMeasure(int i2, int i3) {
        super.onMeasure(200, 200);
    }
}

Finally I play it using ValueAnimator, and FakeAnimView did the trick. Wish to help:

    fun draw() {
        val screenWidth = canvas.width.toFloat()
        val screenHeight = canvas.height.toFloat()

        // draw lottie drawable
        // https://github.com/airbnb/lottie-android/issues/1177
        if (lottieDrawable.composition == null) {

            val input = applicationContext.assets.open("anim/lottie.json")
            val task = LottieCompositionFactory.fromJsonInputStream(input, null)

            task.addFailureListener {
                if (BuildConfig.DEBUG) {
                    Log.i(TAG, "LOT::drawPreview, addFailureListener, " + it.message)
                }
            }
            task.addListener { comp ->

                // lottie drawable
                lottieDrawable.composition = comp

                val lotDrawableWidth = (screenWidth * 80F / 100F).toInt()
                val lotDrawableHeight = (screenWidth * 60F / 100F).toInt()
                val statusBarHeight = if (Sdk30PermissionUtil.hasSdk30()) 0F else 65F
                val lotDrawableLeft = ((screenWidth - lotDrawableWidth) / 2).toInt()
                val lotDrawableTop = (screenHeight - statusBarHeight).toInt()
                lottieDrawable.bounds = Rect(
                    lotDrawableLeft,
                    lotDrawableTop,
                    lotDrawableLeft + lotDrawableWidth,
                    lotDrawableTop + lotDrawableHeight,
                )

                // lottie animator
                lottieAnimator.cancel()
                lottieAnimator.setIntValues(0, lottieDrawable.maxFrame.toInt())
                lottieAnimator.repeatCount = LottieDrawable.INFINITE
                lottieAnimator.duration = 6000
                lottieAnimator.addUpdateListener { anim ->
                    val frame = anim.animatedValue as Int
                    lottieDrawable.frame = frame
                    if (BuildConfig.DEBUG) {
                        Log.i(TAG, "LOT::lottieAnimator.addUpdateListener, frame = ${lottieDrawable.frame}")
                    }
                }
                lottieAnimator.start()

                ReleaseUtil.release(input) //! prevent OOM
            }
        }

        if (lottieDrawable.composition != null) {
            if (BuildConfig.DEBUG) {
                Log.i(TAG, "drawPreview, setDelegate")
            }
            WallpaperUtil.setDelegate(applicationContext, lottieDrawable)
            // draw lottie drawable
            val lotDrawableWidth = (screenWidth * 80F / 100F).toInt()
            val lotDrawableHeight = lotDrawableWidth
            val transX = (screenWidth - lotDrawableWidth) / 2
            val transY = 365F
            canvas.translate(transX, transY)
            lottieDrawable.draw(canvas)

        }
    }



    fun setDelegate(context: Context, lottieDrawable: LottieDrawable) {
        try {
            if (BuildConfig.DEBUG) {
                Log.i(TAG, "setDelegate")
                Log.i(TAG, "setDelegate, lottieDrawable.getCallback = " + lottieDrawable.callback)
            }

            if (lottieDrawable.callback == null) {
                lottieDrawable.callback = FakeAnimView(context, null)
            }
            lottieDrawable.setImageAssetDelegate { asset: LottieImageAsset ->
                var bitmap: Bitmap? = null

                try {
                    val filePath = "anim/images/"
                    val fileName = asset.fileName
                    bitmap = getBitmapFromAsset(context, filePath + fileName)

                    if (BuildConfig.DEBUG) {
                        Log.i(TAG, "setDelegate, fileName=$fileName")
                        Log.i(TAG, "setDelegate, bitmap=$bitmap")
                    }

                } catch (t: Throwable) {
                    t.printStackTrace()
                    Log.i(TAG, "setDelegate, Throwable = $t")
                }

                bitmap
            }
        } catch (t: Throwable) {
            t.printStackTrace()
            Log.i(TAG, "setDelegate, Throwable = $t")
        }
    }




/* loaded from: classes2.dex */
public class FakeAnimView extends View {
    public FakeAnimView(Context context, @Nullable AttributeSet attributeSet) {
        super(context, null);
    }

    @Override // android.view.View
    public int getVisibility() {
        return VISIBLE;
    }

    @Override // android.view.View
    protected void onMeasure(int i2, int i3) {
        super.onMeasure(200, 200);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文