如何连续从下到上移动图像?

时间:2012-12-14 12:11:26

标签: android

我一直在研究来自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的功能是,我们可以单击并将图像拖动到所需位置。我想替换它只是让图像消失或去另一个活动。请帮我改变这段代码。提前谢谢。

1 个答案:

答案 0 :(得分:0)

如果您已经使用SurfaceView,那么在onDraw(Canvas canvas)方法中代码用于从下到上移动图像是这样的。

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

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

并且从下到上移动只需更新

yPoint = yPoint - 1;

onDraw()之前的任何帖子中

愿这对你有所帮助。