我在Android上繪製屏幕時遇到了一些麻煩。我已經調試過,沒有任何東西可以關閉。屏幕只顯示黑色背景。沒有錯誤消息或任何東西。Android沒有在畫布上繪製任何東西
public class GameView extends View {
MazePanel mazePanel;
MazePanel mazePanel2;
Bitmap bmap;
Paint paint;
public GameView(Context context) {
super(context);
mazePanel = new MazePanel(context.getApplicationContext());
mazePanel2 = new MazePanel(context.getApplicationContext());
paint = new Paint();
paint.setStyle(Paint.Style.FILL);
}
@Override
protected void onDraw(Canvas canvas) {
bmap = mazePanel.getBitmap();
mazePanel.fillGraphicsOval(100, 100, 100, 100);
canvas.drawBitmap(bmap, 0, 0, paint);
}
這裏是MazePanel
public class MazePanel extends View {
private int color;
private Bitmap bmap;
private Canvas canvas;
private Paint paint;
private Path polyPath;
private DisplayMetrics metrics;
private int viewWidth;
private int viewHeight;
public enum Colors {
BLACK, GRAY, DARK_GRAY, WHITE, YELLOW, RED
};
public MazePanel(Context context) {
super(context);
metrics = context.getResources().getDisplayMetrics();
viewWidth = metrics.widthPixels;
viewHeight = metrics.heightPixels;
bmap = Bitmap.createBitmap(viewWidth, viewHeight, Config.RGB_565);
canvas = new Canvas(bmap);
paint = new Paint();
polyPath = new Path();
}
public void initbmap() {
bmap = Bitmap.createBitmap(viewWidth, viewHeight, Config.RGB_565);
}
public Canvas getCanvas() {
return canvas;
}
public void setCanvas(Canvas canvas) {
this.canvas = canvas;
}
public void setGraphicsColor(Colors colors) {
switch(colors) {
case BLACK:
paint.setColor(Color.BLACK);
break;
case GRAY:
paint.setColor(Color.GRAY);
break;
case DARK_GRAY:
paint.setColor(Color.DKGRAY);
break;
case WHITE:
paint.setColor(Color.WHITE);
break;
case YELLOW:
paint.setColor(Color.YELLOW);
break;
case RED:
paint.setColor(Color.RED);
break;
default:
break;
};
}
public void setGraphicsColor(int color) {
paint.setColor(color);
}
public void setGraphicsColor(int r, int g, int b) {
paint.setColor(Color.argb(100, r, g, b));
}
public void fillGraphicsRect(int x, int y, int width, int height) {
canvas.drawRect(x, y, x + width - 1, y + height - 1, paint);
}
public void fillGraphicsPolygon(int[] xps, int[] yps, int numPoints) {
polyPath.reset();
polyPath.moveTo(xps[0], yps[0]);
for (int i = 1; i < numPoints; i++) {
polyPath.lineTo(xps[i], yps[i]);
}
polyPath.close();
canvas.drawPath(polyPath, paint);
}
public void fillGraphicsOval(int x, int y, int width, int height) {
canvas.drawOval(new RectF(x, y, x + width - 1, y + height - 1), paint);
}
public void drawGraphicsLine(int x, int y, int fx, int fy) {
canvas.drawLine(x, y, fx, fy, paint);
}
public int createNewColor(int color) {
this.color = color;
return color;
}
public int createNewColor(int r, int g, int b) {
return Color.rgb(r, g, b);
}
public int getColor() {
return color;
}
public Bitmap getBitmap() {
return bmap;
}
public void setBitmap(Bitmap bitmap) {
bmap = bitmap;
}
public void paint(Canvas canvas) {
if (bmap == null)
initbmap();
canvas.drawBitmap(bmap, 0, 0, null);
}
public void update() {
paint(getCanvas());
}
}
我張貼了這麼多的代碼道歉,但我似乎無法找到任何錯誤。在調試時,執行onDraw方法,bmap
被引用爲mazePanel
的位圖。所以要麼fillGraphicsOval
不工作或canvas.drawBitmap()
是不正確的方法來調用。