android - 如何在android中获取屏幕的中心坐标?

标签 android

我正在尝试获取屏幕中心的 x 和 y 坐标。这是我尝试过的:

Display display = getWindowManager().getDefaultDisplay();
final Point size = new Point();
display.getSize(size);
height = size.y;
float centerY=height/2;//i expect this is the y coordinate of center

但这不起作用,当我说:

SomeImageView.setY(centerY);

我没有在屏幕中央看到它。谁能帮我解决这个问题?

谢谢

最佳答案

我猜你得到的中心坐标是正确的,但是 setY 正在设置你的 ImageView 的底部边缘的位置。在我的脑海中,你可以尝试类似的东西

SomeImageView.setY(height - SomeImageView.getDrawable().getIntrinsicHeight());

关于android - 如何在android中获取屏幕的中心坐标?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18304460/

相关文章:

Android @Intdef 用于标记如何使用它

android - 无法安装 Ionic-Native File 插件

Android:AsyncTask 如何使用等待/通知

android - 在Android Studio中构建APK时出现DuplicateFileException

java - Activity 因 NullPointerException 而崩溃(也许是我的 SQLite 数据库命令?)

android - 使用混淆器混淆后,CardView 阴影未出现在 Lollipop 中

android - 如何在jetpack compose中释放位图

Android 找不到构建工具版本 X

android - Xamarin——支持 9 补丁?

android - java.lang.NoClassDefFoundError : com. 谷歌.android.gms.gcm.GoogleCloudMessaging