实时视频UI

This commit is contained in:
2026-05-06 10:34:07 +08:00
parent 4e714ee7b6
commit a3115b6b86
25 changed files with 1212 additions and 1 deletions

View File

@@ -0,0 +1,133 @@
package com.example.defenseapplication.liveVideo
import android.content.Context
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.view.View
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.example.defenseapplication.databinding.LiveVideoActivityBinding
class LiveVideoActivity : AppCompatActivity() {
private lateinit var binding: LiveVideoActivityBinding
private var isVoiceEnabled = false
private var isLightOn = false
private var isRecording = false
private var recordTime = 0
private var recordHandler: Handler? = null
private var recordRunnable: Runnable? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = LiveVideoActivityBinding.inflate(layoutInflater)
setContentView(binding.root)
initListener()
}
private fun initListener() {
// 返回按钮
binding.btnBack.setOnClickListener {
finish()
}
// 菜单按钮
binding.ivMenu.setOnClickListener {
Toast.makeText(this, "菜单", Toast.LENGTH_SHORT).show()
}
// 全屏按钮
binding.ivFullscreen.setOnClickListener {
Toast.makeText(this, "全屏", Toast.LENGTH_SHORT).show()
}
// 语音按钮
binding.llVoice.setOnClickListener {
toggleVoice()
}
// 灯光按钮
binding.llLight.setOnClickListener {
toggleLight()
}
// 录像按钮
binding.llRecord.setOnClickListener {
toggleRecording()
}
// 告警日志区域
binding.llAlarmLog.setOnClickListener {
Toast.makeText(this, "查看告警日志", Toast.LENGTH_SHORT).show()
}
}
private fun toggleVoice() {
isVoiceEnabled = !isVoiceEnabled
if (isVoiceEnabled) {
binding.vVoiceActive.visibility = View.VISIBLE
binding.ivVoice.setColorFilter(resources.getColor(com.example.defenseapplication.R.color.green))
Toast.makeText(this, "语音已开启", Toast.LENGTH_SHORT).show()
} else {
binding.vVoiceActive.visibility = View.INVISIBLE
binding.ivVoice.clearColorFilter()
Toast.makeText(this, "语音已关闭", Toast.LENGTH_SHORT).show()
}
}
private fun toggleLight() {
isLightOn = !isLightOn
if (isLightOn) {
binding.ivLight.setImageResource(com.example.defenseapplication.R.drawable.light_on)
binding.ivLight.setColorFilter(resources.getColor(com.example.defenseapplication.R.color.green))
Toast.makeText(this, "灯光已开启", Toast.LENGTH_SHORT).show()
} else {
binding.ivLight.setImageResource(com.example.defenseapplication.R.drawable.low_light)
binding.ivLight.clearColorFilter()
Toast.makeText(this, "灯光已关闭", Toast.LENGTH_SHORT).show()
}
}
private fun toggleRecording() {
isRecording = !isRecording
if (isRecording) {
recordTime = 0
binding.tvRecordTime.text = "00"
startRecordTimer()
binding.ivRecord.setColorFilter(resources.getColor(com.example.defenseapplication.R.color.red))
Toast.makeText(this, "开始录像", Toast.LENGTH_SHORT).show()
} else {
stopRecordTimer()
binding.ivRecord.clearColorFilter()
Toast.makeText(this, "停止录像,共录制 ${recordTime}", Toast.LENGTH_SHORT).show()
}
}
private fun startRecordTimer() {
recordHandler = Handler(Looper.getMainLooper())
recordRunnable = object : Runnable {
override fun run() {
recordTime++
binding.tvRecordTime.text = String.format("%02d", recordTime)
recordHandler?.postDelayed(this, 1000)
}
}
recordHandler?.post(recordRunnable!!)
}
private fun stopRecordTimer() {
recordRunnable?.let { recordHandler?.removeCallbacks(it) }
recordHandler = null
recordRunnable = null
}
override fun onDestroy() {
super.onDestroy()
stopRecordTimer()
}
}