添加设置IP和端口的对话框

This commit is contained in:
jackfiled 2023-01-30 18:13:42 +08:00
parent 89ecd03f4f
commit 780ba6b580
8 changed files with 123 additions and 3 deletions

View File

@ -38,6 +38,8 @@ dependencies {
implementation 'androidx.appcompat:appcompat:1.5.1'
implementation 'com.google.android.material:material:1.7.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
implementation 'androidx.core:core-ktx:latest.release'
implementation 'androidx.core:core-ktx:+'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.0'

View File

@ -7,6 +7,7 @@ import android.util.Log
import android.util.Size
import android.view.*
import androidx.appcompat.widget.Toolbar
import androidx.fragment.app.DialogFragment
import com.google.mediapipe.components.CameraHelper.CameraFacing
import com.google.mediapipe.components.CameraXPreviewHelper
import com.google.mediapipe.components.ExternalTextureConverter
@ -19,7 +20,7 @@ import com.google.mediapipe.framework.ProtoUtil
import com.google.mediapipe.glutil.EglManager
import com.google.protobuf.InvalidProtocolBufferException
class MainActivity : AppCompatActivity() {
class MainActivity : AppCompatActivity(), SendDialogFragment.NoticeDialogListener {
// 一些在设置MediaPipe时会用到的字符串常量
private val tag = "MainActivity"
private val binaryGraphName = "pose_tracking_gpu.binarypb"
@ -55,7 +56,13 @@ class MainActivity : AppCompatActivity() {
Log.i(tag, "Flip Camera Button Pressed.")
flipCamera()
true
} else -> {
}
R.id.action_show_SetIpPortDialog -> {
Log.i(tag, "show SetIpPortDialog Button Pressed.")
showPortSetDialog()
true
}
else -> {
super.onMenuItemSelected(it.itemId, it)
}
}
@ -136,6 +143,14 @@ class MainActivity : AppCompatActivity() {
PermissionHelper.onRequestPermissionsResult(requestCode, permissions, grantResults)
}
override fun onDialogPositiveClick(dialog: DialogFragment) {
Log.i(tag, "Confirm Button Clicked.")
}
override fun onDialogNegativeClick(dialog: DialogFragment) {
Log.i(tag, "Cancel Button Clicked.")
}
/**
* 启动摄像机
*/
@ -257,6 +272,12 @@ class MainActivity : AppCompatActivity() {
)
}
private fun showPortSetDialog() {
val dialogFragment = SendDialogFragment()
dialogFragment.show(supportFragmentManager, "SetIpPortDialog")
}
/**
* 格式化输出捕捉的标志位置
*/

View File

@ -0,0 +1,46 @@
package top.rrricardo.motioncapture
import android.app.AlertDialog
import android.app.Dialog
import android.content.Context
import android.os.Bundle
import androidx.fragment.app.DialogFragment
class SendDialogFragment : DialogFragment() {
// 用于将点击事件传递回宿主Activity的接口
interface NoticeDialogListener {
fun onDialogPositiveClick(dialog: DialogFragment)
fun onDialogNegativeClick(dialog: DialogFragment)
}
internal lateinit var listener: NoticeDialogListener
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
return activity?.let {
val builder = AlertDialog.Builder(it)
val inflater = requireActivity().layoutInflater
builder.setView(inflater.inflate(R.layout.dialog_set_port, null))
.setPositiveButton(R.string.confirm_button
) { _, _ -> listener.onDialogPositiveClick(this) }
.setNegativeButton(R.string.cancel_button
) { _, _ -> listener.onDialogNegativeClick(this) }
builder.create()
}?: throw java.lang.IllegalStateException("Activity cannot be null")
}
override fun onAttach(context: Context) {
super.onAttach(context)
// 确认宿主Activity实现了点击事件接口
try {
listener = context as NoticeDialogListener
} catch (e: java.lang.ClassCastException) {
// 没有实现
throw java.lang.ClassCastException("$context must implement NoticeDialogListener.")
}
}
}

View File

@ -0,0 +1,5 @@
<vector android:height="24dp" android:tint="#000000"
android:viewportHeight="24" android:viewportWidth="24"
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="@android:color/white" android:pathData="M12,5c-3.87,0 -7,3.13 -7,7h2c0,-2.76 2.24,-5 5,-5s5,2.24 5,5h2c0,-3.87 -3.13,-7 -7,-7zM13,14.29c0.88,-0.39 1.5,-1.26 1.5,-2.29 0,-1.38 -1.12,-2.5 -2.5,-2.5S9.5,10.62 9.5,12c0,1.02 0.62,1.9 1.5,2.29v3.3L7.59,21 9,22.41l3,-3 3,3L16.41,21 13,17.59v-3.3zM12,1C5.93,1 1,5.93 1,12h2c0,-4.97 4.03,-9 9,-9s9,4.03 9,9h2c0,-6.07 -4.93,-11 -11,-11z"/>
</vector>

View File

@ -0,0 +1,34 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="64dp"
android:text="@string/input_ip_port_hint"
android:gravity="center">
</TextView>
<EditText
android:id="@+id/targetIP"
android:inputType="number"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:hint="@string/input_ip_hint"
android:autofillHints="IP Address">
</EditText>
<EditText
android:id="@+id/targetPort"
android:inputType="number"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:hint="@string/input_port_hint"
android:autofillHints="Port Number">
</EditText>
</LinearLayout>

View File

@ -8,4 +8,11 @@
android:icon="@drawable/camera_flip"
android:title="@string/action_flip_camera_name">
</item>
<item
android:id="@+id/action_show_SetIpPortDialog"
app:showAsAction="ifRoom"
android:icon="@drawable/baseline_settings_input_antenna"
android:title="@string/input_ip_port_hint">
</item>
</menu>

View File

@ -2,4 +2,9 @@
<string name="app_name">MotionCapture</string>
<string name="no_camera_access_hint">Please Make Sure the Camera Permission is Given!</string>
<string name="action_flip_camera_name">Flip Camera</string>
<string name="input_ip_port_hint">Input Target IP and Port!</string>
<string name="input_ip_hint">Input IP Address</string>
<string name="input_port_hint">Input Port Number</string>
<string name="confirm_button">Confirm</string>
<string name="cancel_button">Cancel</string>
</resources>

View File

@ -2,5 +2,5 @@
plugins {
id 'com.android.application' version '7.4.0' apply false
id 'com.android.library' version '7.4.0' apply false
id 'org.jetbrains.kotlin.android' version '1.7.20' apply false
id 'org.jetbrains.kotlin.android' version '1.8.0' apply false
}