简体   繁体   English

Android Studio (Kotlin) 放大缩小 imageView

[英]Android Studio (Kotlin) zoom in zoom out imageView

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可移动但不可调整大小的浮动窗口Java

class SimpleFloatingWindow constructor(private val context: Context) {类 SimpleFloatingWindow 构造函数(私有 val 上下文:上下文){

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

I think the solution given in this question will solve your problem:我认为这个问题中给出的解决方案将解决您的问题:

Android ImageView Zoom-in and Zoom-Out Android ImageView 放大和缩小

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如果您不想只添加一些人的代码,您也可以尝试阅读本教程: https : //developer.android.com/training/animation/zoom.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM