I have a custom view (extends View) that is going to be a game board. What I'd like to do now is create an object (well, a bunch of them, but I can start with one) at a specific location on the board and then have the user be able to move it around by dragging it. I have the board view down, but am at a loss on how to code up the "pieces." Thanks for any help and/or pointing to the right place here.
+1
A:
Some pointers:
- You will need to capture the user's touch 'n drag. Unfortunately you can't use a GestureDetector.SimpleOnGestureListener for this (it would see it as a scroll), so you'll have to do this in your view's onTouchEvent. This is a little tricky - refer to the SimpleOnGestureListener source code for ideas on how best to do this.
- As you update the location of the object call invalidate() on the view to trigger the re-draw
- In onDraw() do the background and then draw your object on top
If you are just doing the one object at once this should be sufficient. If there's more to it that this you may have to graduate to a SurfaceView and a separate thread - but don't go there until you need to.
Colin Stewart
2010-07-01 11:29:07