From e93550a24b5044f98f7e2377686f018feaf99fde Mon Sep 17 00:00:00 2001 From: zxj <275873859@qq.com> Date: Mon, 11 Aug 2025 17:53:20 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A4=84=E7=90=86=E5=89=AF=E5=B1=8F=E5=8A=A0?= =?UTF-8?q?=E8=BD=BD=E5=A4=B1=E8=B4=A5=E5=AF=BC=E8=87=B4=E7=9A=84=E5=B4=A9?= =?UTF-8?q?=E6=BA=83=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../sw/dualscreen/activity/MainActivity.kt | 26 ++++++++++--------- .../layout/presentation_secondary_screen.xml | 1 + 2 files changed, 15 insertions(+), 12 deletions(-) diff --git a/app/src/main/java/com/sw/dualscreen/activity/MainActivity.kt b/app/src/main/java/com/sw/dualscreen/activity/MainActivity.kt index e029e5c..da6cc3f 100644 --- a/app/src/main/java/com/sw/dualscreen/activity/MainActivity.kt +++ b/app/src/main/java/com/sw/dualscreen/activity/MainActivity.kt @@ -46,7 +46,7 @@ class MainActivity : BaseActivity() { private val viewModel by viewModels() private var imageCapture: ImageCapture? = null private var isAnalyzing = true // 控制是否进行图像分析 - private lateinit var presentation: SecondaryScreenPresentation + private var presentation: SecondaryScreenPresentation? = null private val executor = Executors.newSingleThreadExecutor() private lateinit var adapter: GenericItemAdapter private var checkedItem: FoodInfo? = null @@ -74,7 +74,7 @@ class MainActivity : BaseActivity() { private fun initData() { viewModel.getEquipmentToken() - presentation.step1() + presentation?.step1() } private fun initView() { @@ -106,7 +106,7 @@ class MainActivity : BaseActivity() { position: Int, id: Long ) { - presentation.updateMealPickupMode(position) + presentation?.updateMealPickupMode(position) } override fun onNothingSelected(parent: AdapterView<*>?) {} @@ -129,7 +129,7 @@ class MainActivity : BaseActivity() { fun updateCurrentFood(foodInfo: FoodInfo?) { if (foodInfo != null) { - presentation.updateFood(foodInfo) + presentation?.updateFood(foodInfo) binding.tvFoodName.text = foodInfo.foodName binding.previewView.visibility = View.GONE binding.ivImg.visibility = View.VISIBLE @@ -137,7 +137,7 @@ class MainActivity : BaseActivity() { if (TextUtils.isEmpty(imgUrl)) { takePhoto { imgUri -> GlideUtils.loadRoundCornerImage(this, imgUri, binding.ivImg, 12) - presentation.updateFood(foodInfo.copy(imgUrl = imgUri.toString())) + presentation?.updateFood(foodInfo.copy(imgUrl = imgUri.toString())) } } else { GlideUtils.loadRoundCornerImage(this, foodInfo.imgUrl, binding.ivImg, 12) @@ -163,7 +163,7 @@ class MainActivity : BaseActivity() { } SensorScaleUtils.startScale { weight -> Timber.d("registerDataChange weight = $weight") - presentation.updateWeight(weight) + presentation?.updateWeight(weight) if (weight > 0.0) { if (canIdentify) { canIdentify = false @@ -222,7 +222,7 @@ class MainActivity : BaseActivity() { // 仅在步骤1时传递 if (isAnalyzing) { val bitmap = imageProxy.toBitmap() - presentation.updateImage(bitmap) // 更新副屏 + presentation?.updateImage(bitmap) // 更新副屏 } imageProxy.close() // 必须关闭以释放资源 } @@ -262,9 +262,9 @@ class MainActivity : BaseActivity() { if (isAnalyzing) return isAnalyzing = true imageAnalysis?.setAnalyzer(executor) { imageProxy -> - if (presentation.currentStep == 1) { + if (presentation?.currentStep == 1) { val bitmap = imageProxy.toBitmap() - presentation.updateImage(bitmap) + presentation?.updateImage(bitmap) } imageProxy.close() } @@ -356,19 +356,21 @@ class MainActivity : BaseActivity() { viewModel = viewModel, recognizeViewModel = viewModels().value ) - presentation.setStepChangeCallback { step -> + presentation!!.setStepChangeCallback { step -> if (step == 1) { updateCurrentFood(null) bottomSheetDialog?.dismiss() } } - presentation.show() + presentation!!.show() + } else { + ToastUtils.showToast("获取副屏失败") } } override fun onDestroy() { SensorScaleUtils.closeScale() - presentation.dismiss() + presentation?.dismiss() super.onDestroy() } } \ No newline at end of file diff --git a/app/src/main/res/layout/presentation_secondary_screen.xml b/app/src/main/res/layout/presentation_secondary_screen.xml index efe1961..a3f7e19 100644 --- a/app/src/main/res/layout/presentation_secondary_screen.xml +++ b/app/src/main/res/layout/presentation_secondary_screen.xml @@ -121,6 +121,7 @@ android:layout_height="486dp" android:layout_gravity="bottom" android:background="@drawable/rounded_border_transparent" + android:orientation="vertical" android:gravity="center">