【问题标题】:How to move a bitmap from one coordinate to another - Android development如何将位图从一个坐标移动到另一个坐标 - Android 开发
【发布时间】:2014-01-08 03:07:45
【问题描述】:

我想沿SurfaceView 中的连续坐标移动位图图像。我在SurfaceView 上的坐标(x1, y1) 上绘制了一个位图myBall,如下(部分代码)(

    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);
            }

  }

现在我想将其移至(x2, y2),然后移至(x3, y3),然后……根据需要,一个接一个。我曾尝试使用TranslateAnimation,但做不到。

【问题讨论】:

  • 只是增加或减少 (x1,y1) 例如x1=x1+5, y1=y1+2
  • 很抱歉,Pradeep 我没有明白你的意思。如果我改变 x1 和 y1 的值,图像将以不同的坐标绘制。我想要的是从一个点到下一个点的动画。提前谢谢!

标签: android animation bitmap surfaceview


【解决方案1】:

我已经弄清楚如何使用坐标制作动画。我将解释我所做的事情,希望对其他人有所帮助: 首先,我将坐标保存为 Point 对象

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\

离开实现 Runnable 并使用 onDraw() 方法代替 run() 方法,如下所示:

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
}
}

我使用 try 和 catch 来避免 IndexOutOfBoundryExeption 干杯!

【讨论】:

    【解决方案2】:

    我认为this 可能是您所追求的。您要做的是补间位图,有几种方法可以做到这一点。还有 ViewPropertyAnimator,它可以为整个视图设置动画。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-03-16
      • 2013-03-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多