lmw
2024-08-01 a036ac0c6c8d87f36e68f88f05231c3be23fcc4b
app/src/main/java/com/dollearn/student/ui/home/MemoryActivity.kt
@@ -1,10 +1,13 @@
package com.dollearn.student.ui.home
import android.os.Handler
import android.util.Log
import androidx.core.os.bundleOf
import androidx.recyclerview.widget.GridLayoutManager
import cn.sinata.xldutils.gone
import cn.sinata.xldutils.utils.SPUtils
import cn.sinata.xldutils.utils.showAllowingStateLoss
import cn.sinata.xldutils.visible
import com.dollearn.student.R
import com.dollearn.student.dialog.TipDialog
import com.dollearn.student.network.HttpManager
@@ -17,7 +20,13 @@
import com.dollearn.student.utils.Const
import com.dollearn.student.utils.event.EmptyEvent
import kotlinx.android.synthetic.main.activity_memory.*
import kotlinx.android.synthetic.main.item_card.view.*
import org.greenrobot.eventbus.EventBus
import org.jetbrains.anko.imageResource
import kotlin.math.abs
import kotlin.math.max
import kotlin.math.min
import kotlin.math.sqrt
class MemoryActivity : TransparentStatusBarActivity(), AudioUtils.OnAudioStatusUpdateListener {
    override fun setContentView() = R.layout.activity_memory
@@ -27,17 +36,18 @@
    }
    private val list = arrayListOf<Card>()
    private val adapter = CardAdapter(list)
    val TAG = "Memory====>"
    private var lastTime = 600 //剩余秒
    private var totalCount = 0
    private var rightCount = 0
    private var handler: Handler? = null
    var handler: Handler? = null
    var time = 0 //学习秒数
    private val PLAY_VOICE = 1
    val PLAY_VOICE = 1
    private val STUDY_TIME = 2
    val PLAY_RIGHT = 4
@@ -51,7 +61,7 @@
    private val player by lazy { AudioUtils() }
    private var index = 0
    var index = 0
    var playing = false
@@ -97,13 +107,17 @@
                            sendEmptyMessageDelayed(STUDY_TIME,1000)
                    }
                    PLAY_VOICE->{
                        playing = true
                        player.startPlayMusic(this@MemoryActivity,list[index].url)
                        rv_list.getChildAt(index).iv_play.setImageResource(R.mipmap.playing_blue)
                    }
                    PLAY_RIGHT->{
                        playing = true
                        rightPlaying = true
                        player.startPlayMusic(this@MemoryActivity,rightVoice)
                    }
                    PLAY_ERROR->{
                        playing = true
                        errorPlaying = true
                        player.startPlayMusic(this@MemoryActivity,errorVoice)
                    }
@@ -114,11 +128,41 @@
        lastTime = data.data.answerTime //倒计时时间
        tv_timer.text = "${lastTime}S"
        handler?.sendEmptyMessageDelayed(STUDY_TIME,1000)
        rv_list.layoutManager = GridLayoutManager(this,5)
        rv_list.adapter = adapter
        list.addAll(data.photoList.map { Card(it.id,1,it.photo) })
        list.addAll(data.voiceList.map { Card(it.id,2,it.voice) })
        list.shuffle()
        rv_list.postDelayed({
            initRecycler(list.size)
        },500)
    }
    /**
     * 需要计算行列数
     */
    private fun initRecycler(total: Int){
        var bestRows = 1
        var bestColumns: Int = total
        var i = 1
        while (i <= sqrt(total.toDouble())) {
            val rows = i
            val columns: Int = if (total%i>0) (total / i)+1 else total / i
            // Calculate the absolute difference between rows and columns
            val diff = abs(rows - columns)
            // Update the best rows and columns if this pair has a smaller difference
            if (diff < abs(bestRows - bestColumns)) {
                bestRows = rows
                bestColumns = columns
            }
            i++
        }
        Log.e(TAG,"总数:$total,计算得到行数:${min(bestColumns,bestRows)},列数:${max(bestColumns,bestRows)}")
        val lineHeight = rv_list.height.toDouble() / min(bestColumns, bestRows)
        val lineWidth = rv_list.width.toDouble() / max(bestColumns, bestRows)
        Log.e(TAG,"高度:${rv_list.height},计算得到行高:${lineHeight},宽度:${rv_list.width},计算得到行宽:${lineWidth}")
        rv_list.layoutManager = GridLayoutManager(this, max(bestColumns,bestRows))
        adapter.height = lineHeight.toInt()
        adapter.widgh = lineWidth.toInt()
        adapter.notifyDataSetChanged()
    }
@@ -143,6 +187,20 @@
            if (card1.type!=card2.type&&card1.id == card2.id){//匹配成功
                handler?.sendEmptyMessage(PLAY_RIGHT)
                rightCount++
                rv_list.getChildAt(adapter.position1).iv_result?.apply {
                    imageResource = R.mipmap.successs
                    visible()
                    postDelayed({
                        gone()
                    },1500)
                }
                rv_list.getChildAt(adapter.position2).iv_result?.apply {
                    imageResource = R.mipmap.successs
                    visible()
                    postDelayed({
                        gone()
                    },1500)
                }
                tv_timer.postDelayed({
                    if (list.filter { !it.isBack }.size == list.size){ //全部答对
                        tv_exit.text = "提交"
@@ -151,6 +209,20 @@
                },500)
            }else{
                handler?.sendEmptyMessage(PLAY_ERROR)
                rv_list.getChildAt(adapter.position1).iv_result?.apply {
                    imageResource = R.mipmap.zhifushibai
                    visible()
                    postDelayed({
                        gone()
                    },1500)
                }
                rv_list.getChildAt(adapter.position2).iv_result?.apply {
                    imageResource = R.mipmap.zhifushibai
                    visible()
                    postDelayed({
                        gone()
                    },1500)
                }
            }
        }
    }
@@ -189,8 +261,10 @@
        playing = false
        if (errorPlaying){
            errorPlaying = false
            playing = true
            tv_timer.postDelayed({
                adapter.bothToBack()
                playing = false //防止翻转过程中点击,造成缓存view被销毁
            },500)
        }else if (rightPlaying){
            rightPlaying = false
@@ -202,6 +276,7 @@
            adapter.releaseHolder()
            handler?.sendEmptyMessage(PLAY_VOICE)
        }else{
            rv_list.getChildAt(index).iv_play.setImageResource(R.mipmap.play_blue)
            checkResult()
        }
    }