Kotlin
复制代码
import android.animation.Animator
import android.animation.AnimatorSet
import android.animation.ObjectAnimator
import android.content.Context
import android.util.AttributeSet
import android.util.Log
import android.view.MotionEvent
import android.view.ScaleGestureDetector
import androidx.recyclerview.widget.RecyclerView
class MyTouchView : androidx.appcompat.widget.AppCompatImageView {
private var mContext: Context? = null
private var mRV1: RecyclerView? = null
private var mRV2: RecyclerView? = null
private var mScaleGestureDetector: ScaleGestureDetector? = null
//缩放因子
private var mScaleFactor = 1.0f
private var mIsScaling = false
private val BIG = 0
private val SMALL = 1
private var mGridStatus = BIG
constructor(ctx: Context, attributeSet: AttributeSet) : super(ctx, attributeSet) {
mContext = ctx
mScaleGestureDetector = ScaleGestureDetector(mContext!!, object : ScaleGestureDetector.SimpleOnScaleGestureListener() {
override fun onScaleBegin(detector: ScaleGestureDetector): Boolean {
return super.onScaleBegin(detector)
}
override fun onScale(detector: ScaleGestureDetector): Boolean {
mScaleFactor = detector.scaleFactor
Log.d(MainActivity.TAG, "onScale scaleFactor=${detector.scaleFactor}")
return super.onScale(detector)
}
})
}
fun bindRV(rv1: RecyclerView, rv2: RecyclerView) {
mRV1 = rv1
mRV2 = rv2
}
override fun onTouchEvent(event: MotionEvent?): Boolean {
var bRet = false
val pointerId = event?.getPointerId(event.actionIndex)
if (pointerId!! > 0) {
this.postDelayed({
mScaleGestureDetector?.onTouchEvent(event!!)
}, 10)
if (mScaleFactor < 1f) {
if (!mIsScaling && mGridStatus != SMALL) {
mIsScaling = true
Log.d(MainActivity.TAG, "to small mScaleFactor=$mScaleFactor")
val animatorSet = AnimatorSet()
animatorSet.addListener(object : Animator.AnimatorListener {
override fun onAnimationStart(animation: Animator) {
}
override fun onAnimationEnd(animation: Animator) {
mIsScaling = false
mScaleFactor = 1f
mGridStatus = SMALL
}
override fun onAnimationCancel(animation: Animator) {
mIsScaling = false
mScaleFactor = 1f
mGridStatus = SMALL
}
override fun onAnimationRepeat(animation: Animator) {
}
})
val x1 = ObjectAnimator.ofFloat(mRV1, "scaleX", 1f, 0.01f).apply {
duration = 2000
}
val y1 = ObjectAnimator.ofFloat(mRV1, "scaleY", 1f, 0.01f).apply {
duration = 2000
}
val a1 = ObjectAnimator.ofFloat(mRV1, "alpha", 1f, 0f).apply {
duration = 2000
}
val x2 = ObjectAnimator.ofFloat(mRV2, "scaleX", 0.01f, 1f).apply {
duration = 2000
}
val y2 = ObjectAnimator.ofFloat(mRV2, "scaleY", 0.01f, 1f).apply {
duration = 2000
}
val a2 = ObjectAnimator.ofFloat(mRV2, "alpha", 0f, 1f).apply {
duration = 2000
}
animatorSet.playTogether(x1, y1, a1, x2, y2, a2)
animatorSet.start()
}
} else if (mScaleFactor > 1f && mGridStatus != BIG) {
if (!mIsScaling) {
Log.d(MainActivity.TAG, "to big mScaleFactor=$mScaleFactor")
mIsScaling = true
val animatorSet = AnimatorSet()
animatorSet.addListener(object : Animator.AnimatorListener {
override fun onAnimationStart(animation: Animator) {
}
override fun onAnimationEnd(animation: Animator) {
mIsScaling = false
mScaleFactor = 1f
mGridStatus = BIG
}
override fun onAnimationCancel(animation: Animator) {
mIsScaling = false
mScaleFactor = 1f
mGridStatus = BIG
}
override fun onAnimationRepeat(animation: Animator) {
}
})
val x1 = ObjectAnimator.ofFloat(mRV1, "scaleX", 0.01f, 1f).apply {
duration = 2000
}
val y1 = ObjectAnimator.ofFloat(mRV1, "scaleY", 0.01f, 1f).apply {
duration = 2000
}
val a1 = ObjectAnimator.ofFloat(mRV1, "alpha", 0f, 1f).apply {
duration = 2000
}
val x2 = ObjectAnimator.ofFloat(mRV2, "scaleX", 1f, 0.01f).apply {
duration = 2000
}
val y2 = ObjectAnimator.ofFloat(mRV2, "scaleY", 1f, 0.01f).apply {
duration = 2000
}
val a2 = ObjectAnimator.ofFloat(mRV2, "alpha", 1f, 0f).apply {
duration = 2000
}
animatorSet.playTogether(x1, y1, a1, x2, y2, a2)
animatorSet.start()
}
}
bRet = true
} else {
bRet = false
}
return bRet
}
}