//从小到大
private fun zoomImageFromThumb() {
mCurrentAnimator?.cancel()
imageView.setImageResource(R.drawable.pic_11)
//获取尺寸
val startBoundsInt = Rect()
val finalBoundsInt = Rect()
val globalOffset = Point()
imageBtn.getGlobalVisibleRect(startBoundsInt)
imageView.getGlobalVisibleRect(finalBoundsInt, globalOffset)
//调整使top=left=0
startBoundsInt.offset(-globalOffset.x, -globalOffset.y)
finalBoundsInt.offset(-globalOffset.x, -globalOffset.y)
val startBounds = RectF(startBoundsInt)
val finalBounds = RectF(finalBoundsInt)
//计算宽高缩放比
val startScale: Float
if ((finalBounds.width() / finalBounds.height() > startBounds.width() / startBounds.height())) {
startScale = startBounds.height() / finalBounds.height()
val startWidth: Float = startScale * finalBounds.width()
val deltaWidth: Float = (startWidth - startBounds.width()) / 2
startBounds.left -= deltaWidth.toInt()
startBounds.right += deltaWidth.toInt()
} else {
// Extend start bounds vertically
startScale = startBounds.width() / finalBounds.width()
val startHeight: Float = startScale * finalBounds.height()
val deltaHeight: Float = (startHeight - startBounds.height()) / 2f
startBounds.top -= deltaHeight.toInt()
startBounds.bottom += deltaHeight.toInt()
}
imageBtn.visibility = View.INVISIBLE
imageView.visibility = View.VISIBLE
imageView.pivotX = 0f
imageView.pivotY = 0f
mCurrentAnimator = AnimatorSet().apply {
//x、y、scaleX、scaleY四个维度一起动画
play(ObjectAnimator.ofFloat(
imageView,
View.X,
startBounds.left,
finalBounds.left)
).apply {
with(ObjectAnimator.ofFloat(imageView, View.Y, startBounds.top, finalBounds.top))
with(ObjectAnimator.ofFloat(imageView, View.SCALE_X, startScale, 1f))
with(ObjectAnimator.ofFloat(imageView, View.SCALE_Y, startScale, 1f))
}
duration = mShortAnimationDuration.toLong()
interpolator = DecelerateInterpolator()
addListener(object : AnimatorListenerAdapter() {
override fun onAnimationEnd(animation: Animator) {
mCurrentAnimator = null
}
override fun onAnimationCancel(animation: Animator) {
mCurrentAnimator = null
}
})
start()
}
}