在画布上画画不起作用

时间:2016-08-05 13:49:05

标签: android canvas

我有以下xml设置:

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/content"
        android:orientation="vertical">
    </LinearLayout>

    <ListView
        android:id="@+id/navList"
        android:layout_width="250dp"
        android:layout_height="match_parent"
        android:layout_gravity="left|start"
        android:background="#cccccc"/>
</android.support.v4.widget.DrawerLayout>

我现在想在线性布局中绘制画布,因此我这样做:

Radar radar = new Radar(this, this.width);
content.addView(radar);

这是雷达类:

private int width;

public Radar(Context context, int width){
    super(context);
    this.width = width;
}

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    Paint circlePaint = new Paint();
    circlePaint.setColor(0xFF00FF00);
    circlePaint.setStyle(Paint.Style.FILL);

    Rect rect = new Rect();
    rect.set(0, 0, canvas.getWidth(), canvas.getHeight() / 2);

    canvas.drawRect(rect, circlePaint);

    //canvas.drawCircle(0, 0, this.width, circlePaint);
}

我遇到的问题是画布没有显示

我该如何解决?

2 个答案:

答案 0 :(得分:0)

我通过添加setWillNotDraw(false);

找到了答案

答案 1 :(得分:0)

它在我的代码下面工作。你也可以试试。

1)添加布局

   <com.example.canvas.DrawingArea
        android:id="@+id/drawing"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
       android:layout_marginBottom="3dp"
        android:layout_weight="1"
       android:background="#FFFFFFFF" />

2)代码

    public class DrawingArea extends View {

    private Path drawPath;
    private Paint drawPaint, canvasPaint;
    private int paintColor = 0xFF660000;
    private Canvas drawCanvas;
    private Bitmap canvasBitmap;

    private boolean erase = false;

    public DrawingArea(Context context) {
        super(context);
        setupDrawing();
    }

    public DrawingArea(Context context, AttributeSet attrs) {
        super(context, attrs);
        setupDrawing();
    }

    public void setupDrawing() {
        drawPath = new Path();
        drawPaint = new Paint();
        drawPaint.setAntiAlias(true);
        drawPaint.setStrokeWidth(20);
        drawPaint.setStyle(Paint.Style.STROKE);
        drawPaint.setStrokeJoin(Paint.Join.ROUND);
        drawPaint.setStrokeCap(Paint.Cap.ROUND);

        canvasPaint = new Paint(Paint.DITHER_FLAG);
    }

    public void setColor(String newColor) {
        invalidate();
        paintColor = Color.parseColor(newColor);
        drawPaint.setColor(paintColor);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawBitmap(canvasBitmap, 0, 0, canvasPaint);
        canvas.drawPath(drawPath, drawPaint);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        canvasBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
        drawCanvas = new Canvas(canvasBitmap);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        float eventX = event.getX();
        float eventY = event.getY();

        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                drawPath.moveTo(eventX, eventY);
                break;
            case MotionEvent.ACTION_MOVE:
                drawPath.lineTo(eventX, eventY);
                break;
            case MotionEvent.ACTION_UP:
                drawCanvas.drawPath(drawPath, drawPaint);
                drawPath.reset();
                break;
            default:
                return false;
        }

        // Makes our view repaint and call onDraw
        invalidate();
        return true;
    }

    public void setErase() {
        canvasBitmap.eraseColor(Color.TRANSPARENT);
        drawPath.reset();
        invalidate();
    }

    public void startNew() {
        drawCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
        invalidate();
    }
}

3)创建绘图对象

DrawingArea mDrawView = (DrawingArea) findViewById(R.id.drawing);