From f8b1e567c1cec28509c2214a54d528e9ff9d1cf8 Mon Sep 17 00:00:00 2001 From: xiangsilian Date: Mon, 27 Apr 2026 22:15:09 +0800 Subject: [PATCH] save code --- example/build.gradle | 2 +- .../com/inewme/uvmirror/MakeupActivity.kt | 144 ++++++++---------- example/src/main/res/values/strings.xml | 2 +- 3 files changed, 67 insertions(+), 81 deletions(-) diff --git a/example/build.gradle b/example/build.gradle index 8582c7a..545444c 100644 --- a/example/build.gradle +++ b/example/build.gradle @@ -9,7 +9,7 @@ android { compileSdk 36 defaultConfig { - applicationId "com.inewme.uvmirror.f20260113" + applicationId "com.inewme.uvmirror.f20260127" minSdk 29 targetSdk 36 versionCode 1 diff --git a/example/src/main/java/com/inewme/uvmirror/MakeupActivity.kt b/example/src/main/java/com/inewme/uvmirror/MakeupActivity.kt index ba5db8b..2b4ad88 100644 --- a/example/src/main/java/com/inewme/uvmirror/MakeupActivity.kt +++ b/example/src/main/java/com/inewme/uvmirror/MakeupActivity.kt @@ -1,14 +1,21 @@ package com.inewme.uvmirror +import android.graphics.Color import android.os.Bundle import android.util.Log +import android.view.Gravity import android.view.KeyEvent import android.view.KeyEvent.KEYCODE_BACK import android.view.MotionEvent +import android.view.View +import android.view.ViewGroup +import android.widget.FrameLayout +import android.widget.LinearLayout +import android.widget.ProgressBar +import android.widget.TextView import androidx.lifecycle.lifecycleScope -import com.hmwl.face_sdk.DebugLog import com.hmwl.face_sdk.FaceActivity import kotlinx.coroutines.Job import kotlinx.coroutines.delay @@ -19,9 +26,12 @@ class MakeupActivity : FaceActivity() { lateinit var motionMgr: MotionManager private var updateJob: Job? = null + private var loadingOverlay: View? = null + override fun onCreate(savedInstanceState: Bundle?) { motionMgr = MotionManager(this, 10, 1.5f, 0f,0f,1f,240f, 200f, -200f) super.onCreate(savedInstanceState) + showLoadingOverlay() // 启动一个生命周期感知的协程, 每10ms 调用一次,可以通过update 来处理业务的逻辑 updateJob = lifecycleScope.launch { while (true) { @@ -30,98 +40,71 @@ class MakeupActivity : FaceActivity() } } - DebugLog.i("MakeupActivity", "onCreate: starting LoadMotionList(sequenceAll=$sequenceAll)") - // 首先一次性加载当前业务逻辑所要求的所有动画资源, 之后就可以任意跳转,和播放,不用考虑状态 - motionMgr.LoadMotionList(sequenceAll){ + // 首先一次性加载当前业务逻辑所要求的所有动画资源, 之后就可以任意跳转,和播放,不用考虑状态 + motionMgr.LoadMotionList(sequenceString){ //当加载完成了所有资源后会回调 - DebugLog.i("MakeupActivity", - "LoadMotionList callback fired -> isMotionLoadFinished=true (sequenceAll=$sequenceAll)") - isMotionLoadFinished = true + isMotionLoadFinished = true + hideLoadingOverlay() } } + private fun showLoadingOverlay() { + val overlay = FrameLayout(this).apply { + setBackgroundColor(0x99000000.toInt()) + } + val container = LinearLayout(this).apply { + orientation = LinearLayout.VERTICAL + gravity = Gravity.CENTER + layoutParams = FrameLayout.LayoutParams( + FrameLayout.LayoutParams.MATCH_PARENT, + FrameLayout.LayoutParams.MATCH_PARENT + ) + } + container.addView(ProgressBar(this).apply { + layoutParams = LinearLayout.LayoutParams( + LinearLayout.LayoutParams.WRAP_CONTENT, + LinearLayout.LayoutParams.WRAP_CONTENT + ) + }) + container.addView(TextView(this).apply { + text = "加载中..." + setTextColor(Color.WHITE) + textSize = 16f + layoutParams = LinearLayout.LayoutParams( + LinearLayout.LayoutParams.WRAP_CONTENT, + LinearLayout.LayoutParams.WRAP_CONTENT + ).apply { topMargin = 24 } + }) + overlay.addView(container) + addContentView(overlay, ViewGroup.LayoutParams( + ViewGroup.LayoutParams.MATCH_PARENT, + ViewGroup.LayoutParams.MATCH_PARENT + )) + loadingOverlay = overlay + } + + private fun hideLoadingOverlay() { + runOnUiThread { loadingOverlay?.visibility = View.GONE } + } + //定义加载完成的变量,通过回调函数设置,加载完成后才可以进行动画播放的操作 var isMotionLoadFinished: Boolean = false + private var hasStartedPlaying: Boolean = false + private var currentIndex: Int = 0 - - - //这里是模拟业务逻辑的地方 - // 模拟问题1, 打断当前的动画,然后跳转到指定步骤 - // 首先播放动画序列 粉底(4、5、6、7)、腮红(55、59)、口红由(76、77、78) 一直循环播放 "4", "5", "6", "7", "55", "59", "76", "77", "78" - // 然后 当播放到 10秒的时候,也可以是其他事件,需要暂停动画 - // 然后 暂停5秒,这个时候需要继续播放动画 - // 然后 又播放了8秒钟,这个时候要却换到 (11, 13, 21 动画序列了), 同时设置序列播放完成回调函数 - // 然后 播放完这个序列又要切换为原来的序列播放,通过序列播放完成回调函数实现 - private var elapsedTime = 0 // 总经过时间(ms) - private val sequenceA = listOf("4") - private val sequenceB = listOf("56") - private val sequenceAll = listOf("4", "56") + private val sequence = listOf(4,5,6,7,11,12,13,14,21,56,59,62,65,68,71,78,81) + private val sequenceString = sequence.map { it.toString() } private fun update() { - if(isMotionLoadFinished) + if(isMotionLoadFinished && !hasStartedPlaying) { - if(elapsedTime == 0){ - DebugLog.i("MakeupActivity", "update: first call -> PlayMotionList(sequenceA=$sequenceA, loop=true)") - //这次播放是一直循环播放,我们不需要判断动画是否播放完成, 回调函数直接传入null - PlayMotionList(sequenceA, true, null) - } - - elapsedTime += 10 // 动画加载完成后开始计时,每次调用增加 10ms - - if(elapsedTime == 10*1000){ - DebugLog.i("MakeupActivity", "update: 10s tick -> StopMotion()") - //暂停动画 - StopMotion() - } - - if(elapsedTime == 10*1000 + 5*1000){ - DebugLog.i("MakeupActivity", "update: 15s tick -> ResumeMotion()") - //恢复播放动画 - ResumeMotion() - } - - if(elapsedTime == 10*1000 + 5*1000 + 8*1000){ - DebugLog.i("MakeupActivity", "update: 23s tick -> PlayMotionList(sequenceB=$sequenceB, loop=true)") - PlayMotionList(sequenceB, true){ - DebugLog.i("MakeupActivity", "sequenceB cb -> PlayMotionList(sequenceA, loop=true)") - //这里是播放完动画后的回调,再次切换到原来的 sequenceA - PlayMotionList(sequenceA, true, null) - } - } - + hasStartedPlaying = true + PlayMotionList(listOf(sequenceString[0]), true, null) } } - // 模拟问题2, 当语音播报结束的时候,打断当前的动画, 跳转到另外一个动画 - // 首先播放动画序列 粉底(4、5、6、7)、 - // 然后 当播放到 10秒的时候,表示语音播报结束, 条装到 "55", "59" 动画 -// private var elapsedTime = 0 // 总经过时间(ms) -// private val sequenceA = listOf("4", "5", "6") -// private val sequenceB = listOf("7", "55", "59") -// private val sequenceAll = listOf("4", "5", "6", "7", "55", "59") -// -// private fun update() -// { -// if(isMotionLoadFinished) -// { -// if(elapsedTime == 0){ -// //这次播放是一直循环播放,我们不需要判断动画是否播放完成, 回调函数直接传入null -// PlayMotionList(sequenceA, true, null) -// } -// -// elapsedTime += 10 // 动画加载完成后开始计时,每次调用增加 10ms -// -// if(elapsedTime == 10*1000){ -// //暂停动画 -// PlayMotionList(sequenceB, true, null) -// } -// } -// -// } - - override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean { if (keyCode == KEYCODE_BACK) { Log.d("MakeupActivity", "Back key pressed") @@ -146,7 +129,10 @@ class MakeupActivity : FaceActivity() override fun onTouchEvent(event: MotionEvent): Boolean { when (event.action) { MotionEvent.ACTION_UP -> { - + if (isMotionLoadFinished) { + currentIndex = (currentIndex + 1) % sequenceString.size + PlayMotionList(listOf(sequenceString[currentIndex]), true, null) + } } } return true diff --git a/example/src/main/res/values/strings.xml b/example/src/main/res/values/strings.xml index ad5cc65..ff80e8a 100644 --- a/example/src/main/res/values/strings.xml +++ b/example/src/main/res/values/strings.xml @@ -1,3 +1,3 @@ - f20260113 + f20260127 \ No newline at end of file