gpt4 book ai didi

android - 如何让图片从下往上连续移动?

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:28:10 26 4
gpt4 key购买 nike

我一直在研究 http://obviam.net/index.php/a-very-basic-the-game-loop-for-android/ 中的示例在此我想做一些改变。

Speed.java

public class Speed {

public static final int DIRECTION_RIGHT = 1;
public static final int DIRECTION_LEFT = -1;
public static final int DIRECTION_UP = -1;
public static final int DIRECTION_DOWN = 1;

private float xv = 1; // velocity value on the X axis
private float yv = 1; // velocity value on the Y axis

private int xDirection = DIRECTION_RIGHT;
private int yDirection = DIRECTION_DOWN;

public Speed() {
this.xv = 1;
this.yv = 1;
}

public Speed(float xv, float yv) {
this.xv = xv;
this.yv = yv;
}

public float getXv() {
return xv;
}
public void setXv(float xv) {
this.xv = xv;
}
public float getYv() {
return yv;
}
public void setYv(float yv) {
this.yv = yv;
}

public int getxDirection() {
return xDirection;
}
public void setxDirection(int xDirection) {
this.xDirection = xDirection;
}
public int getyDirection() {
return yDirection;
}
public void setyDirection(int yDirection) {
this.yDirection = yDirection;
}

// changes the direction on the X axis
public void toggleXDirection() {
xDirection = xDirection * -1;
}

// changes the direction on the Y axis
public void toggleYDirection() {
yDirection = yDirection * -1;
}

}

使用它,图像可以向各个方向移动。现在我只想限制这个从下到上的移动。 onclick 的功能是,我们可以点击并拖动图像到需要的位置。我想用只是让图像消失或转到另一个 Activity 来替换它。请帮助我更改此代码。提前致谢。

最佳答案

如果您使用过 SurfaceView,那么在 onDraw(Canvas canvas) 方法中,将图像从底部移动到顶部的代码是这样的。

canvas.drawBitmap(bitmap,xPoint,yPoint,paint);

其中 bitmap 是一个图像(要移动的位图),xPoint 是 x 坐标,yPoint 是 y 坐标,paint 是 Paint,也可以为 null。

对于从下到上的移动,只需更新

yPoint = yPoint - 1;

onDraw() 调用之前的任何线程中。

希望这对你有帮助。

关于android - 如何让图片从下往上连续移动?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13878451/

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