save code

This commit is contained in:
xsl
2026-04-27 22:15:09 +08:00
parent b753fd36a2
commit f8b1e567c1
3 changed files with 67 additions and 81 deletions
+1 -1
View File
@@ -9,7 +9,7 @@ android {
compileSdk 36 compileSdk 36
defaultConfig { defaultConfig {
applicationId "com.inewme.uvmirror.f20260113" applicationId "com.inewme.uvmirror.f20260127"
minSdk 29 minSdk 29
targetSdk 36 targetSdk 36
versionCode 1 versionCode 1
@@ -1,14 +1,21 @@
package com.inewme.uvmirror package com.inewme.uvmirror
import android.graphics.Color
import android.os.Bundle import android.os.Bundle
import android.util.Log import android.util.Log
import android.view.Gravity
import android.view.KeyEvent import android.view.KeyEvent
import android.view.KeyEvent.KEYCODE_BACK import android.view.KeyEvent.KEYCODE_BACK
import android.view.MotionEvent 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 androidx.lifecycle.lifecycleScope
import com.hmwl.face_sdk.DebugLog
import com.hmwl.face_sdk.FaceActivity import com.hmwl.face_sdk.FaceActivity
import kotlinx.coroutines.Job import kotlinx.coroutines.Job
import kotlinx.coroutines.delay import kotlinx.coroutines.delay
@@ -19,9 +26,12 @@ class MakeupActivity : FaceActivity()
{ {
lateinit var motionMgr: MotionManager lateinit var motionMgr: MotionManager
private var updateJob: Job? = null private var updateJob: Job? = null
private var loadingOverlay: View? = null
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
motionMgr = MotionManager(this, 10, 1.5f, 0f,0f,1f,240f, 200f, -200f) motionMgr = MotionManager(this, 10, 1.5f, 0f,0f,1f,240f, 200f, -200f)
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
showLoadingOverlay()
// 启动一个生命周期感知的协程, 每10ms 调用一次,可以通过update 来处理业务的逻辑 // 启动一个生命周期感知的协程, 每10ms 调用一次,可以通过update 来处理业务的逻辑
updateJob = lifecycleScope.launch { updateJob = lifecycleScope.launch {
while (true) { while (true) {
@@ -30,98 +40,71 @@ class MakeupActivity : FaceActivity()
} }
} }
DebugLog.i("MakeupActivity", "onCreate: starting LoadMotionList(sequenceAll=$sequenceAll)") // 首先一次性加载当前业务逻辑所要求的所有动画资源, 之后就可以任意跳转,和播放,不用考虑状态
// 首先一次性加载当前业务逻辑所要求的所有动画资源, 之后就可以任意跳转,和播放,不用考虑状态 motionMgr.LoadMotionList(sequenceString){
motionMgr.LoadMotionList(sequenceAll){
//当加载完成了所有资源后会回调 //当加载完成了所有资源后会回调
DebugLog.i("MakeupActivity", isMotionLoadFinished = true
"LoadMotionList callback fired -> isMotionLoadFinished=true (sequenceAll=$sequenceAll)") hideLoadingOverlay()
isMotionLoadFinished = true
} }
} }
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 var isMotionLoadFinished: Boolean = false
private var hasStartedPlaying: Boolean = false
private var currentIndex: Int = 0
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() }
//这里是模拟业务逻辑的地方
// 模拟问题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 fun update() private fun update()
{ {
if(isMotionLoadFinished) if(isMotionLoadFinished && !hasStartedPlaying)
{ {
if(elapsedTime == 0){ hasStartedPlaying = true
DebugLog.i("MakeupActivity", "update: first call -> PlayMotionList(sequenceA=$sequenceA, loop=true)") PlayMotionList(listOf(sequenceString[0]), true, null)
//这次播放是一直循环播放,我们不需要判断动画是否播放完成, 回调函数直接传入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)
}
}
} }
} }
// 模拟问题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 { override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
if (keyCode == KEYCODE_BACK) { if (keyCode == KEYCODE_BACK) {
Log.d("MakeupActivity", "Back key pressed") Log.d("MakeupActivity", "Back key pressed")
@@ -146,7 +129,10 @@ class MakeupActivity : FaceActivity()
override fun onTouchEvent(event: MotionEvent): Boolean { override fun onTouchEvent(event: MotionEvent): Boolean {
when (event.action) { when (event.action) {
MotionEvent.ACTION_UP -> { MotionEvent.ACTION_UP -> {
if (isMotionLoadFinished) {
currentIndex = (currentIndex + 1) % sequenceString.size
PlayMotionList(listOf(sequenceString[currentIndex]), true, null)
}
} }
} }
return true return true
+1 -1
View File
@@ -1,3 +1,3 @@
<resources> <resources>
<string name="app_name">f20260113</string> <string name="app_name">f20260127</string>
</resources> </resources>