アクティビティに 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の場所を示しています。
率直に言って、なぜこれが起こっているのか途方に暮れています。