gpt4 book ai didi

android - 如何将位图从一个坐标移动到另一个坐标 - Android 开发

转载 作者:行者123 更新时间:2023-11-30 03:10:59 26 4
gpt4 key购买 nike

我想在 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 但无法使用。

最佳答案

我已经弄清楚如何使用坐标制作动画。我将解释我所做的事情,希望对其他人有所帮助:首先我将坐标保存为 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干杯!

关于android - 如何将位图从一个坐标移动到另一个坐标 - Android 开发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20986287/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com