package com.dollearn.student.ui.home
|
|
import android.animation.Animator
|
import android.animation.ObjectAnimator
|
import android.animation.PropertyValuesHolder
|
import android.animation.ValueAnimator
|
import android.os.Handler
|
import android.os.Looper
|
import android.os.Message
|
import android.util.Log
|
import android.view.View
|
import android.view.animation.LinearInterpolator
|
import androidx.constraintlayout.widget.ConstraintLayout
|
import androidx.core.os.bundleOf
|
import cn.sinata.xldutils.fragment.BaseFragment
|
import cn.sinata.xldutils.gone
|
import cn.sinata.xldutils.invisible
|
import cn.sinata.xldutils.visible
|
import com.dollearn.student.R
|
import com.dollearn.student.utils.AudioUtils
|
import com.dollearn.student.utils.extention.clickDelay
|
import kotlinx.android.synthetic.main.fragment_q_a.*
|
import org.jetbrains.anko.support.v4.dip
|
|
class QAFragment:BaseFragment(), AudioUtils.OnAudioStatusUpdateListener {
|
override fun contentViewId() = R.layout.fragment_q_a
|
|
private val TAG = "QA=======》"
|
|
private var handler: Handler? = null
|
private val PLAY_VOICE = 1
|
private val TO_NEXT = 2
|
|
private val player by lazy { AudioUtils() }
|
private val group by lazy { arguments?.getInt("group")?:0 }
|
private val data by lazy {
|
(requireActivity() as QAActivity).data
|
}
|
private val list by lazy { //题组内图片
|
data!!.subjectList[group]
|
}
|
|
private val act by lazy { requireActivity() as QAActivity }
|
|
private val voiceViews by lazy { arrayListOf(cl_voice_1,cl_voice_2,cl_voice_3,cl_voice_4) } //语音蓝条
|
private val v1Views by lazy { arrayListOf(iv1_1,iv1_2,iv1_3,iv1_4,iv1_answer_1,iv1_answer_2,iv1_answer_3) }
|
private val v2Views by lazy { arrayListOf(iv2_1,iv2_2,iv2_3,iv2_4,iv2_answer_1,iv2_answer_2,iv2_answer_3) }
|
private val v3Views by lazy { arrayListOf(iv3_1,iv3_2,iv3_3,iv3_4) }
|
private val playingViews by lazy { arrayListOf(iv_playing_1,iv_playing_2,iv_playing_3,iv_playing_4,iv_playing_answer_1,iv_playing_answer_2,iv_playing_answer_3) }
|
|
private var voiceIndex = -1 //点击播放的声音序号 0-3取值
|
private var questionIndex = -1 //当前题号
|
|
private var playing = false
|
|
private var layoutChanged = false
|
|
private var answerList = arrayListOf<String>() //右侧答案语音
|
|
|
override fun onFirstVisibleToUser() {
|
player.setOnAudioStatusUpdateListener(this)
|
player.stopPlayMusic()
|
handler = object : Handler(Looper.getMainLooper()){
|
override fun handleMessage(msg: Message) {
|
super.handleMessage(msg)
|
when(msg.what){
|
PLAY_VOICE->{
|
if (voiceIndex in 0..3)
|
player.startPlayMusic(requireContext(),list[voiceIndex].correct)
|
else
|
player.startPlayMusic(requireContext(),answerList[voiceIndex-4])
|
}
|
TO_NEXT->{
|
if (isAdded&&!playing){
|
(requireActivity() as QAActivity).next()
|
}
|
}
|
}
|
}
|
}
|
iv_1.setImageURI(list[0].img)
|
iv_2.setImageURI(list[1].img)
|
iv_3.setImageURI(list[2].img)
|
iv_4.setImageURI(list[3].img)
|
showVoiceUi()
|
|
rl_check_1.clickDelay {
|
if (list[questionIndex].completed) //已作答
|
return@clickDelay
|
act.totalCount++
|
if (list[questionIndex].correct == answerList[0]){
|
act.rightCount++
|
Log.e(TAG,"选择正确")
|
transAnimation(cl_answer_1,voiceViews[questionIndex])
|
list[questionIndex].completed = true
|
}else{
|
dot_1.visible()
|
iv_error_1.visible()
|
dot_1.postDelayed({
|
dot_1.gone()
|
iv_error_1.gone()
|
},500)
|
}
|
}
|
rl_check_2.clickDelay {
|
if (list[questionIndex].completed) //已作答
|
return@clickDelay
|
act.totalCount++
|
if (list[questionIndex].correct == answerList[1]){
|
act.rightCount++
|
Log.e(TAG,"选择正确")
|
transAnimation(cl_answer_2,voiceViews[questionIndex])
|
list[questionIndex].completed = true
|
}else{
|
dot_2.visible()
|
iv_error_2.visible()
|
dot_2.postDelayed({
|
dot_2.gone()
|
iv_error_2.gone()
|
},500)
|
}
|
}
|
rl_check_3.clickDelay {
|
if (list[questionIndex].completed) //已作答
|
return@clickDelay
|
act.totalCount++
|
if (list[questionIndex].correct == answerList[2]){
|
act.rightCount++
|
Log.e(TAG,"选择正确")
|
transAnimation(cl_answer_3,voiceViews[questionIndex])
|
list[questionIndex].completed = true
|
}else{
|
dot_3.visible()
|
iv_error_3.visible()
|
dot_3.postDelayed({
|
dot_3.gone()
|
iv_error_3.gone()
|
},500)
|
}
|
}
|
}
|
|
|
|
fun recover(){
|
|
}
|
|
private fun showVoiceUi(){
|
list.forEachIndexed { index, subject ->
|
if (subject.isQuestion == 1){
|
v3Views[index].setImageResource(R.mipmap.voice_question)
|
voiceViews[index].visible()
|
}else{
|
v3Views[index].setImageResource(R.mipmap.voice_answer)
|
voiceViews[index].invisible()
|
}
|
voiceViews[index].clickDelay {
|
voiceIndex = index
|
handler?.sendEmptyMessage(PLAY_VOICE)
|
}
|
}
|
}
|
|
private fun showAnswerView(position:Int){
|
if (!layoutChanged){ //布局未改变,执行属性动画
|
val anim = ValueAnimator.ofInt(dip(120))
|
anim.duration = 500
|
anim.addUpdateListener {
|
val i = it.animatedValue as Int
|
val layoutParams = center.layoutParams as ConstraintLayout.LayoutParams
|
layoutParams.marginEnd = i
|
center.layoutParams = layoutParams
|
}
|
anim.start()
|
layoutChanged = true
|
}
|
questionIndex = position
|
rl_check_1.visible()
|
rl_check_2.visible()
|
rl_check_3.visible()
|
cl_answer_1.visible()
|
cl_answer_2.visible()
|
cl_answer_3.visible()
|
answerList.add(list[position].correct)
|
answerList.addAll(list[position].error?.split(",")?: arrayListOf())
|
answerList.shuffle()
|
cl_answer_1.setOnClickListener {
|
voiceIndex = 4
|
handler?.sendEmptyMessage(PLAY_VOICE)
|
}
|
cl_answer_2.setOnClickListener {
|
voiceIndex = 5
|
handler?.sendEmptyMessage(PLAY_VOICE)
|
}
|
cl_answer_3.setOnClickListener {
|
voiceIndex = 6
|
handler?.sendEmptyMessage(PLAY_VOICE)
|
}
|
}
|
|
|
private fun transAnimation(v:View,endView: View){
|
val offx = endView.left - v.left
|
val offy = endView.top - v.top
|
val x = PropertyValuesHolder.ofFloat("translationX",0f,offx.toFloat())
|
val y = PropertyValuesHolder.ofFloat("translationY",0f,offy.toFloat())
|
val animator = ObjectAnimator.ofPropertyValuesHolder(v, x, y)
|
animator.duration = 1000
|
animator.interpolator = LinearInterpolator()
|
animator.start()
|
animator.addListener(object :Animator.AnimatorListener{
|
override fun onAnimationStart(animation: Animator?) {
|
|
}
|
|
override fun onAnimationEnd(animation: Animator?) {
|
endView.visible()
|
v.translationX = 0f
|
v.translationY = 0f
|
rl_check_1.gone()
|
rl_check_2.gone()
|
rl_check_3.gone()
|
cl_answer_1.gone()
|
cl_answer_2.gone()
|
cl_answer_3.gone()
|
}
|
|
override fun onAnimationCancel(animation: Animator?) {
|
}
|
|
override fun onAnimationRepeat(animation: Animator?) {
|
}
|
})
|
}
|
|
|
companion object{
|
/**
|
* @param group 题组号
|
* @param index 组内序号
|
*/
|
fun getInstance(group: Int):QAFragment{
|
val listenFragment = QAFragment()
|
listenFragment.arguments = bundleOf("group" to group)
|
return listenFragment
|
}
|
}
|
|
override fun onUpdate(db: Double, time: Long) {
|
|
}
|
|
override fun onStop(filePath: String?) {
|
}
|
|
override fun onStartPlay() {
|
playing = true
|
v1Views[voiceIndex].gone()
|
v2Views[voiceIndex].gone()
|
playingViews[voiceIndex].visible()
|
if (voiceIndex == 0&&!list[1].completed){
|
showAnswerView(1)
|
}
|
if (voiceIndex == 1&&!list[0].completed){
|
showAnswerView(0)
|
|
}
|
if (voiceIndex == 2&&!list[3].completed){
|
showAnswerView(3)
|
}
|
if (voiceIndex == 3&&!list[2].completed){
|
showAnswerView(2)
|
}
|
}
|
|
override fun onFinishPlay() {
|
playing = false
|
v1Views[voiceIndex].visible()
|
v2Views[voiceIndex].visible()
|
playingViews[voiceIndex].gone()
|
}
|
}
|