2011-12-27 39 views
0

我已經實現井字遊戲,因爲它是從API samples.from是井字遊戲,我想提請位圖,而不是lines.I已經寫的代碼和平作爲的onTouchEvent如下:如何繪製位圖圖像而不是在android的tic tac toe遊戲中畫線?

@Override 
    public boolean onTouchEvent(MotionEvent event) { 

    if (event.getAction() == MotionEvent.ACTION_DOWN) 
    { 
    RectF rct=_logic.getPositionToFill(event.getX(), event.getY()); 

    if(rct!=null) 
    { 
    if(_drawX) 
    { 

     _bitmap=BitmapFactory.decodeResource(getResources(), R.drawable.smile); 

     _canvas.drawBitmap(_bitmap, 0.0f, 0.0f, _paint); 

     /* _canvas.drawLine(rct.left, rct.top, 
       rct.right, rct.bottom, _paint); 


     _canvas.drawLine(rct.right, rct.top, 
       rct.left, rct.bottom, _paint);*/ 

    } 
    else 
    { 

      _bitmap=BitmapFactory.decodeResource(getResources(), R.drawable.sad); 

     _canvas.drawBitmap(_bitmap, 0.0f, 0.0f, _paint); 

    //   _canvas.drawOval(rct, _paint); 

    } 
    _drawX=!_drawX; 

    invalidate(); 

    } 
    } 
    return true; 
    } 

我在if塊中插入了一些代碼而不是drawLine。

+0

你能粘貼你收到的錯誤嗎? – MikeIsrael 2011-12-27 11:00:09

+0

有沒有錯誤,但如果我點擊一個單元格,整個單元格消失並獲得圖像。 – 2011-12-27 11:16:53

回答

1

我不知道你爲什麼在onTouchEvent方法中畫線。在onTouchEvent方法上,您只需要取得觸摸的座標並將圖片繪製到onDraw方法中即可。

bitampCross = BitmapFactory.decodeResource(getResources(),R.drawable.cross); 
     protected void onDraw(Canvas canvas) { 
    Paint background = new Paint(); 
    background.setColor(Color.WHITE); 
    canvas.drawRect(0, 0, getWidth(), getHeight(), background); 
    canvas.drawBitmap(backgroundGrid, 10, 0, null); 

       if (pos == 1) { 
         canvas.drawBitmap(bitampCross, bitampCross.getWidth() + 20, 
          bitampCross.getHeight() + 15, null); 
          } 
       if (pos == 2) { 
         canvas.drawBitmap(bitmapCircle, bitmapCircle.getWidth() + 20, 
             bitmapCircle.getHeight() + 15, null); 
       } 
        }//end of onDraw 
---------- 

    public boolean onTouchEvent(MotionEvent event) { 
    int action = event.getAction();   
    if (action == MotionEvent.ACTION_DOWN) { 
     return true;} 
     }else if(action == MotionEvent.ACTION_UP){ 
     //Do your checks if want to draw the image. 
     //For example your board can not take the whole screen 
     //return true or false in this block 
    //For example: 
     int x = (int) event.getX(); 
     int y = (int) event.getY(); 

    if(x<200 && y<200) 
     return true; 
    else 
    return false; 
} 
invalidate(); 
return false; 
}//end of onTouchEvente`