mirror of
https://github.com/lizongying/my-tv.git
synced 2026-10-29 15:46:36 +08:00
129 lines
4.3 KiB
Kotlin
129 lines
4.3 KiB
Kotlin
package com.lizongying.mytv
|
|
|
|
import android.content.res.Resources
|
|
import android.os.Bundle
|
|
import android.os.Handler
|
|
import android.view.LayoutInflater
|
|
import android.view.View
|
|
import android.view.ViewGroup
|
|
import androidx.core.view.marginRight
|
|
import androidx.core.view.marginTop
|
|
import androidx.fragment.app.Fragment
|
|
import com.lizongying.mytv.databinding.ChannelBinding
|
|
import com.lizongying.mytv.models.TVViewModel
|
|
|
|
class ChannelFragment : Fragment() {
|
|
private var _binding: ChannelBinding? = null
|
|
private val binding get() = _binding!!
|
|
|
|
private val handler = Handler()
|
|
private val delay: Long = 5000
|
|
private var channel = 0
|
|
private var channelCount = 0
|
|
|
|
override fun onCreateView(
|
|
inflater: LayoutInflater, container: ViewGroup?,
|
|
savedInstanceState: Bundle?
|
|
): View {
|
|
_binding = ChannelBinding.inflate(inflater, container, false)
|
|
_binding!!.root.visibility = View.GONE
|
|
|
|
val application = requireActivity().applicationContext as MyTvApplication
|
|
|
|
val width = application.getWidth()
|
|
val height = application.getHeight()
|
|
|
|
val ratio = 16f / 9f
|
|
|
|
if (width.toFloat() / height > ratio) {
|
|
val x =
|
|
((Resources.getSystem().displayMetrics.widthPixels - height * ratio) / 2).toInt()
|
|
val originalLayoutParams =
|
|
binding.channel.layoutParams as ViewGroup.MarginLayoutParams
|
|
originalLayoutParams.rightMargin += x
|
|
binding.channel.layoutParams = originalLayoutParams
|
|
}
|
|
|
|
if (width.toFloat() / height < ratio) {
|
|
val y =
|
|
((height - Resources.getSystem().displayMetrics.widthPixels / ratio) / 2).toInt()
|
|
val originalLayoutParams =
|
|
binding.channel.layoutParams as ViewGroup.MarginLayoutParams
|
|
originalLayoutParams.topMargin += y
|
|
binding.channel.layoutParams = originalLayoutParams
|
|
}
|
|
|
|
binding.channel.layoutParams.width = application.px2Px(binding.channel.layoutParams.width)
|
|
binding.channel.layoutParams.height = application.px2Px(binding.channel.layoutParams.height)
|
|
val layoutParams = binding.channel.layoutParams as ViewGroup.MarginLayoutParams
|
|
layoutParams.topMargin = application.px2Px(binding.channel.marginTop)
|
|
layoutParams.rightMargin = application.px2Px(binding.channel.marginRight)
|
|
binding.channel.layoutParams = layoutParams
|
|
binding.content.textSize = application.px2PxFont(binding.content.textSize)
|
|
|
|
(activity as MainActivity).fragmentReady("ChannelFragment")
|
|
return binding.root
|
|
}
|
|
|
|
fun show(tvViewModel: TVViewModel) {
|
|
handler.removeCallbacks(hideRunnable)
|
|
handler.removeCallbacks(playRunnable)
|
|
binding.content.text = (tvViewModel.getTV().id.plus(1)).toString()
|
|
view?.visibility = View.VISIBLE
|
|
handler.postDelayed(hideRunnable, delay)
|
|
}
|
|
|
|
fun show(channel: String) {
|
|
if (channelCount > 1) {
|
|
return
|
|
}
|
|
channelCount++
|
|
this.channel = "${this.channel}$channel".toInt()
|
|
handler.removeCallbacks(hideRunnable)
|
|
handler.removeCallbacks(playRunnable)
|
|
if (channelCount < 2) {
|
|
binding.content.text = "${this.channel}"
|
|
view?.visibility = View.VISIBLE
|
|
handler.postDelayed(playRunnable, delay)
|
|
} else {
|
|
handler.postDelayed(playRunnable, 0)
|
|
}
|
|
}
|
|
|
|
override fun onResume() {
|
|
super.onResume()
|
|
if (view?.visibility == View.VISIBLE) {
|
|
handler.postDelayed(hideRunnable, delay)
|
|
}
|
|
}
|
|
|
|
override fun onPause() {
|
|
super.onPause()
|
|
handler.removeCallbacks(hideRunnable)
|
|
handler.removeCallbacks(playRunnable)
|
|
}
|
|
|
|
private val hideRunnable = Runnable {
|
|
binding.content.text = ""
|
|
view?.visibility = View.GONE
|
|
channel = 0
|
|
channelCount = 0
|
|
}
|
|
|
|
private val playRunnable = Runnable {
|
|
(activity as MainActivity).play(channel - 1)
|
|
binding.content.text = ""
|
|
view?.visibility = View.GONE
|
|
channel = 0
|
|
channelCount = 0
|
|
}
|
|
|
|
override fun onDestroyView() {
|
|
super.onDestroyView()
|
|
_binding = null
|
|
}
|
|
|
|
companion object {
|
|
private const val TAG = "ChannelFragment"
|
|
}
|
|
} |