[英]Android: OnTouch not responding.
I want an action to be performed when the view is touched. 我希望在触摸视图时执行一项操作。 However, the touches do not respond.
但是,触摸不响应。 The app doesn't crash, it just seems to ignore it.
该应用程序不会崩溃,只是似乎忽略了它。
public class CustomDrawableView extends View implements OnTouchListener
{
static final int width = 100;
static final int height = 50;
public CustomDrawableView(Context context)
{
super(context);
setFocusable(true);
setOnTouchListener(mCustomDrawableView);
mDrawable = new ShapeDrawable(new OvalShape());
mDrawable.getPaint().setColor(0xff74AC23);
mDrawable.setBounds(x, y, x + width, y + height);
}
@Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction()==MotionEvent.ACTION_DOWN )
{
x = 400;
return true;
}
else {
x = 300;
return false;
}
}
protected void onDraw(Canvas canvas)
{
int mCanvasHeight = canvas.getHeight();
int mCanvasWidth = canvas.getWidth();
canvas.save();
canvas.rotate(R,x,y);
if (y >= mCanvasHeight-100) {
y = 0;
}
RectF oval = new RectF(x, y, x + width, y
+ height); // set bounds of rectangle
Paint p = new Paint(); // set some paint options
p.setColor(Color.BLUE);
canvas.drawOval(oval, p);
canvas.restore();
invalidate();
}
}
I have tried a bunch of different code to fix it. 我尝试了一堆不同的代码来修复它。 None of it does anything except if I change setOnTouchListener(mCustomDrawable) to mCustomDrawableView.setOnTouchListener(this) the app crashes.
除了将setOnTouchListener(mCustomDrawable)更改为mCustomDrawableView.setOnTouchListener(this)以外,其他任何操作均不起作用。 There is a bunch more code in the activity that I did not put up.
活动中还有很多我没有提出的代码。
I'm guessing you believe that the onTouch()
isn't firing because you're not seeing your graphics change in response to changes in x
or whatever. 我猜您相信
onTouch()
不会触发,因为您没有看到图形响应x
或其他任何变化而改变。 If that's the case, it looks like you're missing a call to invalidate()
in your touch handler to cause the View
to redraw itself again (via a call to onDraw()
). 如果是这种情况,似乎您缺少在触摸处理程序中对
invalidate()
的调用,从而导致View
再次重绘自身(通过调用onDraw()
)。
Also, you have an invalidate()
actually inside onDraw()
itself which really shouldn't be there. 另外,实际上在
onDraw()
本身中有一个invalidate()
实际上不应该在其中。 It would certainly cause your View
to redraw itself over and over - I suppose - so actually, I guess you should be seeing updates because that's there. 我想,这肯定会导致您的
View
地重绘自身,因此,实际上,我想您应该看到更新,因为在那里。 But that isn't the way you should make a View
animate; 但这不是使
View
动画的方式; you should instead use a Thread
or Handler
to schedule a regular, periodic invalidate()
- or some other means to regularly schedule an update. 您应该改为使用
Thread
或Handler
来安排定期的,定期的invalidate()
-或通过其他方式来定期安排更新。
Also there should be no need to implement 'OnTouchListener' when you can just override onTouch()
as you have done. 同样,当您可以像完成操作一样覆盖
onTouch()
时,也无需实现“ OnTouchListener”。 There are a couple of ways you can detect touch events for a View
: (1) Override onTouch()
as you have done, to get touch events on that View
. 您可以通过以下两种方法来检测
View
触摸事件:(1)像这样覆盖onTouch()
,以获取该View
触摸事件。 (2) Register a listener using setOnTouchListener()
. (2)使用
setOnTouchListener()
注册一个侦听器。 This latter option enables you to have a listener that listens to touch events from one View
or multiple View
s, and it also 'sees' touch events before a registered View
's own onTouch()
sees them. 后者的选择使您能够侦听从一个触摸事件的侦听器
View
或多个View
S,同时也“看到”触摸注册前的事件View
自己的onTouch()
看到他们。
Another thing I see is that you're setting x
to 400 when you get an ACTION_DOWN event but then you're setting it to 300 for any other kind of event such as an ACTION_MOVE. 我看到的另一件事是,当您收到ACTION_DOWN事件时,将
x
设置为400,但是对于其他任何类型的事件,例如ACTION_MOVE,则将其设置为300。 Considering it's actually quite difficult to keep your finger still enough to never cause a string of ACTION_MOVE events immediately after an ACTION_DOWN, perhaps you're just never seeing the graphics when x
is at 400 or something. 考虑到实际上很难保持手指足够静止以致在ACTION_DOWN之后永远不会立即引起一连串的ACTION_MOVE事件,也许当
x
等于400时您永远都看不到图形。
You really need to post a complete example (for instance your code as you've posted has setOnTouchListener(mCustomDrawableView)
where mCustomDrawableView
has not been defined anywhere) together with specific errors from Logcat. 您确实需要发布一个完整的示例(例如,发布的代码具有
setOnTouchListener(mCustomDrawableView)
,其中尚未在任何地方定义setOnTouchListener(mCustomDrawableView)
以及mCustomDrawableView
特定错误。 Also, you should use the debugger to see if your onTouch
ever executes (stick a breakpoint in it). 另外,您应该使用调试器查看您的
onTouch
是否曾经执行过(在其中插入一个断点)。
Also, you should accept some answers. 另外,您应该接受一些答案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.