2016-05-24 167 views
1

我對自定義視圖畫布有這個問題:自定義視圖畫布

1-如何在更改後重置視圖?這是我的代碼,但沒有任何反應

@Override 
protected void onDraw(Canvas canvas) { 
    int viewWidthHalf = this.getMeasuredWidth()/2; 
    int viewHeightHalf = this.getMeasuredHeight()/2; 
    int radius; 
    if (viewWidthHalf > viewHeightHalf) 
     radius = viewHeightHalf - 10; 
    else 
     radius = viewWidthHalf - 10; 
    paint.setStyle(Paint.Style.FILL_AND_STROKE); 
    paint.setColor(backgroundColor); 
    canvas.drawCircle(viewWidthHalf, viewHeightHalf, radius, paint); 
} 

public void setbackground(int backgroundColor) { 
    this.backgroundColor = backgroundColor; 
    invalidate(); 
} 

2 - 如何獲得其他方法的畫布?

3 - 當我用drawCircle圈上面的代碼有一個0.3-0.4的alpha。爲什麼?

回答

1

1-嘗試這樣做:

 @Override 
    protected void onDraw(Canvas canvas) { 
    int viewWidthHalf = this.getMeasuredWidth()/2; 
    int viewHeightHalf = this.getMeasuredHeight()/2; 
    int radius; 
    if (viewWidthHalf > viewHeightHalf) 
     radius = viewHeightHalf - 10; 
    else 
     radius = viewWidthHalf - 10; 

    // All lines above would go better in the onCreate Method 
    paint.setStyle(Paint.Style.FILL_AND_STROKE); 
    paint.setColor(backgroundColor); 
    canvas.drawCircle(viewWidthHalf, viewHeightHalf, radius, paint); 
    invalidate(); 
} 

2 - 你可能不會需要一個

3-什麼控制,這是油漆您使用畫圓。我無法看到發生了什麼,只能通過這段代碼,但你應該開始尋找什麼可以給你的油漆alpha。

+0

invalidate();沒有改變圓的顏色和保護無效onDraw()你寫的代碼似乎不正確 –

+0

你給backgroundColor變量什麼值? –

+0

解決了謝謝:) –