i just found the solution myself. It lies in the onInterceptTouchEvent() function. You can override it to intercept all touch events before being handed over to the child views. So i created a layout extending FrameLayout. Surrounded my layout with this layout and thats it :)
public class Vector2D {
private float x;
private float y;
public Vector2D(float x, float y) {
this.x = x;
this.y = y;
}
public void setX(float x) {
this.x = x;
}
public float getX() {
return this.x;
}
public void setY(float y) {
this.y = y;
}
public float getY() {
return this.y;
}
}
Main.java
public class Main extends Activity implements OnTouchListener {
/** Called when the activity is first created.
* @return */
//defined as class variable so it's accessible from onTouch()
List<Vector2D> points;
@Override
public boolean onTouch(View v, MotionEvent event) {
//adds a new coordinate to the list,
//with the X and Y values of the touch
points.add(new Vector2D(event.getX(), event.getY()));
Log.d("TOUCH", "X:" + event.getX() + " Y:" + event.getY());
return true;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
points = new ArrayList<Vector2D>();
final LinearLayout ll = (LinearLayout)findViewById(R.id.linearlayout);
final TextView pointsList = (TextView)findViewById(R.id.points_list);
ll.setOnTouchListener(this);
final Button listPoints = (Button)findViewById(R.id.list_points);
listPoints.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
StringBuilder sb = new StringBuilder();
for (Vector2D vector : points) {
sb.append(
"X:" +vector.getX() + " " +
"Y:" + vector.getY() + "\n");
}
pointsList.setText(sb.toString());
}
});
}
}
Do you just want to capture how many times a specific view is clicked? Or are you wanting the precise pixel coordinates of the touch? If the former, you could just override the onTouchListener for each of them, and increment a counter for that view.
For the latter, I played around a bit with an onTouchListener, and was able to get it to work, but you'll probably have to set an onTouchListener to every view, which shouldn't be a big deal, but just something to keep in mind unless someone has a better way.
Vector2D.java
public class Vector2D {
private float x;
private float y;
public Vector2D(float x, float y) {
this.x = x;
this.y = y;
}
public void setX(float x) {
this.x = x;
}
public float getX() {
return this.x;
}
public void setY(float y) {
this.y = y;
}
public float getY() {
return this.y;
}
}
Main.java
public class Main extends Activity implements OnTouchListener {
/** Called when the activity is first created.
* @return */
//defined as class variable so it's accessible from onTouch()
List<Vector2D> points;
@Override
public boolean onTouch(View v, MotionEvent event) {
//adds a new coordinate to the list,
//with the X and Y values of the touch
points.add(new Vector2D(event.getX(), event.getY()));
Log.d("TOUCH", "X:" + event.getX() + " Y:" + event.getY());
return true;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
points = new ArrayList<Vector2D>();
final LinearLayout ll = (LinearLayout)findViewById(R.id.linearlayout);
final TextView pointsList = (TextView)findViewById(R.id.points_list);
ll.setOnTouchListener(this);
final Button listPoints = (Button)findViewById(R.id.list_points);
listPoints.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
StringBuilder sb = new StringBuilder();
for (Vector2D vector : points) {
sb.append(
"X:" +vector.getX() + " " +
"Y:" + vector.getY() + "\n");
}
pointsList.setText(sb.toString());
}
});
}
}
发布评论
评论(2)
我自己刚刚找到了解决方案。它位于 onInterceptTouchEvent() 函数中。您可以覆盖它以在将所有触摸事件移交给子视图之前拦截所有触摸事件。所以我创建了一个扩展 FrameLayout 的布局。用这个布局包围我的布局,就是这样:)
i just found the solution myself. It lies in the
onInterceptTouchEvent()
function. You can override it to intercept all touch events before being handed over to the child views. So i created a layout extending FrameLayout. Surrounded my layout with this layout and thats it :)您只想捕获特定视图被点击的次数吗?或者您想要触摸的精确像素坐标?如果是前者,您只需重写每个视图的 onTouchListener ,并增加该视图的计数器。
对于后者,我使用了 onTouchListener 进行了一些尝试,并且能够让它工作,但是您可能必须为每个视图设置一个 onTouchListener,这不应该是一个大问题,但只是需要保留的东西除非有人有更好的方法。
Vector2D.java
Main.java
Do you just want to capture how many times a specific view is clicked? Or are you wanting the precise pixel coordinates of the touch? If the former, you could just override the onTouchListener for each of them, and increment a counter for that view.
For the latter, I played around a bit with an onTouchListener, and was able to get it to work, but you'll probably have to set an onTouchListener to every view, which shouldn't be a big deal, but just something to keep in mind unless someone has a better way.
Vector2D.java
Main.java