iT邦幫忙

第 12 屆 iT 邦幫忙鐵人賽

DAY 18
0
Mobile Development

菜雞 Android APP 從安裝 Android Studio 到上架 Google Play系列 第 18

Day18 - 利用 ObjectAnimator 跑動畫

class FragmentTwo : Fragment() {

    val layoutList by lazy { listOf(chapter_one, chapter_two, chapter_three, chapter_four) }

    val state = arrayListOf(false, false, false, false)

    val btnList by lazy { listOf(btn_one, btn_two, btn_three, btn_four) }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {

        return inflater.inflate(R.layout.fragment_two, container, false)
    }


    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {

        initEvent()

        super.onViewCreated(view, savedInstanceState)
    }


    fun initEvent() {
        btn_one.setOnClickListener(listener)
        btn_two.setOnClickListener(listener)
        btn_three.setOnClickListener(listener)
        btn_four.setOnClickListener(listener)
    }

    fun stateChange(position: Int) {

        if (state[position]) {
            layoutList[position].apply {
                closeAnimation()
                Handler().postDelayed({
                    visibility = View.GONE
                },500)
            }
            state[position] = false
        } else {
            layoutList[position].apply {
                visibility = View.VISIBLE
                openAnimation()
            }
            state[position] = true
        }
    }

    val listener = View.OnClickListener {
        when (it) {
            btn_one -> stateChange(0)
            btn_two -> stateChange(1)
            btn_three -> stateChange(2)
            btn_four -> stateChange(3)
        }
    }

    fun LinearLayout.openAnimation() {
        this.pivotY = 0f
        ObjectAnimator.ofFloat(this, "ScaleY", 0f, 1f).setDuration(500).start()
    }

    fun LinearLayout.closeAnimation() {
        ObjectAnimator.ofFloat(this, "ScaleY", 1f, 0f).setDuration(500).start()
    }

    fun canNotClick() {
        btnList.forEach {
            it.isClickable = false
        }
    }

    fun canClick() {
        btnList.forEach {
            it.isClickable = true
        }
    }

}


上一篇
Day17 - Cloud FireStore (3/3) 取得排序後資料// 排行榜
下一篇
Day19 - Spinner 選擇音樂藉由 Service 背景播放
系列文
菜雞 Android APP 從安裝 Android Studio 到上架 Google Play30

尚未有邦友留言

立即登入留言