java - 设备上获取的比例坐标

标签 java android coordinates scale draw

我正在开发一个 Android 应用程序,它允许用户绘图,然后检查它是否已绘制在形状内。形状的坐标通过之前创建的 .txt 检索。现在,我在Google Pixel C上创建了它,所以当用户绘制我制作的算法时不会有任何问题。我在 Galaxy Tab 上尝试了该应用程序,它的屏幕分辨率为 1920 x 1200,而且形状显然处于不同的位置。我做了这个转换坐标的方法,但它不起作用:

public void loadShapePoints () {
    BufferedReader reader = null;
    Display display = getWindowManager(). getDefaultDisplay();
    Point size = new Point();
    display. getSize(size);
    int width = size. x;
    int height = size. y;
    float tmp = 0;
    try {
        reader = new BufferedReader(new InputStreamReader(getAssets().open(protocol+cornice)));
        String mLine;
        for (int elem=0; (mLine = reader.readLine()) != null; elem++) {
            mLine = mLine.replaceAll("\\s+","");
            if (elem%2==0) {
                tmp = Float.parseFloat(mLine);
            } else {
                float x = Float.valueOf(tmp)*(width/(float)2560);
                float y = Float.valueOf(Float.parseFloat(mLine))*(height/(float)1704);
                Pair p1 = new Pair(x, y);
                points.add(p1);
            }
        }
    } catch (IOException e) { } finally {
        if (reader != null) {
            try {
                reader.close();
            } catch (IOException e) { }
        }
    }
    drawView.setShape(points);
}

我以这种方式读取文件,因为它也是结构化的: 第 1 行:x1 第 2 行:y1 第 3 行:x2... 我在转换中使用了 2560x1740,因为它是我在模拟器上用于开发应用程序的 Google Pixel C 的分辨率。 我注意到让它在 Galaxy Tab A 上工作的唯一方法是将 2600x2200 作为坐标(我不知道为什么),但显然如果我输入这个值,当我回到 Pixel 上时它就不起作用C. 我如何转换它们?问题是绘图区域被分为 3 个部分,并在不同的屏幕分辨率下调整大小吗?这是该区域的 xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FCFCFC"
android:orientation="vertical"
android:layout_marginTop="8dp"
tools:context=".PaintingActivity" >

<!-- Top Buttons -->

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="50dp"
    android:layout_gravity="center"
    android:orientation="horizontal" >


    <ImageButton
        android:id="@+id/draw_btn"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_marginRight="50dp"
        android:contentDescription="@string/brush"
        android:background="@drawable/rounded_corners"
        android:padding="13dp"
        android:layout_marginTop="5dp"
        android:src="@drawable/brush" />

    <ImageButton
        android:id="@+id/erase_btn"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_marginRight="50dp"
        android:contentDescription="@string/erase"
        android:background="@drawable/rounded_corners"
        android:padding="13dp"
        android:layout_marginTop="5dp"
        android:src="@drawable/eraser" />

    <ImageButton
        android:id="@+id/undo_btn"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:contentDescription="Undo"
        android:background="@drawable/rounded_corners"
        android:padding="16dp"
        android:layout_marginTop="5dp"
        android:src="@drawable/undo" />

</LinearLayout>

<!-- Custom View -->

<com.example.sample.DrawingView
    android:id="@+id/drawing"
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:layout_marginBottom="3dp"
    android:layout_marginLeft="5dp"
    android:layout_marginRight="5dp"
    android:layout_marginTop="3dp"
    android:layout_weight="1"
    android:background="#FFFFFFFF" />

<!-- Menu in basso -->

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:orientation="vertical" >

    <!-- Top Row -->

    <LinearLayout
        android:id="@+id/toprow"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="@string/form_intro"
            android:layout_marginTop="15dip"
            android:layout_marginBottom="25dip" />

        <EditText
            android:id="@+id/edittitle"
            android:layout_width="400dp"
            android:layout_height="wrap_content"
            android:layout_marginTop="15dip"
            android:layout_marginBottom="25dip"
            android:singleLine="true" />
    </LinearLayout>

    <!-- Bottom Row -->

    <LinearLayout
        android:id="@+id/bottomrow"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/button_1"
            android:layout_width="100dp"
            android:layout_height="50dp"
            android:layout_marginLeft="160dp"
            android:layout_marginBottom="10dp"
            android:background="@drawable/button_bg"
            android:text="Avanti"
            android:textColor="#FFFFFF" />
    </LinearLayout>
</LinearLayout>

这是它的外观和结构: enter image description here

编辑:问题是,如果我在 Pixel C 上绘制此段,我会得到这些坐标:

1540.9375, 513.01074

如果我在 Galaxy Tab A 上执行此操作,我会得到以下结果:

1158.0, 279.0

enter image description here

最佳答案

我也解决了修改方法:

public void loadShapePoints () {
    BufferedReader reader = null;
    float height = drawView.getCanvasHeight();
    float width = drawView.getCanvasWidth();
    float tmp = 0;
    try {
        reader = new BufferedReader(new InputStreamReader(getAssets().open(protocol+cornice)));
        String mLine;
        for (int elem=0; (mLine = reader.readLine()) != null; elem++) {
            mLine = mLine.replaceAll("\\s+","");
            if (elem%2==0) {
                tmp = Float.parseFloat(mLine);
            } else {
                float x = (width/2540)*(Float.valueOf(tmp));
                float y = (height/1109)*(Float.valueOf(Float.parseFloat(mLine)));
                Pair p1 = new Pair(x, y);
                points.add(p1);
            }
        }
    } catch (IOException e) { } finally {
        if (reader != null) {
            try {
                reader.close();
            } catch (IOException e) { }
        }
    }
    drawView.setShape(points);
}

问题是每个设备上的绘图区域都在减少,因此,由于它的尺寸与屏幕不同,我必须手动计算它并按 Google Pixel C 绘图区域尺寸 (2540x1109) 进行划分。

关于java - 设备上获取的比例坐标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57398361/

相关文章:

ios - 按纬度订购 MKAnnotationViews 数组?

java - 单元测试类规范

java - 如何在倒数第二个特殊字符出现后删除字符串?

java - 当你更新它的子版本时,如何级联更新@Version?

android - 一次调用多个 AsyncTask 在 Android 中不起作用

google-maps - 我可以将用户限制在 Google map 上的特定范围和缩放级别吗?

opengl - OpenGL 中球体的错误 UV 坐标?

java - 在将值与列表进行比较时从 Map 中删除值

android - 将 .aar 文件集成到多模块 android studio 项目中

android - DiffUtil 违反了 areContentTheSame 的契约(Contract) [下一版本将修复]