2015-09-17 52 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); 
    } 

} 
} 
} 
相关问题