Android 改变位图的位置
Android change position of the bitmap
我想问一下如何更改位图的位置,我正在使用以下代码。我从一个似乎有同样问题的人那里看了下面的问题,但我已经尝试过这个并且收到了不同的动画位置。
android positioning a drawBitmap that uses rect
public class Yodasprite implements Drawable {
private static final int BMP_COLUMNS = 6;
private static final int BMP_ROWS = 1;
private GameContent gameContent;
float fracsect;
private int x = 0;
private int y = 0;
int xSpeed = 5;
private Bitmap bmp;
private int currentFrame = 0;
private int width;
private int height;
long current_time;
long lasttime;
long delta;
public Yodasprite(GameContent gameContent, Bitmap bmp) {
this.gameContent = gameContent;
this.bmp = bmp;
this.width = bmp.getWidth() / BMP_COLUMNS;
this.height = bmp.getHeight() / BMP_ROWS;
this.lasttime = System.currentTimeMillis();
}
@Override
public void update(float fracsec) {
if (x > gameContent.getGameWidth() - width + xSpeed) {
xSpeed = -5;
}
if (x + xSpeed < 0) {
xSpeed = 5;
}
if (delta / fracsec >999) {
x = x + xSpeed;
currentFrame = ++currentFrame % BMP_COLUMNS;
}
}
@Override
public void draw(Canvas canvas) {
current_time = System.currentTimeMillis();
delta = current_time - lasttime;
fracsect = (float) delta / 1000;
update(fracsect);
int srcX = currentFrame * (width -4);
int srcY = height - height;
Rect src = new Rect(srcX, srcY,srcX + width+5,srcY+height);
Rect dst = new Rect(x,10,x+width, y+height);
canvas.drawBitmap(bmp, src, dst, null);
}
}
Rect dest 根据我从文档中的理解以及上面的 post 确定 canvas 上的 x,y 位置。我的精灵在 0,0 处生成,我想通过 y 向下移动精灵。有什么建议和/或帮助吗?像下面的代码一样向下移动精灵似乎不起作用。
Rect dst = new Rect(x,10,x+width, y+height);
Rect dst = new Rect(x,10,x+width, y+height);
更改为:
Rect dst = new Rect(x,y,x+width, y+height);
之前实例化 y 的值给了我正在寻找的解决方案。显然硬编码数字不起作用。
我想问一下如何更改位图的位置,我正在使用以下代码。我从一个似乎有同样问题的人那里看了下面的问题,但我已经尝试过这个并且收到了不同的动画位置。
android positioning a drawBitmap that uses rect
public class Yodasprite implements Drawable {
private static final int BMP_COLUMNS = 6;
private static final int BMP_ROWS = 1;
private GameContent gameContent;
float fracsect;
private int x = 0;
private int y = 0;
int xSpeed = 5;
private Bitmap bmp;
private int currentFrame = 0;
private int width;
private int height;
long current_time;
long lasttime;
long delta;
public Yodasprite(GameContent gameContent, Bitmap bmp) {
this.gameContent = gameContent;
this.bmp = bmp;
this.width = bmp.getWidth() / BMP_COLUMNS;
this.height = bmp.getHeight() / BMP_ROWS;
this.lasttime = System.currentTimeMillis();
}
@Override
public void update(float fracsec) {
if (x > gameContent.getGameWidth() - width + xSpeed) {
xSpeed = -5;
}
if (x + xSpeed < 0) {
xSpeed = 5;
}
if (delta / fracsec >999) {
x = x + xSpeed;
currentFrame = ++currentFrame % BMP_COLUMNS;
}
}
@Override
public void draw(Canvas canvas) {
current_time = System.currentTimeMillis();
delta = current_time - lasttime;
fracsect = (float) delta / 1000;
update(fracsect);
int srcX = currentFrame * (width -4);
int srcY = height - height;
Rect src = new Rect(srcX, srcY,srcX + width+5,srcY+height);
Rect dst = new Rect(x,10,x+width, y+height);
canvas.drawBitmap(bmp, src, dst, null);
}
}
Rect dest 根据我从文档中的理解以及上面的 post 确定 canvas 上的 x,y 位置。我的精灵在 0,0 处生成,我想通过 y 向下移动精灵。有什么建议和/或帮助吗?像下面的代码一样向下移动精灵似乎不起作用。
Rect dst = new Rect(x,10,x+width, y+height);
Rect dst = new Rect(x,10,x+width, y+height);
更改为:
Rect dst = new Rect(x,y,x+width, y+height);
之前实例化 y 的值给了我正在寻找的解决方案。显然硬编码数字不起作用。