0

Trying to resize my image view that is not in main activity however main activity button brings float image on top of all apps. I can move image but don’t know how to zoom in or out. Any help would be much appreciated :) thank you

Floating window Java that is movable but not resizable

class SimpleFloatingWindow constructor(private val context: Context) {

private var windowManager: WindowManager? = null
    get() {
        if (field == null) field = (context.getSystemService(WINDOW_SERVICE) as WindowManager)
        return field
    }

private var floatView: View =
    LayoutInflater.from(context).inflate(R.layout.layout_floating_window, null)

private lateinit var layoutParams: WindowManager.LayoutParams

private var lastX: Int = 0
private var lastY: Int = 0
private var firstX: Int = 0
private var firstY: Int = 0

private var isShowing = false
private var touchConsumedByMove = false

private val onTouchListener = View.OnTouchListener { view, event ->
    val totalDeltaX = lastX - firstX
    val totalDeltaY = lastY - firstY

    when (event.actionMasked) {
        MotionEvent.ACTION_DOWN -> {
            lastX = event.rawX.toInt()
            lastY = event.rawY.toInt()
            firstX = lastX
            firstY = lastY
        }
        MotionEvent.ACTION_UP -> {
            view.performClick()
        }
        MotionEvent.ACTION_MOVE -> {
            val deltaX = event.rawX.toInt() - lastX
            val deltaY = event.rawY.toInt() - lastY
            lastX = event.rawX.toInt()
            lastY = event.rawY.toInt()
            if (abs(totalDeltaX) >= 5 || abs(totalDeltaY) >= 5) {
                if (event.pointerCount == 1) {
                    layoutParams.x += deltaX
                    layoutParams.y += deltaY
                    touchConsumedByMove = true
                    windowManager?.apply {
                        updateViewLayout(floatView, layoutParams)
                    }
                } else {
                    touchConsumedByMove = false
                }
            } else {
                touchConsumedByMove = false
            }
        }
        else -> {
        }
    }
    touchConsumedByMove
}

init {
    with(floatView) {


    }

    floatView.setOnTouchListener(onTouchListener)

    layoutParams = WindowManager.LayoutParams().apply {
        format = PixelFormat.TRANSLUCENT
        flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
        @Suppress("DEPRECATION")
        type = when {
            Build.VERSION.SDK_INT >= Build.VERSION_CODES.O ->
                WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY
            else -> WindowManager.LayoutParams.TYPE_TOAST
        }

        gravity = Gravity.CENTER
        width = WindowManager.LayoutParams.WRAP_CONTENT
        height = WindowManager.LayoutParams.WRAP_CONTENT
    }
}

fun show() {
    if (context.canDrawOverlays) {
        dismiss()
        isShowing = true
        windowManager?.addView(floatView, layoutParams)
    }
}

fun dismiss() {
    if (isShowing) {
        windowManager?.removeView(floatView)
        isShowing = false
    }
}
Jacob Nero
  • 1
  • 1
  • 2

1 Answers1

0

I think the solution given in this question will solve your problem:

Android ImageView Zoom-in and Zoom-Out

You can also try going through this tutorial if you don't want to just add some guy's code: https://developer.android.com/training/animation/zoom.html

  • • Thank you momrider.My the biggest issue is that im not in main activity so implementing @extands blah.. blah.. blah.. wont work. Also when i add findViewById cant be implemented (well just wont work) main activity has got btn that brings floating window up. Movability is there just don’t know how to add re-sizebility to it. – Jacob Nero Nov 08 '20 at 01:33