save code
This commit is contained in:
@@ -9,8 +9,10 @@ import android.view.KeyEvent.KEYCODE_BACK
|
||||
import android.view.MotionEvent
|
||||
import androidx.lifecycle.lifecycleScope
|
||||
import com.hmwl.face_sdk.FaceActivity
|
||||
import com.hmwl.face_sdk.Frame
|
||||
import com.hmwl.face_sdk.InitArg
|
||||
import com.hmwl.face_sdk.Motion
|
||||
import com.hmwl.face_sdk.MotionList
|
||||
import kotlinx.coroutines.Job
|
||||
import kotlinx.coroutines.delay
|
||||
import kotlinx.coroutines.launch
|
||||
@@ -21,18 +23,13 @@ import java.io.InputStreamReader
|
||||
|
||||
class MakeupActivity : FaceActivity()
|
||||
{
|
||||
var isLoadFinished: Boolean = false;
|
||||
var isMotionFinished: Boolean = true;
|
||||
var _motionIndex : Int = 0
|
||||
|
||||
|
||||
var animationFinishedFun = object : AnimationFinishedCallbackInterface {
|
||||
override fun OnAnimationFinished() {
|
||||
isMotionFinished = true;
|
||||
Log.i("MakeupActivity", "OnAnimationFinished: ")
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
var isLoadFinished : Boolean = false
|
||||
var loadFinishedFun = object : CallbackInterface {
|
||||
override fun OnLoadActionFinished(motion_type: String) {
|
||||
isLoadFinished = true;
|
||||
@@ -40,28 +37,13 @@ class MakeupActivity : FaceActivity()
|
||||
}
|
||||
|
||||
|
||||
|
||||
private val motionQueue: MutableList<String> = mutableListOf()
|
||||
fun PlayMotionQueue(motions: List<String>)
|
||||
{
|
||||
for (m in motions) {
|
||||
var motion = getMotionByName(m);
|
||||
var motionJsonString = motion.toJson();
|
||||
motionQueue.add(motionJsonString)
|
||||
}
|
||||
//开始播放动画的时候需要先加载第一个动画
|
||||
isLoadFinished = false;
|
||||
_motionIndex = 0;
|
||||
PreLoadAction(motionQueue[_motionIndex], loadFinishedFun)
|
||||
}
|
||||
|
||||
private var updateJob: Job? = null
|
||||
|
||||
|
||||
|
||||
private var existMotions: Map<String, Motion>? = null
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
|
||||
FaceInit(this)
|
||||
existMotions = readMotionsFromAssets(this, "pic/motion_data_ex.json")
|
||||
|
||||
val initArg = InitArg().apply {
|
||||
action_fps = 10
|
||||
@@ -81,27 +63,32 @@ class MakeupActivity : FaceActivity()
|
||||
}
|
||||
}
|
||||
|
||||
val mlist = MotionList();
|
||||
getMotionByName("4")?.let { mlist.motions.add(it) }
|
||||
//getMotionByName("11")?.let { mlist.motions.add(it) }
|
||||
//getMotionByName("13")?.let { mlist.motions.add(it) }
|
||||
PreLoadAction(mlist.toJsonString(), loadFinishedFun)
|
||||
}
|
||||
|
||||
private fun update()
|
||||
{
|
||||
//下一个动画加载完成,并且当前动画已经播放完了 (刚开始的时候当前动画初始化为播放完成)
|
||||
if(isLoadFinished && isMotionFinished)
|
||||
{
|
||||
//切换到加载完成的动画
|
||||
isMotionFinished = false
|
||||
ChangeMotion(motionQueue[_motionIndex], animationFinishedFun, false)
|
||||
|
||||
isLoadFinished = false
|
||||
|
||||
//如果还有动画,则开始预先加载
|
||||
if(_motionIndex + 1 < motionQueue.size)
|
||||
{
|
||||
_motionIndex += 1
|
||||
PreLoadAction(motionQueue[_motionIndex], loadFinishedFun)
|
||||
}
|
||||
|
||||
}
|
||||
// //下一个动画加载完成,并且当前动画已经播放完了 (刚开始的时候当前动画初始化为播放完成)
|
||||
// if(isLoadFinished && isMotionFinished)
|
||||
// {
|
||||
// //切换到加载完成的动画
|
||||
// isMotionFinished = false
|
||||
// ChangeMotion(motionQueue[_motionIndex], animationFinishedFun, false)
|
||||
//
|
||||
// isLoadFinished = false
|
||||
//
|
||||
// //如果还有动画,则开始预先加载
|
||||
// if(_motionIndex + 1 < motionQueue.size)
|
||||
// {
|
||||
// _motionIndex += 1
|
||||
// PreLoadAction(motionQueue[_motionIndex], loadFinishedFun)
|
||||
// }
|
||||
//
|
||||
// }
|
||||
|
||||
}
|
||||
|
||||
@@ -120,12 +107,8 @@ class MakeupActivity : FaceActivity()
|
||||
}
|
||||
|
||||
|
||||
private fun getMotionByName(m: String): Motion {
|
||||
val motion = Motion().apply {
|
||||
type = m
|
||||
png_names = pngFilesByFolder?.get(m)?.fileList
|
||||
}
|
||||
return motion
|
||||
private fun getMotionByName(m: String): Motion? {
|
||||
return existMotions?.get(m);
|
||||
}
|
||||
|
||||
|
||||
@@ -133,20 +116,14 @@ class MakeupActivity : FaceActivity()
|
||||
override fun onTouchEvent(event: MotionEvent): Boolean {
|
||||
when (event.action) {
|
||||
MotionEvent.ACTION_UP -> {
|
||||
val motions: List<String> = listOf("4", "11", "13")
|
||||
PlayMotionQueue(motions)
|
||||
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
private inner class FolderInfo(
|
||||
val folderName: String,
|
||||
val fileList: List<String>
|
||||
)
|
||||
|
||||
private fun readPngFilenamesFromAssets(context: Context, filename: String): Map<String,FolderInfo> {
|
||||
val folderMap = mutableMapOf<String, FolderInfo>()
|
||||
private fun readMotionsFromAssets(context: Context, filename: String): Map<String,Motion> {
|
||||
val motionMap = mutableMapOf<String, Motion>()
|
||||
|
||||
try {
|
||||
context.assets.open(filename).use { inputStream ->
|
||||
@@ -158,30 +135,32 @@ class MakeupActivity : FaceActivity()
|
||||
}
|
||||
|
||||
val jsonObject = JSONObject(stringBuilder.toString())
|
||||
val folderIterator = jsonObject.keys()
|
||||
// 遍历JSON对象
|
||||
val keys = jsonObject.keys()
|
||||
while (keys.hasNext()) {
|
||||
val key = keys.next() // 获取键,即"Motion"的名字
|
||||
val motionObject = jsonObject.getJSONObject(key)
|
||||
|
||||
while (folderIterator.hasNext()) {
|
||||
val folderName = folderIterator.next()
|
||||
val fileArray = jsonObject.getJSONArray(folderName)
|
||||
val filesInFolder = mutableListOf<String>()
|
||||
|
||||
for (i in 0 until fileArray.length()) {
|
||||
filesInFolder.add(fileArray.getString(i))
|
||||
val frameList = mutableListOf<Frame>()
|
||||
val fileKeys = motionObject.keys()
|
||||
while (fileKeys.hasNext()) {
|
||||
val fileKey = fileKeys.next() // 获取每个文件名
|
||||
val fileObject = motionObject.getJSONObject(fileKey)
|
||||
val x = fileObject.getDouble("x").toFloat()
|
||||
val y = fileObject.getDouble("y").toFloat()
|
||||
frameList.add(Frame(fileKey, x, y))
|
||||
}
|
||||
|
||||
folderMap[folderName] = (FolderInfo(folderName, filesInFolder))
|
||||
motionMap[key] = Motion(key, frameList)
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
e.printStackTrace()
|
||||
}
|
||||
|
||||
return folderMap
|
||||
return motionMap
|
||||
}
|
||||
|
||||
private var pngFilesByFolder: Map<String, FolderInfo>? = null
|
||||
private fun FaceInit(context: Context) {
|
||||
pngFilesByFolder = readPngFilenamesFromAssets(context, "pic/motion_data.json")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user