在Android画布上重绘位图

时间:2014-04-05 05:02:20

标签: android canvas bitmap

在我的应用中,当我触摸该图像时,我想将位图图像从一个切换到另一个。每当我触摸图像时,我都会一直收到NULLPOINTEREXCEPTION。

是否有任何方法允许从位图切换到其他位图?

@Override
protected void onDraw(Canvas canvas) {
    canvas.drawColor(Color.BLACK);
    this.cVas = canvas;
    for(int i = 0; i < BSIZE; i++) {
        for(int j = 0; j < BSIZE; j++) {
            gameBoard[i][j] = new Cell(i*wCell, j*wCell+hCell);
            if(gameBoard[i][j].getBitmap() == null) {
                gameBoard[i][j].setBitmap(b3);
            }
            /*if(gameBoard[i][j].getTouch()) {
                gameBoard[i][j].setBitmap(b4);
            }else{
                gameBoard[i][j].setBitmap(b3);
            }*/
            gameBoard[i][j].drawCell(canvas);
        }
    }
}

@Override
public boolean onTouchEvent(MotionEvent event) {
    if(event.getAction() != MotionEvent.ACTION_DOWN) {
        return super.onTouchEvent(event);
    }

    // Get the Cell's position.
    selX = (int)((event.getX()) / wCell);
    selY = (int)((event.getY() - hCell) / wCell);

    if( selX > BSIZE || selY > BSIZE || selY < 0) { 
        return false;
    }
    else {
        Log.d(TAG, "selX: " + selX + " selY: " + selY);
        cellSelection(selX, selY);
    }
    return true;    
}

// Redraw the image (switch to other bitmap).
private void cellSelection( int x, int y) {
    if(gameBoard[x][y].getTouch()) {
        gameBoard[x][y].setTouch(false);
        gameBoard[x][y].setBitmap(b3);
    } else {
        gameBoard[x][y].setTouch(true);
        gameBoard[x][y].setBitmap(b4);
    }
    gameBoard[x][y].drawCell(cVas);
    invalidate();
}

0 个答案:

没有答案