Reputation: 9309
I'm learning how to do a simple game and I have a MainActivity class where I implements onTouchListener and then I have a GameLoop class that extends SurfaceView and implements Runnable. The GameLoop class also handles the rest of the classes like a manager class for the moving sprites. It's inside the GameLoop class that the drawing stuff on the canvas is done.
What I need to do is to draw lines with my finger tips, but I'm not sure how I should handle the values from the onTouchListeners to be able to draw lines. I guess it won't work if I have the onTouchListener inside the MainActivity!? And I guess I can't implement it to GameLoop class since I already implement Runnable!?
How can I implement the onTouchListener to the GameLoop class if I'm already implementing something else? I guess I cant implement more than one interface? When I try I get errors in Eclipse!
Upvotes: 1
Views: 316
Reputation: 1940
Implement your listener on the view your finger is drawing against, using setOnTouchListener()
and pass in your listener class. Since you know there is a MotionEvent
object passed into onTouch()
then you will need to utilize that object by using event.getX()
and event.getY()
to determine where your pointer, in this case your finger, is at on the screen. Notice you will need to deliberately handle multi-touch if you need that function. These are all documented here.
Do attaching your listener in your activity, and you can pass that MotionEvent
object to your view to extract useful information and call drawing methods you define inside your view object and invalidate()
. This assumes you want to concentrate all drawing methods in that view class.
Alternatively, you can draw stuff directly in your activity, maybe putting some new views or other stuff. As long as you have the MotionEvent
object, you will have the coordinates of your pointers(finger) and you can use that information to draw.
The activity in which you want to setOnTouchListener()
will most likely be the activity that is directly associated with the view you draw your line against, either by having a relatively persistent reference to that view, or by binding to that view using setContentView()
.
You can't implement more than one interface in the same class in Java. This is not an Eclipse or ADT issue. If you do want to do that however, extend a interface and add new stuff to it. Using inheritance you literally add multiple interfaces together.
Upvotes: 0
Reputation: 10242
A Java class can only extend one class but it can implement any number of interfaces. Simply comma-separate the interface names you wish to implement in your class declaration:
public MyClass extends BaseClass implements OnClickListener, OnTouchListener, FooListener
You could also use an anonymous listener:
someView.setOnTouchListener(new OnTouchListener() {
@Override
boolean onTouch(View v, MotionEvent event) {
// do something
}
});
Another option is to override onTouchEvent() in your Activity and pass the motion event to whatever class you wish to handle the touch event.
Upvotes: 1