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);
}
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…