| | |
| | | import android.os.Looper |
| | | import android.os.Message |
| | | import android.util.Log |
| | | import android.view.View |
| | | import androidx.constraintlayout.motion.widget.MotionLayout |
| | | import androidx.core.os.bundleOf |
| | | import cn.sinata.xldutils.activity.ImagePagerActivity |
| | | import cn.sinata.xldutils.fragment.BaseFragment |
| | | import cn.sinata.xldutils.gone |
| | | import cn.sinata.xldutils.utils.SPUtils |
| | | import cn.sinata.xldutils.utils.myToast |
| | | import cn.sinata.xldutils.visible |
| | | import com.dollearn.student.R |
| | | import com.dollearn.student.utils.AudioUtils |
| | | import com.dollearn.student.utils.Const |
| | | import com.dollearn.student.utils.extention.clickDelay |
| | | import kotlinx.android.synthetic.main.fragment_include.* |
| | | import org.jetbrains.anko.support.v4.dip |
| | | import org.jetbrains.anko.support.v4.startActivity |
| | | |
| | | class IncludeFragment : BaseFragment(), AudioUtils.OnAudioStatusUpdateListener { |
| | | override fun contentViewId() = R.layout.fragment_include |
| | |
| | | private val PLAY_VOICE = 1 |
| | | private val TO_NEXT = 2 |
| | | |
| | | private val PLAY_ERROR = 3 |
| | | private val PLAY_RIGHT = 4 |
| | | |
| | | private val errorVoice by lazy { SPUtils.instance().getString(Const.EV) } |
| | | private val rightVoice by lazy { SPUtils.instance().getString(Const.RV) } |
| | | |
| | | private var rightPlaying = false |
| | | private var errorPlaying = false |
| | | |
| | | private val player by lazy { AudioUtils() } |
| | | private val group by lazy { arguments?.getInt("group")?:0 } |
| | | private val data by lazy { |
| | |
| | | private val act by lazy { requireActivity() as IncludeActivity } |
| | | |
| | | private var voiceIndex = -1 //点击播放的声音序号 0-5取值 |
| | | private val voiceViews by lazy { arrayListOf(cl_voice1,cl_voice2,cl_voice3,cl_voice4,cl_voice5,cl_voice6) } |
| | | private val voiceViews by lazy { arrayListOf(cl_voice1_real,cl_voice2_real,cl_voice3_real,cl_voice4_real,cl_voice5_real,cl_voice6_real) } |
| | | private var playing = false |
| | | |
| | | private val answerImg = arrayListOf<String>() //随机答案图片 |
| | | private var currentImage = 0 //当前作答的题号:2,4,5 |
| | | |
| | | private var nowVoiceView: View? = null |
| | | |
| | | private var isAutoPlaying = false //true自动播放流程 |
| | | |
| | | var right = true //true进入下一题为全对,false 一旦答错过就置为false,此时进入下一题,本题算做错误 |
| | | |
| | | override fun onFirstVisibleToUser() { |
| | | player.setOnAudioStatusUpdateListener(this) |
| | |
| | | super.handleMessage(msg) |
| | | when(msg.what){ |
| | | PLAY_VOICE->{ |
| | | playing = true |
| | | player.startPlayMusic(requireContext(),data!!.subjectList[group][voiceIndex].correct) |
| | | } |
| | | TO_NEXT->{ |
| | |
| | | (requireActivity() as IncludeActivity).next() |
| | | } |
| | | } |
| | | PLAY_RIGHT->{ |
| | | playing = true |
| | | rightPlaying = true |
| | | player.startPlayMusic(requireContext(),rightVoice) |
| | | } |
| | | PLAY_ERROR->{ |
| | | playing = true |
| | | errorPlaying = true |
| | | player.startPlayMusic(requireContext(),errorVoice) |
| | | } |
| | | } |
| | | } |
| | | } |
| | | data?.apply { |
| | | val originList = subjectList[group] |
| | | iv_1.setImageURI(originList[0].img) |
| | | iv_2.setImageURI(originList[1].img) |
| | | iv_4.setImageURI(originList[3].img) |
| | | answerImg.clear() |
| | | answerImg.add(originList[2].img) |
| | | answerImg.add(originList[4].img) |
| | | answerImg.add(originList[5].img) |
| | | cl_voice1_real.postDelayed({ |
| | | iv_1.setImageURI(originList[0].img) |
| | | iv_2.setImageURI(originList[1].img) |
| | | iv_4.setImageURI(originList[3].img) |
| | | // if (cl_1.width > dip(203)){ |
| | | // voiceViews.forEach { |
| | | // it.layoutParams.width = dip(159) |
| | | // it.layoutParams.height = dip(52) |
| | | // it.requestLayout() |
| | | // } |
| | | // } |
| | | voiceViews.forEach { it.visible() } |
| | | },500) |
| | | } |
| | | voiceViews.forEachIndexed { index, constraintLayout -> |
| | | if (index == 0) |
| | |
| | | constraintLayout.isEnabled = data!!.subjectList[group][index].listend |
| | | } |
| | | } |
| | | showVoiceEnable() |
| | | initClick() |
| | | showVoiceEnable() |
| | | isAutoPlaying = true |
| | | voiceViews[0].callOnClick() |
| | | } |
| | | |
| | | private fun initClick() { |
| | | voiceViews.forEachIndexed { index, constraintLayout -> |
| | | constraintLayout.clickDelay { |
| | | constraintLayout.setOnClickListener { |
| | | if (!playing){ |
| | | if (index!=0&&!data!!.subjectList[group][index-1].listend){ |
| | | myToast("请按顺序听语音") |
| | | return@clickDelay |
| | | return@setOnClickListener |
| | | } |
| | | voiceIndex = index |
| | | handler?.sendEmptyMessage(PLAY_VOICE) |
| | |
| | | } |
| | | |
| | | cl_answer_1.clickDelay { |
| | | if (currentImage == 0) |
| | | if (currentImage == 0||!data!!.subjectList[group][currentImage].listend) |
| | | return@clickDelay |
| | | val subject = data!!.subjectList[group][currentImage] |
| | | if (!subject.listend||subject.completed){ |
| | | return@clickDelay |
| | | } |
| | | act.totalCount++ |
| | | data!!.subjectList[group][currentImage].listend = false |
| | | if (answerImg[0] == subject.img){ |
| | | nowVoiceView = voiceViews[currentImage] |
| | | act.rightCount++ |
| | | data!!.subjectList[group][currentImage].completed = true |
| | | voiceViews[currentImage].callOnClick() |
| | | handler?.sendEmptyMessage(PLAY_RIGHT) |
| | | motion.transitionToEnd() |
| | | showResultAnim() |
| | | }else{ |
| | | right = false |
| | | handler?.sendEmptyMessage(PLAY_ERROR) |
| | | iv_error_1.visible() |
| | | iv_error_1.postDelayed({ |
| | | clearAnswerImg() |
| | |
| | | } |
| | | |
| | | cl_answer_2.clickDelay { |
| | | if (currentImage == 0) |
| | | if (currentImage == 0||!data!!.subjectList[group][currentImage].listend) |
| | | return@clickDelay |
| | | val subject = data!!.subjectList[group][currentImage] |
| | | if (!subject.listend||subject.completed){ |
| | | return@clickDelay |
| | | } |
| | | act.totalCount++ |
| | | |
| | | data!!.subjectList[group][currentImage].listend = false |
| | | if (answerImg[1] == subject.img){ |
| | | data!!.subjectList[group][currentImage].completed = true |
| | | voiceViews[currentImage].callOnClick() |
| | | nowVoiceView = voiceViews[currentImage] |
| | | act.rightCount++ |
| | | handler?.sendEmptyMessage(PLAY_RIGHT) |
| | | motion.transitionToEnd() |
| | | showResultAnim() |
| | | }else{ |
| | | right = false |
| | | handler?.sendEmptyMessage(PLAY_ERROR) |
| | | iv_error_2.visible() |
| | | iv_error_2.postDelayed({ |
| | | clearAnswerImg() |
| | |
| | | } |
| | | |
| | | cl_answer_3.clickDelay { |
| | | if (currentImage == 0) |
| | | if (currentImage == 0||!data!!.subjectList[group][currentImage].listend) |
| | | return@clickDelay |
| | | val subject = data!!.subjectList[group][currentImage] |
| | | if (!subject.listend||subject.completed){ |
| | | return@clickDelay |
| | | } |
| | | act.totalCount++ |
| | | |
| | | data!!.subjectList[group][currentImage].listend = false |
| | | if (answerImg[2] == subject.img){ |
| | | data!!.subjectList[group][currentImage].completed = true |
| | | voiceViews[currentImage].callOnClick() |
| | | nowVoiceView = voiceViews[currentImage] |
| | | act.rightCount++ |
| | | handler?.sendEmptyMessage(PLAY_RIGHT) |
| | | motion.transitionToEnd() |
| | | showResultAnim() |
| | | }else{ |
| | | right = false |
| | | handler?.sendEmptyMessage(PLAY_ERROR) |
| | | iv_error_3.visible() |
| | | iv_error_3.postDelayed({ |
| | | clearAnswerImg() |
| | | },3000) |
| | | } |
| | | } |
| | | |
| | | iv_full_1.clickDelay { |
| | | startActivity<ImagePagerActivity>("url" to arrayListOf(answerImg[0])) |
| | | } |
| | | iv_full_2.clickDelay { |
| | | startActivity<ImagePagerActivity>("url" to arrayListOf(answerImg[1])) |
| | | |
| | | } |
| | | iv_full_3.clickDelay { |
| | | startActivity<ImagePagerActivity>("url" to arrayListOf(answerImg[2])) |
| | | |
| | | } |
| | | } |
| | | |
| | | private fun showResultAnim(){ |
| | | val v = if (currentImage == 2) result_3 else if (currentImage == 4) result_5 else result_6 |
| | | v.visible() |
| | | v.postDelayed({v.gone()},3000) |
| | | } |
| | | |
| | | private fun showVoiceEnable(){ |
| | |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 答错后重置 |
| | | */ |
| | | private fun clearAnswerImg(){ |
| | | iv_answer_1.setImageURI("") |
| | | iv_answer_2.setImageURI("") |
| | | iv_answer_3.setImageURI("") |
| | | iv_3.setImageURI("") |
| | | iv_5.setImageURI("") |
| | | iv_6.setImageURI("") |
| | | if (currentImage == 2) |
| | | iv_3.setImageURI("") |
| | | if (currentImage == 4) |
| | | iv_5.setImageURI("") |
| | | if (currentImage == 5) |
| | | iv_6.setImageURI("") |
| | | iv_full_1.gone() |
| | | iv_full_2.gone() |
| | | iv_full_3.gone() |
| | |
| | | data!!.subjectList[group][currentImage].listend = false |
| | | } |
| | | |
| | | /** |
| | | * 答对后的重置 |
| | | */ |
| | | private fun hideAnswerImg(){ |
| | | iv_answer_1.setImageURI("") |
| | | iv_answer_2.setImageURI("") |
| | | iv_answer_3.setImageURI("") |
| | | iv_full_1.gone() |
| | | iv_full_2.gone() |
| | | iv_full_3.gone() |
| | | iv_error_1.gone() |
| | | iv_error_2.gone() |
| | | iv_error_3.gone() |
| | | } |
| | | |
| | | private fun showAnswerImg(){ |
| | | if (errorPlaying||rightPlaying) |
| | | return |
| | | answerImg.shuffle() |
| | | iv_answer_1.setImageURI(answerImg[0]) |
| | | iv_answer_2.setImageURI(answerImg[1]) |
| | |
| | | */ |
| | | fun recover(){ |
| | | Log.e(TAG,"回到上一题,恢复答题前的状态") |
| | | right = true |
| | | isAutoPlaying = false |
| | | motion.progress = 0f |
| | | clearAnswerImg() |
| | | currentImage = 0 |
| | |
| | | |
| | | override fun onStartPlay() { |
| | | playing = true |
| | | if (errorPlaying||rightPlaying) |
| | | return |
| | | when(voiceIndex){ |
| | | 0->{ |
| | | iv1_1.gone() |
| | |
| | | override fun onFinishPlay() { |
| | | playing = false |
| | | val subject = data!!.subjectList[group][voiceIndex] |
| | | subject.listend = true |
| | | when(voiceIndex){ |
| | | 0->{ |
| | | iv1_1.visible() |
| | |
| | | if (!subject.completed){ |
| | | currentImage = 2 |
| | | showAnswerImg() |
| | | }else{ |
| | | hideAnswerImg() |
| | | } |
| | | } |
| | | 3->{ |
| | |
| | | if (!subject.completed){ |
| | | currentImage = 4 |
| | | showAnswerImg() |
| | | }else{ |
| | | hideAnswerImg() |
| | | } |
| | | } |
| | | 5->{ |
| | |
| | | currentImage = 5 |
| | | showAnswerImg() |
| | | }else{ |
| | | hideAnswerImg() |
| | | Log.e(TAG,"本题全部答对,3秒后进入下一图") |
| | | handler?.sendEmptyMessageDelayed(TO_NEXT,3000) |
| | | handler?.sendEmptyMessageDelayed(TO_NEXT,500) //万俊杰:缩短为0.5秒 |
| | | } |
| | | } |
| | | } |
| | | if (errorPlaying) |
| | | errorPlaying = false |
| | | else if (rightPlaying) { |
| | | rightPlaying = false |
| | | data!!.subjectList[group][currentImage].completed = true |
| | | nowVoiceView?.callOnClick() |
| | | }else{ |
| | | subject.listend = true |
| | | if (isAutoPlaying) |
| | | when(voiceIndex){ |
| | | 0-> voiceViews[1].callOnClick() |
| | | 1-> voiceViews[2].callOnClick() |
| | | 3-> voiceViews[4].callOnClick() |
| | | 2,4,5-> isAutoPlaying = false |
| | | } |
| | | else if (subject.completed){ |
| | | when(voiceIndex){ |
| | | 2->{ |
| | | isAutoPlaying = true |
| | | voiceViews[3].callOnClick() |
| | | } |
| | | 4->{ |
| | | voiceViews[5].callOnClick() |
| | | } |
| | | } |
| | | } |
| | | } |
| | | showVoiceEnable() |
| | | } |
| | | |
| | | override fun onPause() { |
| | | super.onPause() |
| | | player.stopPlayMusic() |
| | | } |
| | | |
| | | override fun onDestroy() { |
| | | super.onDestroy() |
| | | handler?.removeCallbacksAndMessages(null) |
| | | } |
| | | } |