首页 > 解决方案 > 我的视图 land_layout 无法正确定位点

问题描述

我正在做 2D 绘图(使用 View.onDraw)。我发现的所有示例都在 TextView 中使用按钮完成工作。我已经简化为一个测试项目,消除了我的问题源于我更复杂的类的任何可能性。我确信我的问题必须源于布局。我想我需要布局帮助。

在测试中,我定义了一个圆的位置。在纵向和横向中:在我期望的位置绘制定义的圆圈(在 onDraw() 中)。

mDetector = new GestureDetectorCompat(context, gestureListener, mhandler);我通过随后捕获鼠标点击onTouchEvent(MotionEvent event) ,然后private GestureDetector.OnGestureListener gestureListener = new GestureDetector.OnGestureListener()包含onSingleTapup(MotionEvent event)...我捕获event.getX()event.getY()。最后,通过invalidate()onDraw()在我的接触点画一个圆圈。

在纵向:圆圈出现在我的鼠标指针下方。在风景中:圆圈出现在离我的鼠标指针有一段距离的地方

当我在屏幕中向下单击时,当我向左或向上单击时,绘制的点更向右(反之亦然)。

(我在这个简化的测试项目中不做任何翻译;但在我更复杂的项目中,翻译将我绘制的对象移动到与鼠标移动相反的方向。)

我有一个 layout/activity_main 和一个 layout-land/activity_main。它们是相同的,除了背景颜色(这表明我正在使用正确的布局)。

几天来我一直在搞乱不同的布局......试图解决这个问题。

这是我当前的布局文件:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true">

        <androidx.constraintlayout.widget.ConstraintLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            xmlns:tools="http://schemas.android.com/tools"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            tools:context=".MainActivity"
            tools:showIn="@layout/activity_main">

            <appview.GraphicsView
                android:id="@+id/graphicsView"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="0.15"
                android:background="@color/teal_700"
                android:focusableInTouchMode="true"
                android:paddingLeft="8dp"
                android:paddingRight="8dp"
                android:rotationX="0"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent" />

        </androidx.constraintlayout.widget.ConstraintLayout>
    </FrameLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

会有比我更了解 Android 布局的人吗?请来帮我。

标签: javaandroidlayouttouch-eventlandscape-portrait

解决方案


推荐阅读