What I want to do is to draw a line that will follow my finger. I've created
a custom view, and I have an onTouchEvent()
that works.
I can draw a static line in the onDraw()
method without much trouble.
I'm not really sure how to get the line to draw as my finger moves though.
public boolean onTouchEvent(MotionEvent event) {
super.onTouchEvent(event);
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN: {
Log.e(TAG, " - DOWN -");
Log.e(TAG, " getX: " + event.getX());
break;
}
case MotionEvent.ACTION_UP: {
Log.e(TAG, " - UP -");
Log.e(TAG, " getX: " + event.getX());
break;
}
}
return true;
}
Any hints you guys who have been doing it a while can give?
Do I need to set coordinates on the onTouchEvent()
and constantly invalidate the view
so the little line segments draw?
In the end I just want to be able to basically doodle on the screen using my finger for this experiment.
Its been a while but this post still gets some views so I figured i'll post some helpful stuff:
Tutorial on how to make an object follow a line: http://www.rengelbert.com/tutorial.php?id=182
This is a good free game engine that the above tutorial uses also: http://libgdx.badlogicgames.com/
Hope this helps someone out!
Here's what I ended up doing. Hopefully this helps some other beginners out there get started.
I have a Sprite class that represents the object I want to move on the screen:
Then I have a main game loop. That loops through and calls my mainPanel's render and update methods which look like this:
The position of where the sprite will move is handled in the motion event capture:
Hopefully that is helpful. If I trimmed out too much and there is something you don't get let me know.
Next step, making the object follow the line!
Also check out the Java Path class. You can use this to draw path...as you move your finger across the screen. with every update(however you implement this - every so many pixels from the last update for example) you add the x,y coordinate to your Path and re-render the total path through a loop. just an idea that I am playing around with now.
You're only tracking the up and down events. Track the ACTION_MOVE event too. Beware that it will track continuously, even if the person's finger isn't apparently moving. Your code should go something like this:
ACTION_DOWN: Store position.
ACTION_MOVE: If position is different from stored position then draw a line from stored position to current position, and update stored position to current.
ACTION_UP: Stop.
In the ACTION_MOVE bit, it might be a good idea to check if the position is at least 2 or 3 pixels away from the stored position. If you're going to store all of the plot points, so you can do something with the data later, then maybe increase that to 10 pixels so you don't end up with hundreds of points for a simple line.
For newbies this code will help you to create doodle image and export it into Png image here Is the Complete CODE and this the Activity Class With contains a View class too..
A touch event is associated with a list of pointer counts retrievable like the following:
iterating over these and drawing points can cause a continuous line to appear
Assuming
paint
is already set andc
is the canvas, which may need to be locked (Eg- in a multithread application), prior to drawing on it.