uCrop icon indicating copy to clipboard operation
uCrop copied to clipboard

Please add a feature that can keep ratio in free mode. (with kotlin code)

Open ghost opened this issue 6 years ago • 5 comments

I looked the source, but there is no feature can keep ratio with setFreeStyleCropEnabled enabled.

So I wrote some code to do this, but it only works in Debug mode, I can't extend or override, no callbacks or protect method can let me hook it.

My Kotlin code:


    /**
     * @param corner mCurrentTouchCornerIndex
     * @param ratio mTargetAspectRatio, w/h
     * @param x original touchX
     * @param y original touchY
     * @param rect mCropViewRect
     *
     * @return newTouchX, newTouchY for OverlayView#updateCropViewRect
     */
    fun newPositionForUpdateCropViewRect(
            corner: Int,
            ratio: Float,
            x: Float,
            y: Float,
            rect: RectF
    ): Pair<Float, Float> {
        if (corner !in 0..3) return x to y
    
        var k = 1f / ratio
    
        if (corner == 1 || corner == 3) k = -k
    
        val w = if (corner == 0 || corner == 3) rect.left else rect.right
        val h = if (corner == 0 || corner == 1) rect.top else rect.bottom
    
        val kk1 = k * k + 1
        val tx = ((w * k + y - h) * k + x) / kk1
        val ty = ((y * k + x - w) * k + h) / kk1
    
        return tx to ty
    }

untitled

ghost avatar Apr 09 '18 12:04 ghost