相关文章推荐
风流倜傥的玉米  ·  【Android 进程保活】应用进程拉活 ...·  1 月前    · 
怕考试的椰子  ·  获取用户的邮箱设置 - Microsoft ...·  1 月前    · 
近视的橙子  ·  spring 集成 kafka ...·  1 月前    · 
逆袭的热带鱼  ·  开发框架Furion之WebApi+SqlS ...·  3 周前    · 
任性的墨镜  ·  多用户系统中共享conda环境中的权限冲突。 ...·  3 周前    · 
一身肌肉的泡面  ·  html5 后台 操作 ...·  1 年前    · 
绅士的大白菜  ·  药品名智能分类模型(化药_中成药_中药材)_ ...·  1 年前    · 
绅士的跑步机  ·  pandas.DataFrame获取满足某条 ...·  2 年前    · 
含蓄的眼镜  ·  pytorch加载自己的图像数据集实例 - ...·  2 年前    · 
Code  ›  在将lottie的json加载到LottieDrawable in SurfaceHolder之后没有显示的图像开发者社区
context
https://cloud.tencent.com/developer/ask/sof/107005468/answer/116911330
任性的紫菜汤
1 年前
首页
学习
活动
专区
工具
TVP 最新优惠活动
返回腾讯云官网
提问

问 在将lottie的json加载到LottieDrawable in SurfaceHolder之后没有显示的图像

Stack Overflow用户
提问于 2022-05-31 07:45:42
EN

我有一个Lottie json文件,在加载到SurfaceView的LottieDrawable后,我使用它来播放它。但是图像没有显示,动画是没有图像运行的!

但是当我在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"
        />
1 311 0 票数 0
EN
android
image
animation
drawable
lottie

Stack Overflow用户

回答已采纳

发布于 2022-06-20 02:02:20

最后,我使用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;
 
推荐文章
风流倜傥的玉米  ·  【Android 进程保活】应用进程拉活 ( JobScheduler 拉活 | JobScheduler 使用流程 | JobService 服务 | 不同版本兼容 | 源码资源 )开发者社
1 月前
怕考试的椰子  ·  获取用户的邮箱设置 - Microsoft Graph v1.0 | Microsoft Learn
1 月前
近视的橙子  ·  spring 集成 kafka producer(KafkaTemplate) - 护花使者
1 月前
逆袭的热带鱼  ·  开发框架Furion之WebApi+SqlSugar (一)
3 周前
任性的墨镜  ·  多用户系统中共享conda环境中的权限冲突。_conda 多用户使用
3 周前
一身肌肉的泡面  ·  html5 后台 操作 h5后端开发_mob6454cc7aaa9d的技术博客_51CTO博客
1 年前
绅士的大白菜  ·  药品名智能分类模型(化药_中成药_中药材)_中成药联合用药智能评价模型_python机器学习建模的博客-CSDN博客
1 年前
绅士的跑步机  ·  pandas.DataFrame获取满足某条件的行、元素所在的索引(index)_dataframe查找符合条件的记录_quantLearner的博客-CSDN博客
2 年前
含蓄的眼镜  ·  pytorch加载自己的图像数据集实例 - 腾讯云开发者社区-腾讯云
2 年前
今天看啥   ·   Py中国   ·   codingpro   ·   小百科   ·   link之家   ·   卧龙AI搜索
删除内容请联系邮箱 2879853325@qq.com
Code - 代码工具平台
© 2024 ~ 沪ICP备11025650号