1、回调写法
interface TestListener {
fun click1()
fun click2()
}
private var testListener: TestListener? = null
fun setForTestListener(listener: TestListener) {
testListener = listener
}
tvSaveMoon.noFastClick {
//在需要回调的地方,写上对应的方法
testListener?.click1()
}
2、属性动画(最近有需求需要完成一个类似椭圆的动画),研究了一下,做一下记录
private val animatorSet by lazy {
AnimatorSet()
}
private val firstPoint = IntArray(2) //动画开始点
private val endPoint = IntArray(2) //动画结束点
private val parentLocation = IntArray(2) //弧线图点
private val translationX by lazy {
ObjectAnimator.ofFloat(binding.ivAnimationMoon, "translationX", 0f, endPoint[0].toFloat() - firstPoint[0])
}
binding.ivTest.post {
//需要在视图加载成功后才能获取到坐标点
binding.ivAnimation1.getLocationInWindow(firstPoint)
binding.ivAnimationMoon2.getLocationInWindow(endPoint)
binding.ivAnimationCircle.getLocationInWindow(parentLocation)
translationX.interpolator = LinearInterpolator()
val translationY =
ObjectAnimator.ofFloat(binding.ivAnimationMoon,
"translationY",0f,
parentLocation[1].toFloat() - firstPoint[1] - (binding.ivAnimationMoon.height / 2), 0f)
translationY.interpolator = TimeInterpolator { input ->
(0.5f * sin(Math.PI * input)).toFloat()
}
animatorSet.playTogether(translationX, translationY)
animatorSet.duration = 1000
}
if (animatorSet.isRunning) {
animatorSet.cancel()
}
val target = (endPoint[0].toFloat() - firstPoint[0]) * percent
translationX.removeAllUpdateListeners()
translationX.addUpdateListener {
val value = it.animatedValue as Float
if (value >= target) {
animatorSet.cancel()
}
}
animatorSet.start()