Android View 按下缩小的效果

fun View.addClickScale(scale: Float = 0.9f, duration: Long = 150) {
this.setOnTouchListener { _, event ->
when (event.action) {
MotionEvent.ACTION_DOWN -> {
this.animate().scaleX(scale).scaleY(scale).setDuration(duration)
.start()
}
MotionEvent.ACTION_UP -> {
this.animate().scaleX(1f).scaleY(1f).setDuration(duration).start()
this.performClick()
}
MotionEvent.ACTION_CANCEL -> {
this.animate().scaleX(1f).scaleY(1f).setDuration(duration).start()
}
}
true
}
}

这里需要注意有的控件有 MOVE 的效果,有的是 MOVE 同取消一致。具体情况具体分析。