【发布时间】:2017-02-14 18:59:06
【问题描述】:
我有一个简单的自定义视图,其中包含 onDraw 和 onTouchEvent 方法。我需要以某种方式检查用户是否将手指放在同一位置(Y 坐标)超过 2 秒。
编辑:添加了描述和代码。
我的简单自定义视图正在做的是在用户手指下方绘制一条水平线。当您在屏幕上上下移动时,水平线会跟随您。 如果用户在同一位置按住手指 2 秒,我要做的是升起一个标志(布尔值) - 换句话说,如果这条线围绕某个 Y 坐标2 秒。
public class RateView extends View {
float touchY = (getHeight() / 2);
boolean isPressed = false;
Paint paint = new Paint();
//Bunch of standard constructors
private void init(Context context, AttributeSet attr) {
paint.setColor(ContextCompat.getColor(context, R.color.line));
paint.setStrokeWidth(10);
paint.setAntiAlias(true);
}
@Override
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
//If pressed -> draw horizontal line
if (isPressed) {
canvas.drawLine(0, touchY, (canvas.getWidth()), touchY, paint);
}
}
@Override
public boolean onTouchEvent(MotionEvent motionEvent) {
switch (motionEvent.getAction()) {
case MotionEvent.ACTION_DOWN:
isPressed = true;
break;
case MotionEvent.ACTION_MOVE:
touchY = motionEvent.getY();
break;
case MotionEvent.ACTION_UP:
isPressed = false;
break;
}
invalidate();
return true;
}}
【问题讨论】:
标签: android location ondraw seconds ontouch