java - 比例坐标在设备上
问题描述
我正在开发一个 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 CI 上绘制这个段,得到这些坐标:
1540.9375、513.01074
如果我在 Galaxy Tab AI 上执行此操作,请获得以下信息:
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) 进行划分。
推荐阅读
- angularjs - 不同模块中的 AngularJS 参考指令
- flutter - 颤动:计数器未在底部导航栏上完整显示
- azure-logic-apps - Azure Logics-apps(preview) 触发历史
- zipkin - Zipkin UI 显示“错误:无法加载服务名称:服务器错误(确定)”
- macos - navicat 15 macos big sur,保存密码失败错误代码:- 34018
- reactjs - 单击标题时突出显示表格列
- reactjs - 如何在 Graphql Apollo Query 中传递变量
- ios - 使图形线上的边缘变得圆润而不是“锋利” Swiftui
- google-cloud-platform - 在 GCP 中,如何在容器 k8s 集群中添加标签,而不是更新?
- spring-cloud-stream - Spring Cloud Stream 3.0 StreamsBuilderFactoryBeanCustomizer