给定尺寸比时,在预览视图上绘制圆圈不准确

Posted

技术标签:

【中文标题】给定尺寸比时,在预览视图上绘制圆圈不准确【英文标题】:draw circle on preview view is not accurate when dimension ratio is given 【发布时间】:2020-11-14 13:26:15 【问题描述】:

我正在尝试通过setOnTouchListenerpreviewView 上画一个圆圈,如下所示

        val shape = GradientDrawable()
            shape.shape = GradientDrawable.OVAL
            shape.setColor(Color.TRANSPARENT)
            shape.setStroke(5, Color.WHITE)

        var circleSize = 200

        var img: ImageView = ImageView(this)
            img.background = shape
            val params = FrameLayout.LayoutParams(circleSize, circleSize)
            params.leftMargin = (event.x - (circleSize / 2)).toInt()
            params.topMargin = (event.y - (circleSize / 2)).toInt()
            idFocusIndicator.removeAllViews()
            idFocusIndicator.addView(img, params)

这是我的PreviewView

<androidx.camera.view.PreviewView
        android:id="@+id/viewFinder"
        android:layout_
        android:layout_
        app:layout_constraintBottom_toBottomOf="parent"

    --->app:layout_constraintDimensionRatio="" // 3:4, 9:16, 1:1 etc

        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/idFocusIndicator"
        android:layout_
        android:layout_
        android:background="@android:color/transparent"
        app:layout_constraintBottom_toTopOf="@+id/viewFinder"
        app:layout_constraintEnd_toEndOf="@+id/viewFinder"
        app:layout_constraintStart_toStartOf="@+id/viewFinder"
        app:layout_constraintTop_toTopOf="@+id/viewFinder" />

当我在constraintDimensionRatio 为空白(即"")时点击视图。无论我在哪里点击视图,都会以该点为中心出现一个圆形。

但是,当constraintDimensionRatio 被赋予“3:4”、“1:1”等值时,圆圈距离我点击视图的位置有点远。并且不同的尺寸比例与实际位置的距离不同。

当比率为"3:4" 时,看看我点击的位置和绘制圆圈的位置,当比率为"" 时不会出现问题

如何解决这个问题?

编辑:

我发现因为FrameLayout 是用match_parent 约束值硬编码的,所以它的行为是这样的。所以,如果我创建一个FrameLayout 并将ImageView 添加到它,然后将FrameLayout 作为子级添加到parent layout 将解决这个问题。但我不知道该怎么做!有什么帮助吗?

【问题讨论】:

【参考方案1】:

没有人回答我的问题所以,我只是回答

我只是通过更改与PreviewView 完全匹配的约束来解决它。但是,花了将近一天的时间才弄清楚这一点。它可能会帮助犯同样错误的人。

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/idFocusIndicator"
        android:layout_
        android:layout_
        android:background="@android:color/transparent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintDimensionRatio="3:4"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

【讨论】:

以上是关于给定尺寸比时,在预览视图上绘制圆圈不准确的主要内容,如果未能解决你的问题,请参考以下文章

具有渐变的可绘制视图背景在预览中正确显示但在模拟器中不正确

Xcode如何利用预览(Preview)让SwiftUI视图快速适配不同尺寸的设备

Android中的相机,如何获得最佳尺寸,预览尺寸,图片尺寸,视图尺寸,图像失真

Xcode如何利用预览(Preview)让SwiftUI视图快速适配不同尺寸的设备

Xcode 自动布局故事板预览不显示容器视图

UIImageView 在不同设备上保留情节提要“预览屏幕边界” - 框架不根据屏幕尺寸更新