Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
280 views
in Technique[技术] by (71.8m points)

android - onDraw method is not drawing

i am working on application in which my ondraw method is not working properly. I checked similar questions but i didnt get the what is the problem. This code drawing a square but not the correct size. It's so small. Also ? write a code for cells but they are not even working. I didnt get the where is the problem.

My class Code:

public SudokuBoard(Context context, @Nullable AttributeSet attrs) {
    super(context, attrs);
    setWillNotDraw(false);
    setWillNotCacheDrawing(false);

    TypedArray a = context.getTheme().obtainStyledAttributes(attrs,R.styleable.SudokuBoard,0,0);

    try{
        boardColor = a.getInteger(R.styleable.SudokuBoard_boardColor,0);
    }
    finally {
        a.recycle();
    }
}


@Override
protected void onMeasure(int width, int height){
    super.onMeasure(getWidth(),getHeight());

    int dimension = Math.min(this.getMeasuredWidth(),this.getMeasuredHeight());
    cellSize = dimension/9;
    setMeasuredDimension(dimension,dimension);
}
@Override
protected void onDraw(Canvas canvas){

    boardColorPaint.setStyle(Paint.Style.STROKE);
    boardColorPaint.setStrokeWidth(16);
    boardColorPaint.setColor(boardColor);
    boardColorPaint.setAntiAlias(true);
    canvas.drawRect(0,0,getWidth(),getHeight(),boardColorPaint);
    drawBoard(canvas);

}
private void drawThinLine(){
    boardColorPaint.setStyle(Paint.Style.STROKE);
    boardColorPaint.setStrokeWidth(10);
    boardColorPaint.setColor(boardColor);

}
private void drawThickLine(){
    boardColorPaint.setStyle(Paint.Style.STROKE);
    boardColorPaint.setStrokeWidth(4);
    boardColorPaint.setColor(boardColor);
}

private void drawBoard(Canvas canvas){
    for(int c = 0;c<10;c++){
        if(c%3==0){
            drawThickLine();
        }
        else{
            drawThinLine();
        }
        canvas.drawLine(cellSize*c,0,cellSize*c,getWidth(),boardColorPaint);

    }
    for(int r = 0;r<10;r++){
        if(r%3==0){
            drawThickLine();
        }
        else{
            drawThinLine();
        }
        canvas.drawLine(0,cellSize*r,getWidth(),cellSize*r,boardColorPaint);

    }

}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You cannot call getMeasuredWidth() or getMeasuredHeight() from within onMeasure(), those values aren't determined until setMeasuredDimension() is called.

Also you shouldn't call super.onMeasure(), your job in onMeasure() is to calculate the dimensions and call setMeasuredDimension().

Assuming your SodukoBoard is set as layout_width="match_parent" and layout_height="match_parent", this code should set your SodukoBoard width to a square using the lesser of the two dimensions:

void onMeasure (int widthMeasureSpec, int heightMeasureSpec)
{
 int dimension = Math.min(MeasureSpec.getSize(widthMeasureSpec), MeasureSpec.getSize(heightMeasureSpec));
 setMeasuredDimension (dimension, dimension);
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...