Я создаю функцию, позволяющую увеличить масштаб изображения при щелчке, но у меня возникли некоторые проблемы при копировании кода из java-деятельности на язык kotlin.Код ofFloat показывает красное подчеркивание, и я не знаю, как это исправить.Я уже пытаюсь это исправить, но все еще не работает для меня.Я новичок в 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
}
}