Просмотр изображения на Kotlin - PullRequest
0 голосов
/ 26 апреля 2018

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

my error log, и это мой код

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

1 Ответ

0 голосов
/ 26 апреля 2018

Конвертируйте связанные значения Rect из int в float.Вы передаете startBounds top, left и т. Д. В вашем методе ObjectAnimator.ofFloat, который является int значениями.Но метод ofFloat принимает только float значений.

Просто используйте toFloat () для преобразования int в float:

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