如何在Android中的tic tac toe游戏中绘制位图图像而不是绘制线?

时间:2011-12-27 10:47:52

标签: android bitmap draw

我已经实现了tic tac toe游戏,因为它来自api samples.from tic tac toe game我想画一个位图而不是行。我在onTouchEvent上编写了如下的代码:

 @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中插入了一些代码。

1 个答案:

答案 0 :(得分:1)

我不知道你为什么要在onTouchEvent方法中绘制线条。在on 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`