Kotlin 上的图像视图 onclick

Image View onclick on Kotlin

我创建了一个函数,可以在单击时使图像视图缩放,但是在将代码从 java activity 复制到 kotlin 语言时遇到了一些麻烦。 ofFloat 代码显示红色下划线,我不知道如何修复它。我已经尝试修复它但仍然不适合我。我是科特林新手

这是我的代码

@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
    }
}

Rect 的绑定值从 int 转换为 float。您在 ObjectAnimator.ofFloat 方法中传递 startBounds 顶部、左侧等,它们是 int 值。但是 ofFloat 方法只需要 float 个值。

只需使用 toFloat() 将 int 转换为 float:

ObjectAnimator.ofFloat<View>(expandedImageView, View.X,
                startBounds.left, finalBounds.left.toFloat())