我需要有关从TouchEvent获取信息的帮助。例如,如果我绘制一条线,我需要ACTION_DOWN已经启动的信息以及该操作已经完成的位置(例如:started:top,finished:left)。 我包括图像所以这可以更清楚。
http://i.imgur.com/ILEEHT5.png
这是代码(用于绘图):
package com.mmilosanovic.trafficcounting;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.os.Bundle;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
public class JunctionView extends View {
private Paint paint = new Paint();
private Path path = new Path();
public JunctionView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
paint.setAntiAlias(true);
paint.setStrokeWidth(6f);
paint.setColor(Color.BLUE);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeJoin(Paint.Join.ROUND);
}
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
canvas.drawPath(path, paint);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
float eventX = event.getX();
float eventY = event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
path.moveTo(eventX, eventY);
return true;
case MotionEvent.ACTION_MOVE:
path.lineTo(eventX, eventY);
break;
case MotionEvent.ACTION_UP:
break;
default:
return false;
}
invalidate();
return true;
}
}
提前致谢。
答案 0 :(得分:1)
您需要自己跟踪动作。创建类float startX, startY
之类的类变量,并在获得ACTION_DOWN
时分配给它们。然后,当您获得ACTION_UP
时,您可以与这些值进行比较。
答案 1 :(得分:0)
您可以使用:
@Override
public boolean onTouchEvent(MotionEvent event) {
int x = (int)event.getX();
int y = (int)event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
Toast.makeText(activity, "Down", Toast.LENGTH_SHORT).show();
break;
case MotionEvent.ACTION_MOVE:
Toast.makeText(activity, "Move", Toast.LENGTH_SHORT).show();
break;
case MotionEvent.ACTION_UP:
Toast.makeText(activity, "Up", Toast.LENGTH_SHORT).show();
break;
}
return false;
}
答案 2 :(得分:0)
如何获取ACTION已从顶部开始并已在屏幕左侧完成的信息?