fun View.zoomDo(scale: Float = 0.95f, duration: Long = 150, isEnable: Boolean = true) {//按钮点击缩放效果
var starttt = System.currentTimeMillis()
var chatt = System.currentTimeMillis()
this.setOnTouchListener { _, event ->
if (isEnable) {
when (event.action) {
MotionEvent.ACTION_DOWN -> {
starttt = System.currentTimeMillis()
this.animate().scaleX(scale).scaleY(scale).setDuration(duration).start()
}
MotionEvent.ACTION_UP, MotionEvent.ACTION_CANCEL -> {
chatt = System.currentTimeMillis() - starttt
if (chatt > 140) {
this.animate().scaleX(1f).scaleY(1f).setDuration(duration).start()
}else{
afterUIDo((duration-chatt).toInt()) {
this.animate().scaleX(1f).scaleY(1f).setDuration(duration).start()
}
}
}
}
}
// 点击事件处理,交给View自身
this.onTouchEvent(event)
}
}
v1.zoomDo()
方案解析
判断手指点击接触时间,显示完整缩放效果动画
因为是Kotlin拓展,添加isEnable控制某些情况不出现点击动画
网友评论