更新 Canvas 动画
Update Canvas Animation
我不需要回答了!
我创建了一个应用程序,屏幕上应该有气泡弹跳。
一切正常,但没有实时绘制它们。
这里是代码(不是所有代码,而是重要代码):
泡泡Class:
public class Bubble{
private int ID;
private float coordX;
private float coordY;
private float velX;
private float velY;
private float radius;
private int color;
private boolean popped;
private boolean alive;
private boolean dead;
public Bubble(int coordX, int coordY, int velocityX, int velocityY, int ID, int color) {
this.coordX = coordX;
this.coordY = coordY;
this.velX = velocityX;
this.velY = velocityY;
this.radius = 30;
this.ID = ID;
this.color = color;
this.alive = true;
}
public void drawMe(Paint paint, Canvas canvas) {
int paintStartColor = paint.getColor();
paint.setColor(this.color);
canvas.drawCircle(this.coordX, this.coordY, radius, paint);
paint.setColor(paintStartColor);
Log.d("Bubble","drawn bubble [" + ID + "] at (" + this.coordX + "|" + this.coordY + ")");
}
public void update() {
//damit die bubble am rand anstößt und zurück prallt
if(this.coordX - this.radius <= 0) this.velX = - this.velX;
if(this.coordY - this.radius <= 0) this.velY = - this.velY;
if(this.coordX + this.radius >= MainActivity.getInstance().getScreenSize().x) this.velX = - this.velX;
if(this.coordY + this.radius >= MainActivity.getInstance().getScreenSize().y) this.velY = - this.velY;
coordX += velX;
coordY += velY;
updateRadius();
// Log.d("Bubble", "CoordX: " + coordX + ", velX: " + velX);
}
}
绘制方法:
public void drawBubbles() {
Paint paint = new Paint();
for(Bubble b : bubbles) {
b.drawMe(paint, MainActivity.getInstance().getCanvas());
}
}
以及游戏循环:
while(this.isRunning()) {
updateBubbles(); //update bubbles just calls the update() function in every bubble (for each loop)
drawBubbles();
//...sleep...
}
绘制所有气泡,效果很好,但当我离开(而不是关闭!)应用程序然后返回时,我只看到了不同之处。我循环调用方法,每秒调用60次,为什么屏幕上没有显示?我也不能使 canvas.
无效
我只是误用了 invalidate() 方法,所以在我修复它后它工作正常。
我不需要回答了!
我创建了一个应用程序,屏幕上应该有气泡弹跳。 一切正常,但没有实时绘制它们。
这里是代码(不是所有代码,而是重要代码):
泡泡Class:
public class Bubble{
private int ID;
private float coordX;
private float coordY;
private float velX;
private float velY;
private float radius;
private int color;
private boolean popped;
private boolean alive;
private boolean dead;
public Bubble(int coordX, int coordY, int velocityX, int velocityY, int ID, int color) {
this.coordX = coordX;
this.coordY = coordY;
this.velX = velocityX;
this.velY = velocityY;
this.radius = 30;
this.ID = ID;
this.color = color;
this.alive = true;
}
public void drawMe(Paint paint, Canvas canvas) {
int paintStartColor = paint.getColor();
paint.setColor(this.color);
canvas.drawCircle(this.coordX, this.coordY, radius, paint);
paint.setColor(paintStartColor);
Log.d("Bubble","drawn bubble [" + ID + "] at (" + this.coordX + "|" + this.coordY + ")");
}
public void update() {
//damit die bubble am rand anstößt und zurück prallt
if(this.coordX - this.radius <= 0) this.velX = - this.velX;
if(this.coordY - this.radius <= 0) this.velY = - this.velY;
if(this.coordX + this.radius >= MainActivity.getInstance().getScreenSize().x) this.velX = - this.velX;
if(this.coordY + this.radius >= MainActivity.getInstance().getScreenSize().y) this.velY = - this.velY;
coordX += velX;
coordY += velY;
updateRadius();
// Log.d("Bubble", "CoordX: " + coordX + ", velX: " + velX);
}
}
绘制方法:
public void drawBubbles() {
Paint paint = new Paint();
for(Bubble b : bubbles) {
b.drawMe(paint, MainActivity.getInstance().getCanvas());
}
}
以及游戏循环:
while(this.isRunning()) {
updateBubbles(); //update bubbles just calls the update() function in every bubble (for each loop)
drawBubbles();
//...sleep...
}
绘制所有气泡,效果很好,但当我离开(而不是关闭!)应用程序然后返回时,我只看到了不同之处。我循环调用方法,每秒调用60次,为什么屏幕上没有显示?我也不能使 canvas.
无效我只是误用了 invalidate() 方法,所以在我修复它后它工作正常。