---
title: "android view的触摸事件坐标"
author: "uuubd"
date: "2013-04-26T00:11:24Z"
tags: []
categories: ["移动开发"]
slug: "android-view"
draft: false
lastmod: "2013-04-10T23:33:02Z"
id: 1845427
url: "https://uuubd.iteye.com/blog/1845427"
---

android view的触摸事件坐标不是屏幕坐标，是相对于view左上角的坐标。


```java

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        TextView tv = (TextView) findViewById(R.id.tv);
        tv.setOnTouchListener(new OnTouchListener() {
			public boolean onTouch(View v, MotionEvent event) {
				System.out.println("坐标：[x,y] = [" + event.getx() + "," + event.gety() + "]");
				return false;
			}
		});
    }

```



```java

<RelativeLayout 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" >
    <TextView
        android:id="@+id/tv"
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:background="#ff00ffff"
        android:gravity="center"
        android:text="click me"
        tools:context=".MainActivity" />
</RelativeLayout>

```



```java

04-10 23:27:10.889: I/System.out(10515): 坐标：[x,y] = [0.0,21.0]
04-10 23:27:11.159: I/System.out(10515): 坐标：[x,y] = [3.0,21.0]
04-10 23:27:11.369: I/System.out(10515): 坐标：[x,y] = [3.0,21.0]
04-10 23:27:11.549: I/System.out(10515): 坐标：[x,y] = [9.0,18.0]
04-10 23:27:11.699: I/System.out(10515): 坐标：[x,y] = [10.0,20.0]
04-10 23:27:11.869: I/System.out(10515): 坐标：[x,y] = [13.0,20.0]

```