2015-09-17 92 views

回答

0

這個應用程序的開發很容易。您需要了解:

如何獲取點擊的座標。

@Override 
public boolean onTouchEvent(MotionEvent event) { 
    int x = (int)(event.getX()/tileSize); 
    int y = (int)(event.getY()/tileSize); 
    switch (event.getAction()) { 
     case MotionEvent.ACTION_DOWN: 
     map[x][y] = true; 
     break; 
     case MotionEvent.ACTION_MOVE: 
     case MotionEvent.ACTION_UP: 
    } 
return false; 
} 

重寫方法onDraw,繪製矩形。

private void init(){ 
    tileSize = 10; 

    paint1 = new Paint(); 
    paint1.setColor(Color.BLUE); 
    paint1.setStrokeWidth(10); 
    paint1.setStyle(Paint.Style.STROKE); 

    paint2 = new Paint(); 
    paint2.setColor(Color.RED); 
    paint2.setStrokeWidth(10); 
    paint2.setStyle(Paint.Style.STROKE); 

} 

@Override 
protected void onDraw(Canvas canvas) { 
    super.onDraw(canvas); 
for (int i = 0; i < x; i++){ 
    for (int j = 0; j < y; j++){ 
     Paint p = null; 
     if(map[i][j]){ 
     p=paint1; 
     }else{ 
     p=paint2; 
     } 
     canvas.drawRect(i*tileSize, j*tileSize, tileSize, tileSize, paint); 
    } 

} 
} 
} 
相關問題