@@ -2,6 +2,7 @@ package com.lizongying.mytv
22
33import android.os.Bundle
44import android.os.Handler
5+ import android.util.Log
56import android.view.LayoutInflater
67import android.view.View
78import android.view.ViewGroup
@@ -16,14 +17,47 @@ class ChannelFragment : Fragment() {
1617 private val handler = Handler ()
1718 private val delay: Long = 3000
1819 private var channel = 0
20+ private var channelCount = 0
1921
2022 override fun onCreateView (
2123 inflater : LayoutInflater , container : ViewGroup ? ,
2224 savedInstanceState : Bundle ?
2325 ): View {
2426 _binding = ChannelBinding .inflate(inflater, container, false )
2527 _binding !! .root.visibility = View .GONE
26- (activity as MainActivity ).fragmentReady()
28+
29+ val activity = requireActivity()
30+ val application = activity.applicationContext as MyApplication
31+ val displayMetrics = application.getDisplayMetrics()
32+
33+ displayMetrics.density
34+
35+ var screenWidth = displayMetrics.widthPixels
36+ var screenHeight = displayMetrics.heightPixels
37+ if (screenHeight > screenWidth) {
38+ screenWidth = displayMetrics.heightPixels
39+ screenHeight = displayMetrics.widthPixels
40+ }
41+
42+ val ratio = 16f / 9f
43+
44+ if (screenWidth / screenHeight > ratio) {
45+ val x = ((screenWidth - screenHeight * ratio) / 2 ).toInt()
46+ val originalLayoutParams =
47+ binding.channelFragment.layoutParams as ViewGroup .MarginLayoutParams
48+ originalLayoutParams.rightMargin + = x
49+ binding.channelFragment.layoutParams = originalLayoutParams
50+ }
51+
52+ if (screenWidth / screenHeight < ratio) {
53+ val y = ((screenHeight - screenWidth / ratio) / 2 ).toInt()
54+ val originalLayoutParams =
55+ binding.channelFragment.layoutParams as ViewGroup .MarginLayoutParams
56+ originalLayoutParams.topMargin + = y
57+ binding.channelFragment.layoutParams = originalLayoutParams
58+ }
59+
60+ (activity as MainActivity ).fragmentReady(" ChannelFragment" )
2761 return binding.root
2862 }
2963
@@ -36,11 +70,17 @@ class ChannelFragment : Fragment() {
3670 }
3771
3872 fun show (channel : String ) {
39- this .channel = " ${binding.channelContent.text}$channel " .toInt()
73+ if (channelCount > 1 ) {
74+ return
75+ }
76+ channelCount++
77+ Log .i(TAG , " channelCount ${channelCount} " )
78+ this .channel = " ${this .channel}$channel " .toInt()
79+ Log .i(TAG , " this.channel ${this .channel} " )
4080 handler.removeCallbacks(hideRunnable)
4181 handler.removeCallbacks(playRunnable)
42- if (binding.channelContent.text == " " ) {
43- binding.channelContent.text = channel
82+ if (channelCount < 2 ) {
83+ binding.channelContent.text = " ${ this . channel} "
4484 view?.visibility = View .VISIBLE
4585 handler.postDelayed(playRunnable, delay)
4686 } else {
@@ -64,12 +104,18 @@ class ChannelFragment : Fragment() {
64104 private val hideRunnable = Runnable {
65105 binding.channelContent.text = " "
66106 view?.visibility = View .GONE
107+ channel = 0
108+ channelCount = 0
109+ Log .i(TAG , " hideRunnable" )
67110 }
68111
69112 private val playRunnable = Runnable {
70113 (activity as MainActivity ).play(channel - 1 )
71114 binding.channelContent.text = " "
72115 view?.visibility = View .GONE
116+ channel = 0
117+ channelCount = 0
118+ Log .i(TAG , " playRunnable" )
73119 }
74120
75121 override fun onDestroyView () {
0 commit comments