Reputation: 724
I have a class with animation on the canvas. I need to handle touching, but OnTouchListener doesn't work. I tried to put a listener to main activity, but it didn't work anyway.
public class Animation extends View implements View.OnTouchListener{
private Paint paint;
private Snake snake;
public Animation(Context context) {
super(context);
snake = new Snake(10, 10, 1, 0, 1, 50);
paint = new Paint();
paint.setColor(Color.BLACK);
}
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
snake.move(canvas);
invalidate();
}
public boolean onTouch(View v, MotionEvent event) {
System.out.println("asfaf");
float x = event.getX();
float y = event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
System.out.println(x + ' ' + y);
break;
case MotionEvent.ACTION_MOVE:
System.out.println(x + ' ' + y);
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL:
System.out.println(x + ' ' + y);
break;
}
return true;
}
}
Upvotes: 0
Views: 2614
Reputation: 16910
Remove implements View.OnTouchListener
from your class, and put an @Override
on your onTouchEvent()
method.
Upvotes: 1
Reputation: 2418
Replace your onTouch()
method with onTouchEvent()
.
@Override
public boolean onTouchEvent(MotionEvent event) {
System.out.println("asfaf");
float x = event.getX();
float y = event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
System.out.println(x + ' ' + y);
break;
case MotionEvent.ACTION_MOVE:
System.out.println(x + ' ' + y);
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL:
System.out.println(x + ' ' + y);
break;
}
return true;
}
and remove implements View.OnTouchListener
.
Upvotes: 3