Android - Problem detecting onTouchListener
So I have a customdrawableview applied to my activity.
I'm trying to implement a motion listen to the view so that I can detect different touch events in different locations. However, I don't seem to even get a response from Touch Down.
Here's the relevant part of my code:
public class CustomDrawableView extends View implements OnTouchListener
{
public CustomDrawableView(Context context)
{
super(context);
}
@Override
protected void onDraw(Canvas canvas)
{
super.onDraw(canvas);
mDrawBackGround(canvas);
mDrawHexPanel(canvas);
mDrawHuePanel(canvas);
mDrawGreyScaleHexPanel(canvas);
mDrawHuePointer(canvas);
}
@Override
public boolean onTouch(View CustomDrawableView, MotionEvent event)
{
float touchX = event.getX();
float touchY = event.getY();
switch (event.getAction())
开发者_Python百科 {
case MotionEvent.ACTION_DOWN:
pointerTouch=true;
cpRed=255;
cpGreen=108;
cpBlue=0;
invalidate();
break;
}
return true;
}
So what am I doing wrong?
To get multi-touch events, you should use the methods getX(int pointer) and getY(int pointer) which returns the position of each touch point.
You can know how many fingers are on screen with the method getPointerCount().
(Methods from the MotionEvent)
Also, the ACTION_DOWN are fired only when the finger touch for the first time, if it's drag, the next events are going to be ACTION_MOVE.
You are overriding onTouch(View arg0, MotionEvent arg1), but to listen the touch events from the View you are creating, you should override onTouchEvent(MotionEvent evt).
At the moment your class only implements the interface. You have to register the OnTouchListener to your view by calling this.setOnLongClickListener(this).
Add the listener registration in the constructor of the CustomDrawableView
class
精彩评论