1

I want to move a bitmap image along consecutive coordinates in SurfaceView. I have a bitmap myBall drawn in the coordinate (x1, y1) on a SurfaceView as follows (partial code)(

    public class MainSurfaceView extends SurfaceView implements Runnable {...
        ...
        @Override
        public void run() {
            while (isRunning) {
                if (!myHolder.getSurface().isValid())
                    continue; 
                Canvas canvas;// Define canvas to paint on it
                canvas = myHolder.lockCanvas();
                //Draw full screen rectangle to hold the floor map.
                Rect dest = new Rect(0, 0, getWidth(), getHeight());
                Paint paint = new Paint();
                paint.setFilterBitmap(true);
                canvas.drawBitmap(bgImage, null, dest, paint);
                //This is the ball I want to move
                canvas.drawBitmap(myBall, x1, y1, null);

                myHolder.unlockCanvasAndPost(canvas);
            }

  }

Now I want to move it to (x2, y2) then (x3, y3) and ... as many as needed and one after the other. I have tried to use TranslateAnimation but couldn't do it.

Pradeep Pati
  • 5,779
  • 3
  • 29
  • 43
Ermi
  • 201
  • 3
  • 9
  • just increase or decrease (x1,y1) e.g. x1=x1+5, y1=y1+2 – Ketan Ahir Jan 08 '14 at 03:43
  • I am sorry Pradeep I didn't get your point. If I change the value of x1 and y1 the image will be drawn in different coordinate. What I want is to animate it from one point to the next. Thanks ahead! – Ermi Jan 08 '14 at 04:07

2 Answers2

1

I have figured out how to animate using a coordinates. I will explain what I did hopping that it will be helpful to others: First I saved the coordinates as Point object

List<Point> point_list = new ArrayList<Point>();
point_list.add(new Point(x_value, y_value));//Add the x and y coordinates to the Point\

Leave implementing Runnable and use onDraw() method instead of run() method as follows:

public class MainSurfaceView extends SurfaceView {...
  .... 
@Override
protected void onDraw(Canvas canvas) {
// Draw full screen rectangle to hold the floor map.
Rect fArea = new Rect(0, 0, getWidth(), getHeight());
    Paint paint = new Paint();
paint.setFilterBitmap(true);
// draw the paint on the rectangle with the floor map
canvas.drawBitmap(bgImage, null, fArea, paint);

// Get the coordinates of x & y as Point object
List<Point> myPoints = point_list;
// Start printing myBall on the floor (view)
try {
   if (index < myPoints.size()) {
   // Increment the value of index and use it as index for the point_list
   index++;
}
// Print myBall in each coordinates of x & y using the index 
canvas.drawBitmap(myBall, myPoints.get(index).x, myPoints.get(index).y, null);
} catch (IndexOutOfBoundsException e) {
      // TODO: handle exception
}
}

I use try and catch to avoid IndexOutOfBoundryExeption Cheers!

Ermi
  • 201
  • 3
  • 9
0

I think this might be what you're after. What you're trying to do is tween the bitmap and there are a few ways you can do that. There is also the ViewPropertyAnimator, which can animate an entire view.

Community
  • 1
  • 1
Steven Trigg
  • 1,903
  • 2
  • 19
  • 22