我正在开发一个 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>
编辑:问题是,如果我在 Pixel C 上绘制此段,我会得到这些坐标:
1540.9375, 513.01074
如果我在 Galaxy Tab A 上执行此操作,我会得到以下结果:
1158.0, 279.0
最佳答案
我也解决了修改方法:
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/