Android没有在画布上绘制任何东西

时间:2014-11-21 02:09:42

标签: android android-canvas

我在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()不适合调用。

2 个答案:

答案 0 :(得分:0)

尝试在视图上设置背景颜色。 setBackgroundColor(Color.TRANSPARENT);。有一个优化,它不会绘制它认为实际上不会实际绘制的视图。

答案 1 :(得分:0)

看起来MazePanel中的setGraphicsColor方法永远不会被调用。这表明MazePanel和GameView中的paint对象都有默认颜色,这可以解释为什么你没有看到任何东西。尝试将其中一个设置为不同的颜色。