Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1490183
  • 博文数量: 465
  • 博客积分: 8915
  • 博客等级: 中将
  • 技术积分: 6365
  • 用 户 组: 普通用户
  • 注册时间: 2010-07-30 15:05
文章分类

全部博文(465)

文章存档

2017年(33)

2016年(2)

2015年(4)

2014年(29)

2013年(71)

2012年(148)

2011年(178)

分类: 嵌入式

2011-07-15 16:41:00

 

GameView主要就是根据游戏的状态数据来绘制界面上的方块,GameView继承了View组件,重写了View组件上onDraw(Canvas canvas)方法,重写该方法主要就是绘制游戏里剩余的方块;除此之外,它还会负责绘制连接方块的连接线。

GamaView的代码如下。

程序清单:codes\18\Link\src\org\crazyit\link\view\GameView.java

public class GameView extends View

{

     // 游戏逻辑的实现类

     private GameService gameService;          //

     // 保存当前已经被选中的方块

     private Piece selectedPiece;

     // 连接信息对象

     private LinkInfo linkInfo;

     private Paint paint;

     // 选中标识的图片对象

     private Bitmap selectImage;

     public GameView(Context context, AttributeSet attrs)

     {

          super(context, attrs);

          this.paint = new Paint();

          // 设置连接线的颜色

          this.paint.setColor(Color.RED);

          // 设置连接线的粗细

          this.paint.setStrokeWidth(3);

          this.selectImage = ImageUtil.getSelectImage(context);

     }

     public void setLinkInfo(LinkInfo linkInfo)

     {

          this.linkInfo = linkInfo;

     }

     public void setGameService(GameService gameService)

     {

          this.gameService = gameService;

     }

     @Override

     protected void onDraw(Canvas canvas)

     {

          super.onDraw(canvas);

          if (this.gameService == null)

               return;

          Piece[][] pieces = gameService.getPieces();            //

          if (pieces != null)

          {

               // 遍历pieces二维数组

               for (int i = 0; i < pieces.length; i++)

               {

                    for (int j = 0; j < pieces[i].length; j++)

                    {

                          // 如果二维数组中该元素不为空(即有方块),将这个方块的图片画出来

                          if (pieces[i][j] != null)

                          {

                               // 得到这个Piece对象

                               Piece piece = pieces[i][j];

                               // 根据方块左上角XY坐标绘制方块

                               canvas.drawBitmap(piece.getImage().getImage(),

                                 piece.getBeginX(), piece.getBeginY(), null);

                          }

                    }

               }

          }

          // 如果当前对象中有linkInfo对象, 即连接信息

          if (this.linkInfo != null)

          {

               // 绘制连接线

               drawLine(this.linkInfo, canvas);

               // 处理完后清空linkInfo对象

               this.linkInfo = null;

          }

          // 画选中标识的图片

          if (this.selectedPiece != null)

          {

               canvas.drawBitmap(this.selectImage, this.selectedPiece.
                 
getBeginX(),

                    this.selectedPiece.getBeginY(), null);

          }

     }

     // 根据LinkInfo绘制连接线的方法

     private void drawLine(LinkInfo linkInfo, Canvas canvas)

     {

          // 获取LinkInfo中封装的所有连接点

          List points = linkInfo.getLinkPoints();

          // 依次遍历linkInfo中的每个连接点

          for (int i = 0; i < points.size() - 1; i++)

          {

               // 获取当前连接点与下一个连接点

               Point currentPoint = points.get(i);

               Point nextPoint = points.get(i + 1);

               // 绘制连线

               canvas.drawLine(currentPoint.x, currentPoint.y,

                    nextPoint.x, nextPoint.y, this.paint);

          }

     }

     // 设置当前选中方块的方法

     public void setSelectedPiece(Piece piece)

     {

          this.selectedPiece = piece;

     }

     // 开始游戏方法

     public void startGame()

     {

          this.gameService.start();

          this.postInvalidate();

     }

}

上面的GameView中第一段粗体字代码用于根据游戏的状态数据来绘制界面中的所有方块,第二段粗体字代码则用于根据LinkInfo来绘制两个方块之间的连接线。

上面的程序中①号代码处定义了GameService对象,②号代码则调用了GameServicegetPieces()方法来获取游戏中剩余的方块,GameService是游戏的业务逻辑实现类。后面会详细介绍该类的实现,此处暂不讲解。

 

本文节选自《疯狂Android讲义(CD光盘1)》一书。

图书详细信息:http://blog.chinaunix.net/space.php?uid=13164110&do=blog&id=1704751

阅读(430) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~