我正在尝试使用drawLine()绘制数独网格的线条但是每条线条逐渐变得更暗淡,直到它们在屏幕的一半处不可见。 screenshot of emulator
canvas.drawLine(0, (i + 1) * tileHeight,getWidth(), (i + 1) * tileHeight, gridPaint ); //horizontal lines
我知道他们在那里是因为在旋转屏幕的短暂时间内,线条会按预期显示,但随后它们会再次消失。
我已经尝试了各种方法来解决这个问题,但我仍然不知道是什么造成了这个
private static final String TAG = "Soduku";
private final Game game; //game logic
private float tileWidth;
private float tileHeight;
private int selX; //x index of selection
private int selY; //y index of selection
private Rect selRect = new Rect();
private Paint gridPaint = new Paint();
public PuzzleView(Context context) {
super(context);
this.game = (Game)context;
setFocusable(true);
setFocusableInTouchMode(true);
gridPaint.setColor(getResources().getColor(R.color.black));
}
@Override
protected void onSizeChanged(int w, int h, int oldW, int oldH) {
tileWidth = w / 9f;
tileHeight = h / 9f;
getRect(selX, selY, selRect);
Log.d(TAG, "onSizeChaned: width " + tileWidth + ", height " + tileHeight);
super.onSizeChanged(w, h, oldW, oldH);
}
private void getRect(int x, int y, Rect rect) {
rect.set((int) (x * tileWidth), (int) (y * tileHeight), (int) (x * Math.pow((double) tileWidth, 2)),
(int) (y * Math.pow((double) tileHeight, 2)));
}
@Override
protected void onDraw(Canvas canvas) {
invalidate();
Log.d("onDraw", "tileHeight - " + ((Float) tileHeight).toString());
for(int i = 0; i < 9; i++) {
canvas.drawLine(0, (i + 1) * tileHeight,getWidth(), (i + 1) * tileHeight, gridPaint ); //horizontal lines
canvas.drawLine((i + 1) * tileWidth, 0, (i + 1) * tileWidth, getHeight(), gridPaint); //vertical lines
}
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
Log.d(TAG, "onKeyDown: keyCode " + keyCode + ", keyEvent " + event);
switch(keyCode) {
case KeyEvent.KEYCODE_DPAD_UP:
select(selX, selY - 1);
break;
case KeyEvent.KEYCODE_DPAD_DOWN:
select(selX, selY + 1);
break;
case KeyEvent.KEYCODE_DPAD_LEFT:
select(selX - 1, selY);
break;
case KeyEvent.KEYCODE_DPAD_RIGHT:
select(selX + 1, selY);
break;
case KeyEvent.KEYCODE_0:
case KeyEvent.KEYCODE_SPACE: setSelectedTile(0); break;
case KeyEvent.KEYCODE_1: setSelectedTile(1); break;
case KeyEvent.KEYCODE_2: setSelectedTile(2); break;
case KeyEvent.KEYCODE_3: setSelectedTile(3); break;
case KeyEvent.KEYCODE_4: setSelectedTile(4); break;
case KeyEvent.KEYCODE_5: setSelectedTile(5); break;
case KeyEvent.KEYCODE_6: setSelectedTile(6); break;
case KeyEvent.KEYCODE_7: setSelectedTile(7); break;
case KeyEvent.KEYCODE_8: setSelectedTile(8); break;
case KeyEvent.KEYCODE_9: setSelectedTile(9); break;
case KeyEvent.KEYCODE_ENTER:
case KeyEvent.KEYCODE_DPAD_CENTER:
game.showKeypadOrError(selX, selY);
break;
default:
return super.onKeyDown(keyCode, event);
}
return true;
}
//locate the selected square
private void select(int x, int y) {
invalidate(selRect);
selX = Math.min(Math.max(x,0), 8);
selY = Math.min(Math.max(y,0), 8);
getRect(selX, selY, selRect);
invalidate(selRect);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if(event.getAction() != MotionEvent.ACTION_DOWN) {
return super.onTouchEvent(event);
}
select((int)(event.getX() / tileWidth), (int)(event.getY() / tileHeight));
game.showKeypadOrError(selX, selY);
Log.d(TAG, "onTouchEvent: x" + selX + ", y" + selY);
return true;
}
public void setSelectedTile(int tile) {
if(game.setTileIfValid(selX, selY, tile)) {
invalidate();
} else {
//number not valid for selected tile
Log.d(TAG, "setSelectedTile: invalid - " + tile);
startAnimation(AnimationUtils.loadAnimation(game, R.anim.shake));
}
}