代码是照着教程打的
funzoomImageFromThumb(thumbView: View, imageResId: Int) {
if(null!=mCurrentAnimator) {
mCurrentAnimator?.cancel()
}
BigzoomImage.setImageResource(imageResId)
varstartBounds = Rect()
varfinalBounds = Rect()
varglobalOffset = Point()
//获得缩略图位置信息
thumbView.getGlobalVisibleRect(startBounds)
//获得总布局的位置信息和画笔
container.getGlobalVisibleRect(finalBounds, globalOffset)
startBounds.offset(-globalOffset.x, -globalOffset.y)
finalBounds.offset(-globalOffset.x, -globalOffset.y)
valstartScale: Float
if(finalBounds.width() / finalBounds.height() > startBounds.width() / startBounds.height()) {
// Extend start bounds horizontally
startScale = startBounds.height().toFloat() / finalBounds.height()
valstartWidth = startScale * finalBounds.width()
valdeltaWidth = (startWidth.toInt() - startBounds.width()asFloat) /2
startBounds.left-= deltaWidth.toInt()
startBounds.right+= deltaWidth.toInt()
}else{
// Extend start bounds vertically
startScale = startBounds.width().toFloat() / finalBounds.width()
valstartHeight = startScale * finalBounds.height()
valdeltaHeight = (startHeight - startBounds.height().toFloat()) /2
startBounds.top-= deltaHeight.toInt()
startBounds.bottom+= deltaHeight.toInt()
}
// Hide the thumbnail and show the zoomed-in view. When the animation
// begins, it will position the zoomed-in view in the place of the
// thumbnail.
thumbView.alpha=0f
BigzoomImage.setVisibility(View.VISIBLE)
// Set the pivot point for SCALE_X and SCALE_Y transformations
// to the top-left corner of the zoomed-in view (the default
// is the center of the view).
BigzoomImage.setPivotX(0f)
BigzoomImage.setPivotY(0f)
// Construct and run the parallel animation of the four translation and
// scale properties (X, Y, SCALE_X, and SCALE_Y).
valset = AnimatorSet()
set.play(ObjectAnimator.ofFloat(BigzoomImage, View.X,
startBounds.left.toFloat(), finalBounds.left.toFloat()))
.with(ObjectAnimator.ofFloat(BigzoomImage, View.Y,
startBounds.top.toFloat(), finalBounds.top.toFloat()))
.with(ObjectAnimator.ofFloat(BigzoomImage, View.SCALE_X,
startScale,1f)).with(ObjectAnimator.ofFloat(BigzoomImage,
View.SCALE_Y, startScale,1f))
set.setDuration(mShortAnimationDuration.toLong())
set.interpolator= DecelerateInterpolator()
set.addListener(object: AnimatorListenerAdapter() {
override funonAnimationEnd(animation: Animator) {
mCurrentAnimator=null
}
override funonAnimationCancel(animation: Animator) {
mCurrentAnimator=null
}
})
set.start()
mCurrentAnimator= set
// Upon clicking the zoomed-in image, it should zoom back down
// to the original bounds and show the thumbnail instead of
// the expanded image.
BigzoomImage.setOnClickListener(View.OnClickListener{
if(mCurrentAnimator!=null) {
(mCurrentAnimatorasAnimatorSet).cancel()
}
// Animate the four positioning/sizing properties in parallel,
// back to their original values.
valset = AnimatorSet()
set.play(ObjectAnimator
.ofFloat(BigzoomImage, View.X, startBounds.left.toFloat()))
.with(ObjectAnimator
.ofFloat(BigzoomImage,
View.Y, startBounds.top.toFloat()))
.with(ObjectAnimator
.ofFloat(BigzoomImage,
View.SCALE_X, startScale))
.with(ObjectAnimator
.ofFloat(BigzoomImage,
View.SCALE_Y, startScale))
set.setDuration(mShortAnimationDuration.toLong())
set.interpolator= DecelerateInterpolator()
set.addListener(object: AnimatorListenerAdapter() {
override funonAnimationEnd(animation: Animator) {
thumbView.alpha=1f
BigzoomImage.setVisibility(View.GONE)
mCurrentAnimator=null
}
override funonAnimationCancel(animation: Animator) {
thumbView.alpha=1f
BigzoomImage.setVisibility(View.GONE)
mCurrentAnimator=null
}
})
set.start()
mCurrentAnimator= set
})
}
网友评论