【问题标题】:Top Left Coordinates of ImageView in Android displaying incorrectlyAndroid中ImageView的左上角坐标显示不正确
【发布时间】:2013-08-28 14:11:29
【问题描述】:

我在活动中有一个 ImageView,我必须检索其左上角坐标,以便我可以将 imageview 划分为 5 个触摸区域。我使用 getLocationOnScreen 来获取这些坐标。

X 坐标很好,但是 Y 坐标由于某种原因似乎有问题,总是有一个偏移量,它似乎指向窗口的顶部(通过在开发工具中启用指针触摸来验证这一点)。

这是活动代码:

protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.activity_main);

    amslerView = (ImageView) findViewById(R.id.amsler_grid);

    locText = (TextView) findViewById(R.id.locationLabel);

    amslerView.setOnTouchListener(new OnTouchListener()
    {

        @Override
        public boolean onTouch(View v, MotionEvent event)
        {
            switch (event.getAction())
            {
                case MotionEvent.ACTION_DOWN :
                    startX = (int) event.getRawX();
                    startY = (int) event.getRawY();

                    int[] loc = new int[2];
                    amslerView.getLocationOnScreen(loc);
                    Log.i("Screen Location of View", "X:" + loc[0] + "\t Y:" + loc[1]);

                    locText.setText("Location " + startX + " " + startY + "Screen View Location is " + "X:" + loc[0] + "\t Y:" + loc[1]);

                    break;

                case MotionEvent.ACTION_UP :
                    endX = (int) event.getX();
                    endY = (int) event.getY();

                    // locText.setText("End Location " + endX + "\t" +
                    // endY);
                    break;

            }

            return true;

        }
    });

}

这是 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:orientation="vertical"
    tools:context=".MainActivity" >

    <ImageView
        android:id="@+id/amsler_grid"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_weight="1"
        android:adjustViewBounds="true"
        android:contentDescription="Amsler Grid"
        android:padding="0dp"
        android:scaleType="center"
        android:src="@drawable/amsler_boundary" />

这是我面临的问题的截图:http://puu.sh/4d1LJ.jpg

如您所见,X坐标很好,但是Y坐标显示位置http://puu.sh/4d1OJ.jpg

感谢任何帮助,坦率地说,我不知道为什么会发生这种情况。

【问题讨论】:

    标签: java android imageview android-imageview


    【解决方案1】:

    终于发现哪里不对了,Y偏移是因为状态栏和ActionBar的高度都考虑到了。所以在我的情况下,偏移量是 108(ActionBar 为 75,状态栏为 33)。编写两个函数来计算这些高度并将它们从 getRawY() 返回的值中减去即可解决问题。

    【讨论】:

      【解决方案2】:

      不是这个

       case MotionEvent.ACTION_DOWN :
                      startX = (int) event.getRawX();
                      startY = (int) event.getRawY();
      

      试试这个

      case MotionEvent.ACTION_DOWN:
                      //Get X, Y coordinates from the ImageView
                      X = (int) event.getX();
                      Y = (int) event.getY();
      

      【讨论】:

      • 这是我在使用 getX() puu.sh/4d4b7.jpg> 时得到的。我已经尝试了几乎所有代码组合来尝试获取坐标,但到目前为止,Y 中总是存在偏移量。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多