自定义视图画布

时间:2016-05-24 18:21:03

标签: java android layout

我在自定义视图画布上遇到了这个问题:

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 - 当我用上面的代码绘制圆圈时,圆圈有0.3-0.4 alpha。为什么呢?

1 个答案:

答案 0 :(得分: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-对此进行控制的是用于绘制圆圈的 paint 。我无法通过这段代码看到发生了什么,但你应该开始寻找可能给你的油漆带来alpha的东西。