Browse Source

1.点击控件添加动画效果

王鹏鹏 2 years ago
parent
commit
525a997622

+ 94 - 55
mmse/src/main/java/com/yingyangfly/mmse/fragment/JudgmentFragment.kt

@@ -1,27 +1,31 @@
 package com.yingyangfly.mmse.fragment
 
+import android.annotation.SuppressLint
 import android.os.Bundle
 import android.text.TextUtils
+import android.view.MotionEvent
+import android.view.View
 import androidx.core.os.bundleOf
 import androidx.navigation.Navigation
 import com.yingyang.mmse.R
 import com.yingyang.mmse.databinding.FragmentJudgmentBinding
 import com.yingyangfly.baselib.base.BaseFragment
 import com.yingyangfly.baselib.db.QuestionsBean
-import com.yingyangfly.baselib.ext.setOnSingleClickListener
+import com.yingyangfly.baselib.ext.getEndAnimation
+import com.yingyangfly.baselib.ext.getScaleAnimation
 import com.yingyangfly.baselib.ext.toast
 
 /**
  * 录入单个判断
  */
-class JudgmentFragment : BaseFragment<FragmentJudgmentBinding>() {
+class JudgmentFragment : BaseFragment<FragmentJudgmentBinding>(), View.OnTouchListener {
 
     /**
      * 原始问题id(6.7.8.9.10) (29)
      */
     var questionId = 0
 
-    var question: QuestionsBean? = null
+    private var question: QuestionsBean? = null
 
     override fun onCreate(savedInstanceState: Bundle?) {
         questionId = arguments?.getInt("questionId") ?: 6
@@ -32,60 +36,12 @@ class JudgmentFragment : BaseFragment<FragmentJudgmentBinding>() {
 
     }
 
+    @SuppressLint("ClickableViewAccessibility")
     override fun initListener() {
         binding {
-            btnPrevious.setOnSingleClickListener {
-                if (questionId == 29) {
-                    val bundle = bundleOf("questionId" to 26)
-                    val controller = Navigation.findNavController(it)
-                    controller.navigate(
-                        R.id.action_judgmentFragment_to_multipleChoiceFragment,
-                        bundle
-                    )
-                } else {
-                    questionId--
-                    if (questionId == 5) {
-                        val bundle = bundleOf("questionId" to questionId)
-                        val controller = Navigation.findNavController(it)
-                        controller.navigate(
-                            R.id.action_judgmentFragment_to_selectedItemFragment,
-                            bundle
-                        )
-                    } else {
-                        judgeRadio.clearCheck()
-                        loadData()
-                    }
-                }
-            }
-            btnNext.setOnSingleClickListener {
-                if (questionId == 29) {
-                    val bundle = bundleOf("questionId" to 30)
-                    val controller = Navigation.findNavController(it)
-                    controller.navigate(R.id.action_judgmentFragment_to_drawDesignsFragment, bundle)
-                } else {
-                    if (question != null) {
-                        if (question!!.inputString.isNullOrEmpty()) {
-                            val str = "请判断受试者的回答是否正确"
-                            str.toast()
-                        } else {
-                            questionId++
-                            if (questionId < 11) {
-                                judgeRadio.clearCheck()
-                                loadData()
-                            } else {
-                                val bundle = bundleOf("questionId" to questionId)
-                                val controller = Navigation.findNavController(it)
-                                controller.navigate(
-                                    R.id.action_judgmentFragment_to_soundRecordFragment,
-                                    bundle
-                                )
-                            }
-                        }
-                    }
-                }
-            }
-
-            judgeRadio.setOnCheckedChangeListener { group, checkedId ->
+            btnPrevious.setOnTouchListener(this@JudgmentFragment)
+            btnNext.setOnTouchListener(this@JudgmentFragment)
+            judgeRadio.setOnCheckedChangeListener { _, checkedId ->
                 question!!.reviewId = question!!.id
                 question!!.reviewAnswer = ""
                 if (checkedId == R.id.btnDeny) {
@@ -129,4 +85,87 @@ class JudgmentFragment : BaseFragment<FragmentJudgmentBinding>() {
             }
         }
     }
+
+    @SuppressLint("ClickableViewAccessibility")
+    override fun onTouch(v: View, event: MotionEvent): Boolean {
+        when (event.action) {
+            MotionEvent.ACTION_DOWN -> {
+                if (v.id == R.id.btnPrevious || v.id == R.id.btnNext) {
+                    v.startAnimation(getScaleAnimation())
+                }
+            }
+            MotionEvent.ACTION_UP -> {
+                v.startAnimation(getEndAnimation())
+                if (v.id == R.id.btnPrevious) {
+                    pageUp(v)
+                } else if (v.id == R.id.btnNext) {
+                    pageNext(v)
+                }
+            }
+            MotionEvent.ACTION_CANCEL -> {
+                if (v.id == R.id.btnPrevious || v.id == R.id.btnNext) {
+                    v.startAnimation(getEndAnimation())
+                }
+            }
+        }
+        return true
+    }
+
+    /**
+     * 上一页
+     */
+    private fun pageUp(v: View) {
+        if (questionId == 29) {
+            val bundle = bundleOf("questionId" to 26)
+            val controller = Navigation.findNavController(v)
+            controller.navigate(
+                R.id.action_judgmentFragment_to_multipleChoiceFragment,
+                bundle
+            )
+        } else {
+            questionId--
+            if (questionId == 5) {
+                val bundle = bundleOf("questionId" to questionId)
+                val controller = Navigation.findNavController(v)
+                controller.navigate(
+                    R.id.action_judgmentFragment_to_selectedItemFragment,
+                    bundle
+                )
+            } else {
+                binding.judgeRadio.clearCheck()
+                loadData()
+            }
+        }
+    }
+
+    /**
+     * 下一页
+     */
+    private fun pageNext(v: View) {
+        if (questionId == 29) {
+            val bundle = bundleOf("questionId" to 30)
+            val controller = Navigation.findNavController(v)
+            controller.navigate(R.id.action_judgmentFragment_to_drawDesignsFragment, bundle)
+        } else {
+            if (question != null) {
+                if (question!!.inputString.isNullOrEmpty()) {
+                    val str = "请判断受试者的回答是否正确"
+                    str.toast()
+                } else {
+                    questionId++
+                    if (questionId < 11) {
+                        binding.judgeRadio.clearCheck()
+                        loadData()
+                    } else {
+                        val bundle = bundleOf("questionId" to questionId)
+                        val controller = Navigation.findNavController(v)
+                        controller.navigate(
+                            R.id.action_judgmentFragment_to_soundRecordFragment,
+                            bundle
+                        )
+                    }
+                }
+            }
+        }
+    }
 }

+ 70 - 29
mmse/src/main/java/com/yingyangfly/mmse/fragment/SoundRecordFragment.kt

@@ -1,27 +1,30 @@
 package com.yingyangfly.mmse.fragment
 
+import android.annotation.SuppressLint
 import android.os.Bundle
+import android.view.MotionEvent
+import android.view.View
 import androidx.core.os.bundleOf
 import androidx.navigation.Navigation
 import com.yingyang.mmse.R
 import com.yingyang.mmse.databinding.FragmentSoundRecordBinding
 import com.yingyangfly.baselib.base.BaseFragment
-import com.yingyangfly.baselib.ext.setOnSingleClickListener
+import com.yingyangfly.baselib.ext.getEndAnimation
+import com.yingyangfly.baselib.ext.getScaleAnimation
 import com.yingyangfly.baselib.voice.PlayVoice
 
 /**
  * 听录音判断
  */
-class SoundRecordFragment : BaseFragment<FragmentSoundRecordBinding>() {
+class SoundRecordFragment : BaseFragment<FragmentSoundRecordBinding>(), View.OnTouchListener {
 
     /**
      * 原始问题id
      */
-    var questionId = 0
-
-    var firstWord = ""
-    var secondWord = ""
-    var thirdWord = ""
+    private var questionId = 0
+    private var firstWord = ""
+    private var secondWord = ""
+    private var thirdWord = ""
 
     private var playVoice: PlayVoice? = null
 
@@ -35,30 +38,12 @@ class SoundRecordFragment : BaseFragment<FragmentSoundRecordBinding>() {
         playVoice?.setContext(mContext)
     }
 
+    @SuppressLint("ClickableViewAccessibility")
     override fun initListener() {
         binding {
-            btnPrevious.setOnSingleClickListener {
-                val bundle = bundleOf("questionId" to 10)
-                val controller = Navigation.findNavController(it)
-                controller.navigate(
-                    R.id.action_soundRecordFragment_to_judgmentFragment,
-                    bundle
-                )
-
-            }
-            btnNext.setOnSingleClickListener {
-                val bundle = Bundle()
-                bundle.putInt("questionId", questionId)
-                val controller = Navigation.findNavController(it)
-                controller.navigate(
-                    R.id.action_soundRecordFragment_to_multipleChoiceFragment,
-                    bundle
-                )
-            }
-
-            soundImage.setOnSingleClickListener {
-                playVoice?.speakWithParagraph(firstWord, secondWord, thirdWord)
-            }
+            btnPrevious.setOnTouchListener(this@SoundRecordFragment)
+            btnNext.setOnTouchListener(this@SoundRecordFragment)
+            soundImage.setOnTouchListener(this@SoundRecordFragment)
         }
     }
 
@@ -99,4 +84,60 @@ class SoundRecordFragment : BaseFragment<FragmentSoundRecordBinding>() {
             playVoice?.stop()
         }
     }
+
+    @SuppressLint("ClickableViewAccessibility")
+    override fun onTouch(v: View, event: MotionEvent): Boolean {
+        when (event.action) {
+            MotionEvent.ACTION_DOWN -> {
+                if (v.id == R.id.btnPrevious || v.id == R.id.btnNext || v.id == R.id.soundImage) {
+                    v.startAnimation(getScaleAnimation())
+                }
+            }
+            MotionEvent.ACTION_UP -> {
+                v.startAnimation(getEndAnimation())
+                when (v.id) {
+                    R.id.btnPrevious -> {
+                        pageUp(v)
+                    }
+                    R.id.btnNext -> {
+                        pageNext(v)
+                    }
+                    R.id.soundImage -> {
+                        playVoice?.speakWithParagraph(firstWord, secondWord, thirdWord)
+                    }
+                }
+            }
+            MotionEvent.ACTION_CANCEL -> {
+                if (v.id == R.id.btnPrevious || v.id == R.id.btnNext || v.id == R.id.soundImage) {
+                    v.startAnimation(getEndAnimation())
+                }
+            }
+        }
+        return true
+    }
+
+    /**
+     * 上一页
+     */
+    private fun pageUp(v: View) {
+        val bundle = bundleOf("questionId" to 10)
+        val controller = Navigation.findNavController(v)
+        controller.navigate(
+            R.id.action_soundRecordFragment_to_judgmentFragment,
+            bundle
+        )
+    }
+
+    /**
+     * 下一页
+     */
+    private fun pageNext(v: View) {
+        val bundle = Bundle()
+        bundle.putInt("questionId", questionId)
+        val controller = Navigation.findNavController(v)
+        controller.navigate(
+            R.id.action_soundRecordFragment_to_multipleChoiceFragment,
+            bundle
+        )
+    }
 }