I create a function to make the image view zoom able when clicked, but i have some trouble when copying the code from java activity to kotlin language. The ofFloat code is showing red underline and i don't know how to fix it. I already try to fix it but still not working for me. I'm a newbie in kotlin
@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
private fun zoomImageFromThumb(thumbView: View, imageResId: Int) {
if (mCurrentAnimator != null) {
mCurrentAnimator!!.cancel()
}
val expandedImageView = activity.findViewById<View>(
R.id.expanded_image) as ImageView
expandedImageView.setImageResource(imageResId)
val startBounds = Rect()
val finalBounds = Rect()
val globalOffset = Point()
thumbView.getGlobalVisibleRect(startBounds)
activity.findViewById<View>(R.id.container)
.getGlobalVisibleRect(finalBounds, globalOffset)
startBounds.offset(-globalOffset.x, -globalOffset.y)
finalBounds.offset(-globalOffset.x, -globalOffset.y)
val startScale: Float
if (finalBounds.width().toFloat() / finalBounds.height() > startBounds.width().toFloat() / startBounds.height()) {
// Extend start bounds horizontally
startScale = startBounds.height().toFloat() / finalBounds.height()
val startWidth = startScale * finalBounds.width()
val deltaWidth = (startWidth - startBounds.width()) / 2
startBounds.left -= deltaWidth.toInt()
startBounds.right += deltaWidth.toInt()
} else {
// Extend start bounds vertically
startScale = startBounds.width().toFloat() / finalBounds.width()
val startHeight = startScale * finalBounds.height()
val deltaHeight = (startHeight - startBounds.height()) / 2
startBounds.top -= deltaHeight.toInt()
startBounds.bottom += deltaHeight.toInt()
}
thumbView.alpha = 0f
expandedImageView.visibility = View.VISIBLE
expandedImageView.pivotX = 0f
expandedImageView.pivotY = 0f
// scale properties (X, Y, SCALE_X, and SCALE_Y).
val set = AnimatorSet()
set
.play(ObjectAnimator.ofFloat<View>(expandedImageView, View.X,
startBounds.left, finalBounds.left))
.with(ObjectAnimator.ofFloat<View>(expandedImageView, View.Y,
startBounds.top, finalBounds.top))
.with(ObjectAnimator.ofFloat(expandedImageView, View.SCALE_X,
startScale, 1f)).with(ObjectAnimator.ofFloat(expandedImageView,
View.SCALE_Y, startScale, 1f))
set.duration = mShortAnimationDuration.toLong()
set.interpolator = DecelerateInterpolator()
set.addListener(object : AnimatorListenerAdapter() {
override fun onAnimationEnd(animation: Animator) {
mCurrentAnimator = null
}
override fun onAnimationCancel(animation: Animator) {
mCurrentAnimator = null
}
})
set.start()
mCurrentAnimator = set
expandedImageView.setOnClickListener {
if (mCurrentAnimator != null) {
mCurrentAnimator!!.cancel()
}
// back to their original values.
val set = AnimatorSet()
set.play(ObjectAnimator
.ofFloat<View>(expandedImageView, View.X, startBounds.left))
.with(ObjectAnimator
.ofFloat<View>(expandedImageView,
View.Y, startBounds.top))
.with(ObjectAnimator
.ofFloat(expandedImageView,
View.SCALE_X, startScale))
.with(ObjectAnimator
.ofFloat(expandedImageView,
View.SCALE_Y, startScale))
set.duration = mShortAnimationDuration.toLong()
set.interpolator = DecelerateInterpolator()
set.addListener(object : AnimatorListenerAdapter() {
override fun onAnimationEnd(animation: Animator) {
thumbView.alpha = 1f
expandedImageView.visibility = View.GONE
mCurrentAnimator = null
}
override fun onAnimationCancel(animation: Animator) {
thumbView.alpha = 1f
expandedImageView.visibility = View.GONE
mCurrentAnimator = null
}
})
set.start()
mCurrentAnimator = set
}
}
Convert your Rect
's bound values from int to float. You are passing startBounds
top, left, etc. in your ObjectAnimator.ofFloat
method which are int
values. But ofFloat method only takes float
values.
Just use toFloat() to convert int to float:
ObjectAnimator.ofFloat<View>(expandedImageView, View.X,
startBounds.left, finalBounds.left.toFloat())