Browse Source

1.添加游戏进度弹窗

王鹏鹏 2 years ago
parent
commit
19ac68b279

+ 27 - 2
game/src/main/java/com/yingyangfly/game/dialog/GameResultDialog.kt

@@ -1,11 +1,14 @@
 package com.yingyangfly.game.dialog
 
+import android.annotation.SuppressLint
 import android.os.Bundle
+import android.text.TextUtils
 import android.view.LayoutInflater
 import android.view.View
 import android.view.ViewGroup
 import androidx.appcompat.widget.AppCompatButton
 import androidx.appcompat.widget.AppCompatTextView
+import androidx.constraintlayout.widget.ConstraintLayout
 import androidx.fragment.app.DialogFragment
 import com.gyf.immersionbar.BarHide
 import com.gyf.immersionbar.ktx.immersionBar
@@ -17,11 +20,16 @@ import com.yingyangfly.game.R
  */
 class GameResultDialog : DialogFragment() {
 
+    private var gameLayout: ConstraintLayout? = null
     private var tvGameLevel: AppCompatTextView? = null
     private var tvTotalGameScore: AppCompatTextView? = null
     private var tvGameScore: AppCompatTextView? = null
     private var btnHome: AppCompatButton? = null
     private var btnContinueThisLevel: AppCompatButton? = null
+    private var curLevel: String = ""
+    private var curLevelScore: String = ""
+    private var isPass: String = ""
+    private var totalScore: String = ""
 
     override fun onCreate(savedInstanceState: Bundle?) {
         super.onCreate(savedInstanceState)
@@ -29,8 +37,11 @@ class GameResultDialog : DialogFragment() {
         dialog?.setCanceledOnTouchOutside(true)
     }
 
-    fun setData() {
-
+    fun setData(curLevel: String, totalScore: String, curLevelScore: String, isPass: String) {
+        this.curLevel = curLevel
+        this.totalScore = totalScore
+        this.curLevelScore = curLevelScore
+        this.isPass = isPass
     }
 
     override fun onStart() {
@@ -55,14 +66,28 @@ class GameResultDialog : DialogFragment() {
             activity, R.layout.dialog_game_result, container, 1194, 834
         )
         findId(rootView)
+        initData()
         return rootView
     }
 
     private fun findId(rootView: View) {
+        gameLayout = rootView.findViewById(R.id.gameLayout)
         tvGameLevel = rootView.findViewById(R.id.tvGameLevel)
         tvTotalGameScore = rootView.findViewById(R.id.tvTotalGameScore)
         tvGameScore = rootView.findViewById(R.id.tvGameScore)
         btnHome = rootView.findViewById(R.id.btnHome)
         btnContinueThisLevel = rootView.findViewById(R.id.btnContinueThisLevel)
     }
+
+    @SuppressLint("SetTextI18n")
+    private fun initData() {
+        if (TextUtils.equals("1", isPass)) {
+            gameLayout?.setBackgroundResource(R.mipmap.icon_game_fail)
+        } else {
+            gameLayout?.setBackgroundResource(R.mipmap.icon_game_success)
+        }
+        tvGameLevel?.text = "当前关卡:$curLevel"
+        tvTotalGameScore?.text = "总得分:" + totalScore + "分"
+        tvGameScore?.text = "本关得分:" + curLevelScore + "分"
+    }
 }

+ 24 - 0
game/src/main/java/com/yingyangfly/game/play/PlayGameActivity.kt

@@ -6,6 +6,7 @@ import android.os.Bundle
 import android.util.Log
 import android.view.MotionEvent
 import android.view.View
+import android.view.ViewGroup
 import android.webkit.WebResourceRequest
 import android.webkit.WebSettings
 import android.webkit.WebView
@@ -22,6 +23,7 @@ import com.yingyangfly.baselib.utils.LiveEventBusUtil
 import com.yingyangfly.baselib.utils.RxBusCodes
 import com.yingyangfly.game.R
 import com.yingyangfly.game.databinding.ActivityPlayGameBinding
+import com.yingyangfly.game.dialog.GameResultDialog
 import com.yingyangfly.game.entity.GameBean
 import com.yingyangfly.game.entity.GameDataBean
 import com.yingyangfly.game.utils.AndroidToJs
@@ -35,6 +37,7 @@ class PlayGameActivity : BaseMVVMActivity<ActivityPlayGameBinding, PlayGameViewM
 
     private var gameBean: GameBean? = null
     private lateinit var webSettings: WebSettings
+    private var gameResultDialog: GameResultDialog? = null
 
     override fun onCreate(savedInstanceState: Bundle?) {
         gameBean = intent.getSerializableExtra("bean") as GameBean
@@ -99,6 +102,9 @@ class PlayGameActivity : BaseMVVMActivity<ActivityPlayGameBinding, PlayGameViewM
 
             }
         }
+        gameResultDialog = GameResultDialog()
+        gameResultDialog?.setData("2", "120", "50", "1")
+        gameResultDialog?.show(supportFragmentManager, "gameResultDialog")
     }
 
     /**
@@ -191,4 +197,22 @@ class PlayGameActivity : BaseMVVMActivity<ActivityPlayGameBinding, PlayGameViewM
         }
         return true
     }
+
+    override fun onDestroy() {
+        destoryWebView()
+        super.onDestroy()
+    }
+
+    /**
+     * 结束webview清空缓存
+     */
+    private fun destoryWebView() {
+        binding.webView.stopLoading() // 停止加载
+        binding.webView.clearCache(true) // 清除缓存
+        binding.webView.clearHistory() // 清楚历史
+        binding.webView.loadUrl("about:blank");
+        binding.webView.onPause();
+        binding.webView.removeAllViews() // 移除webview上子view
+        binding.webView.destroy() // 销毁WebView自身。
+    }
 }

+ 1 - 1
game/src/main/res/layout/dialog_game_result.xml

@@ -7,9 +7,9 @@
     tools:ignore="ResourceName">
 
     <androidx.constraintlayout.widget.ConstraintLayout
+        android:id="@+id/gameLayout"
         android:layout_width="@dimen/divider_656px"
         android:layout_height="@dimen/divider_464px"
-        android:background="@mipmap/icon_game_fail"
         app:layout_constraintBottom_toBottomOf="parent"
         app:layout_constraintEnd_toEndOf="parent"
         app:layout_constraintStart_toStartOf="parent"

BIN
game/src/main/res/mipmap-xxhdpi/icon_game_success.png