TestHomeRealTestView.kt 6.12 KB
Newer Older
konghaorui committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
package com.yidianling.tests.home.widget

import android.content.Context
import android.os.Handler
import android.os.Looper
import android.os.Message
import android.view.View
import android.view.ViewGroup
import android.view.animation.Animation
import android.view.animation.DecelerateInterpolator
import android.view.animation.TranslateAnimation
import android.widget.FrameLayout
import com.yidianling.tests.R
import com.yidianling.tests.home.bean.TestHomeBodyBean
import com.yidianling.tests.home.event.ITestHomeEvent
16 17
import kotlinx.android.synthetic.main.tests_testhome_realtest_view_in.view.*
import kotlinx.android.synthetic.main.tests_testhome_realtest_view_out.view.*
konghaorui committed
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72

/**
 * @author yuanwai
 * @描述:测评首页--实时测试状态View
 * @Copyright Copyright (c) 2018
 * @Company 壹点灵
 * @date 2018/7/28
 */
class TestHomeRealTestView(mContext : Context,testHomeEvent : ITestHomeEvent) : FrameLayout(mContext){

    private val STATUS_IN = 0
    private val STATUS_OUT = 1
    private var curTipIndex = 0
    private var lastTimeMillis: Long = 0
    private val ANIM_DELAYED_MILLIONS = 2 * 1000
    /**
     * 动画持续时长
     */
    private val ANIM_DURATION = 500
    /**
     * 进、出 两个view (主要用于做动画,其实是两个相同的布局文件)
     */
    private var view_out: View? = null
    private var view_in:View? = null
    /**
     * 进、出 两个View 的动画
     */
    private var anim_out: Animation? = null
    private var anim_in:Animation? = null
    /**
     * 数据缓存
     */
    private var mDataList : List<TestHomeBodyBean>? = null

    private var testHomeEvent : ITestHomeEvent? = null

    private val mHandler: Handler = object : Handler(Looper.getMainLooper()) {
        override fun handleMessage(msg: Message?) {
            super.handleMessage(msg)
            updateTipAndPlayAnimation()
            sendMessageDelayed(Message(), ANIM_DELAYED_MILLIONS.toLong())
        }
    }

    init {
        this.testHomeEvent = testHomeEvent
        initView()
        initAnimation()
    }

    /**
     * 界面初始化
     */
    private fun initView() {
        var params = ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT)
73 74
        view_out = View.inflate(context, R.layout.tests_testhome_realtest_view_out,null)
        view_in = View.inflate(context, R.layout.tests_testhome_realtest_view_in,null)
konghaorui committed
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
        addView(view_out)
        addView(view_in)
        layoutParams = params
    }

    private fun initAnimation() {
        anim_out = newAnimation(0f, -1f)
        anim_in = newAnimation(1f, 0f)
        anim_in!!.setAnimationListener(object : Animation.AnimationListener {

            override fun onAnimationStart(animation: Animation) {

            }

            override fun onAnimationRepeat(animation: Animation) {

            }

            override fun onAnimationEnd(animation: Animation) {
                updateViewVisibility()
            }
        })
    }

    /**
     * 设置数据
     */
    fun initData(list: List<TestHomeBodyBean>){
        mHandler.removeCallbacksAndMessages(null)
        if (null == list || list.isEmpty()){
            visibility = View.GONE
            return
        }
        visibility = View.VISIBLE
        if (null == mDataList){
            mDataList = ArrayList()
        }else{
            (mDataList as ArrayList).clear()
        }
        (mDataList as ArrayList).addAll(list)
        curTipIndex = 0
        updateTip(STATUS_OUT)
        updateTipAndPlayAnimation()
        mHandler.sendMessageDelayed(Message(), ANIM_DELAYED_MILLIONS.toLong())
    }

    private fun updateViewVisibility() {
        if (curTipIndex % 2 == 0) {
            view_out!!.visibility = View.INVISIBLE
        } else {
            view_in!!.visibility = View.INVISIBLE
        }
    }

    private fun newAnimation(fromYValue: Float, toYValue: Float): Animation {
        val anim = TranslateAnimation(Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 0f,
                Animation.RELATIVE_TO_SELF, fromYValue, Animation.RELATIVE_TO_SELF, toYValue)
        anim.duration = ANIM_DURATION.toLong()
        anim.interpolator = DecelerateInterpolator()
        return anim
    }

    private fun updateTipAndPlayAnimation() {
        view_in!!.visibility = View.VISIBLE
        view_out!!.visibility = View.VISIBLE
        if (curTipIndex % 2 == 0) {
            updateTip(STATUS_OUT)
            view_in!!.startAnimation(anim_out)
            view_out!!.startAnimation(anim_in)
            this.bringChildToFront(view_in)
        } else {
            updateTip(STATUS_IN)
            view_out!!.startAnimation(anim_out)
            view_in!!.startAnimation(anim_in)
            this.bringChildToFront(view_out)
        }
    }

    private fun updateTip(status : Int){
        val bodyBean = getNextTip() ?: return

        when(status){
            STATUS_IN -> {

                tv_title_in.text = bodyBean.realTestTitle
                tv_name_in.text = getName(bodyBean.realTestName)
                view_in!!.setOnClickListener{
                    testHomeEvent!!.realTestClick(bodyBean.realTestLinkUrl,bodyBean.realTestTitle)
                }
            }
            STATUS_OUT -> {
                tv_title_out.text = bodyBean.realTestTitle
                tv_name_out.text = getName(bodyBean.realTestName)
                view_out!!.setOnClickListener{
                    testHomeEvent!!.realTestClick(bodyBean.realTestLinkUrl,bodyBean.realTestTitle)
                }
            }
        }
    }

    private fun getName(name : String?) : String{
        var nameBuffer = StringBuffer()
177
        nameBuffer.append(resources.getString(R.string.tests_testhome_just))
konghaorui committed
178 179 180
        nameBuffer.append(" ")
        nameBuffer.append(name)
        nameBuffer.append(" ")
181
        nameBuffer.append(resources.getString(R.string.tests_testhome_test))
konghaorui committed
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196

        return nameBuffer.toString()
    }

    private fun getNextTip() : TestHomeBodyBean? {
        if (null == mDataList || mDataList!!.isEmpty()){
            return null
        }
        return mDataList!!.get(curTipIndex++ % mDataList!!.size)
    }

    fun onDestory(){
        mHandler.removeCallbacksAndMessages(null)
    }
}