2016-08-05 82 views
0

我有以下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); 
} 

我遇到的問題是,在畫布上顯示不出來

我該如何解決這個問題?

回答

0

我加入setWillNotDraw(false);

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)創建繪圖對象

繪圖區mDrawView =(繪圖區)findViewById(R.id 。畫畫);