【问题标题】:Get the center coordinates of phone screen获取手机屏幕中心坐标
【发布时间】:2016-10-09 01:37:39
【问题描述】:

谁能给我一个如何获取当前显示屏幕的中心坐标的例子?我希望我的对象始终相对于活动视图的中心坐标。

【问题讨论】:

  • 你要坐标还是不能用重心?
  • @cricket_007 我愿意接受任何建议,我是安卓应用程序开发的新手。我有一个 iOS 应用可以做到这一点,但我对 android 最简单的等价物感兴趣。
  • 假设您只想将视图居中,请参阅此处stackoverflow.com/questions/1499258/…
  • @cricket_007 我真的想要坐标。我正在围绕 x 轴移动一个对象;所以我想将它保持在屏幕中心 x 坐标的某个范围 +/-[某个值] 内。
  • Get the screen dimensions 除以 2,然后呢?

标签: java android coordinates


【解决方案1】:

您可以尝试使用DisplayMetrics 来实现您的解决方案

int mWidth= this.getResources().getDisplayMetrics().widthPixels;
int mHeight= this.getResources().getDisplayMetrics().heightPixels;

【讨论】:

  • 如果键盘打开,这可能不会给你完整的高度。
  • @Gupta ,一个简单的解决方案是设置活动从不使用键盘调整大小(在清单中)
【解决方案2】:

这是我解决问题的方法。作为背景,我有一个按钮,一旦用户将地图移动到所需位置(显示地图中心的十字线),他们可以点击该按钮,并且地图中心会出现一个标记。

findViewById(R.id.addMarker).setOnClickListener(
            new View.OnClickListener(){
                @Override
                public  void onClick(View view) {
                    if(myMap != null){
                        double myLat = myMap.getCameraPosition().target.latitude;
                        double myLng = myMap.getCameraPosition().target.longitude;
                        LatLng markerPoint = new LatLng(myLat, myLng);
                        myMap.addMarker(new MarkerOptions().position(markerPoint)).setTitle("new marker");
                    }
                }
            }
    );

【讨论】:

    【解决方案3】:

    int cx =screenHeight = displaymetrics.heightPixels; int cy = screenWidth = displaymetrics.widthPixels; float finalRadius = (float) Math.sqrt(cx * cx + cy * cy);

    float 是对象的中心

    【讨论】:

      猜你喜欢
      • 2020-04-13
      • 2012-02-02
      • 2021-10-22
      • 2015-08-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-21
      • 1970-01-01
      • 2011-03-24
      相关资源
      最近更新 更多