invalidate() doesn't refresh View

570 Views Asked by At

i'm struggling with this for a while, not sure if because almost 2 years of absence in Android DEV or my stupidity, I've tried everything and just cannot redraw my screen even if invalidate() is happening. Here's some code:

GameActivity.java

onCreate

...

final CanvasActivity mCanvasActivity = new CanvasActivity(this);
setContentView(mCanvasActivity);

mCanvasActivity.setOnTouchListener(new OnSwipeTouchListener(this) {
        @Override
        public void onSwipeTop() {

            tilesArray[playerPositionY][playerPositionX] = 0;
            playerPositionY--;
            tilesArray[playerPositionY][playerPositionX] = 2;

            mCanvasActivity.invalidate();

        }

CanvasActivity.java

protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    movePlayer(canvas);
    Log.e("player", "x " + playerPositionX + " y " + playerPositionY);
}

movePlayer

if (currentBlock == 0) {
                canvas.drawRect(posLeft, posTop, posRight, posBottom, paintWall);
            } else if (currentBlock == 1) {
                canvas.drawRect(posLeft, posTop, posRight, posBottom, paintLabirynth);
            } else if (currentBlock == 3) {
                canvas.drawRect(posLeft, posTop, posRight, posBottom, paintExit);
            } else {
                canvas.drawRect(posLeft, posTop, posRight, posBottom, paintCharacter);
            }

So basically, when We swipe to the top, position is being changed by movePlayer (decreasing y in 2d array). Then, every rectangle on screen is being redrawn (whole screen has only rectangles which are drawn with different colors according to array line by line, doesn't matter I suppose). My variables are changing properly, so invalidate() is firing onDraw(), however there's no change on the screen. Any help much appreciated.

1

There are 1 best solutions below

0
cyrial On

Looks like I didn't reset paintY to 0 every onDraw call and it was painting properly, but below the screen. Like I said, could be, and was, my stupidity :).