I use a ColorBall class from a earlier example, and let the balls now being dragged.
The ColorBall.class holds the images and also the coordinates on the screen.
Using java Syntax Highlighting
- canvas.drawBitmap(ball.getBitmap(), ball.getX(), ball.getY(), null);
Parsed in 0.010 seconds, using GeSHi 1.0.8.4
The DrawView.class is doing all the work, and looks also for the touch events.
Using java Syntax Highlighting
- // events when touching the screen
- public boolean onTouchEvent(MotionEvent event) {
- int eventaction = event.getAction();
- int X = (int)event.getX();
- int Y = (int)event.getY();
- switch (eventaction ) {
- case MotionEvent.ACTION_DOWN: // touch down so check if the finger is on a ball
- for (ColorBall ball : colorballs) {
- // check all the bounds of the ball
- if (X > ball.getX() && X < ball.getX()+50 && Y > ball.getY() && Y < ball.getY()+50){
- balID = ball.getID();
- break;
- }
- }
- break;
- case MotionEvent.ACTION_MOVE: // touch drag with the ball
- // move the balls the same as the finger
- colorballs[balID-1].setX(X-25);
- colorballs[balID-1].setY(Y-25);
- break;
- case MotionEvent.ACTION_UP:
- // touch drop - just do things here after dropping
- break;
- }
- // redraw the canvas
- invalidate();
- return true;
- }
Parsed in 0.012 seconds, using GeSHi 1.0.8.4