连连看的游戏界面十分简单,大致可以分为两个区域:
——游戏主界面区
——控制按钮和数据显示区
<强> 1,开发界面布局强>
本程序使用一个RelativeLayout作为整体的界面布局元素,界面布局上面是一个自定义组件,下面是一个水平排列的LinearLayout。
下面是本程序的布局文件:/res/layout/main。xml
& lt; & # 63; xml version=" 1.0 " encoding=" utf - 8 " & # 63;比; & lt; RelativeLayout xmlns: android=" http://schemas.android.com/apk/res/android " android: layout_width="宽和" android: layout_height=翱砗汀? android:背景=癅drawable/房间”比; & lt; !——游戏主界面的自定义组件——比; & lt; cn.oyp.link.view.GameView android: id=癅 + id/gameView”android: layout_width=翱砗汀? android: layout_height="宽和"/比; & lt; !——水平排列的LinearLayout——比; & lt; LinearLayout android: layout_width="宽和" 面向android: layout_height="宽和" android:=八健? android: layout_marginTop=" 380 px " android:背景=? 1 e72bb” android:重力="中心"的在 & lt; !——控制游戏开始的按钮,该按钮的背景图片可以根据按钮的状态改变——比; & lt;按钮android: id=癅 + id/startButton”android: layout_width=皐rap_content” android: layout_height=" wrap_content " android:背景=癅drawable/button_selector”/比; & lt; !——显示游戏剩余时间的文本框——比; & lt; TextView android: id=癅 + id/timeText”android: layout_width=皐rap_content” android: layout_height=" wrap_content " android:重力=爸行摹? android: textSize=" 20下降" android:宽度=?50 px”android:输入textColor=" # ff9 "/比; & lt;/LinearLayout> & lt;/RelativeLayout> >之前其中指定按钮背景色使用了@drawable/button_selector,这是在res/可拉的目录下配置的StateListDrawable对象,配置文件代码如下:res/可拉的/button_selector。xml
& lt; & # 63; xml version=" 1.0 " encoding=" utf - 8 " & # 63;比; & lt;选择xmlns: android=" http://schemas.android.com/apk/res/android "比; & lt; !——指定按钮按下时的图片——比; & lt;项目android: state_pressed=" true " android:可拉的=癅drawable/start_down”/比; & lt; !——指定按钮松开时的图片——比; & lt;项目android: state_pressed=" false " android:可拉的=癅drawable/开始”/比; & lt;/selector> >之前<强> 2,开发游戏界面组件
强>本游戏采用了一个自定义观点:GameView,它从视图的基类派生出来,这个自定义视图的功能就是根据游戏状态来描绘游戏界面上的全部方块。
为了开发这个GameView,本程序还提供了一个类,一个块对象代表游戏界面中的一个方块,它除了封装了方块上的图片之外,还需要封装该方块代表二维数组中的那个元素,也需要封装它的左上角在游戏界面中的X, Y坐标,这X, Y坐标可以决定方块的绘制位置,GameView根据这两个坐标值绘制全部方块即可。
下面是块类的代码:cn \视图\ \ oyp \联系。java
包cn.oyp.link.view; 进口android.graphics.Point;/* * *连连看游戏中的方块对象& lt; br/比; * & lt; br/比; *关于本代码介绍可以参考一下博客:欧阳鹏的CSDN博客& lt;/a>& lt; br/比; */公共类块{/* * *保存方块对象的所对应的图片 */私人PieceImage PieceImage;/* * *该方块的左上角的x坐标 */私人int beginX;/* * *该方块的左上角的y座标 */私人int beginY;/* * *该对象在块[][]数组中第一维的索引值 */私人int indexX;/* * *该对象在块[][]数组中第二维的索引值 */私人int indexY;/* * *设置该块对象在数组中的索引值 * * @param indexX *该方块的左上角的x坐标 * @param indexY *该方块的左上角的y座标 */公共部分(int indexX int indexY) { 这一点。indexX=indexX; 这一点。indexY=indexY; }/* * *获取该块的中心位置 * * @return中心点的坐标对象 */公共点getCenter () { 返回新点(getBeginX () + getPieceImage () .getImage () .getWidth ()/2,getBeginY () + getPieceImage () .getImage () .getHeight ()/2); }/* * *判断两个块上的图片是否相同 * * @param otherPieceImage *另外的一个块对象 * @return是否相同 */公共布尔isSameImage(块otherPieceImage) { 如果(pieceImage==null) { 如果(otherPieceImage。pieceImage !=null) 返回错误; }//当两个块封装图片资源ID相同时,即可认为这两个块上的图片相同。==otherPieceImage.pieceImage返回pieceImage.getImageId () .getImageId (); }/* * * @return该方块的左上角的X坐标 */公共int getBeginX () { 返回beginX; }/* * *设置该方块的左上角的X坐标 * * @param beginX */公共空间setBeginX (int beginX) { 这一点。beginX=beginX; }/* * * @return该方块的左上角的Y座标 */公共int getBeginY () { 返回beginY; }/* * *设置该方块的左上角的Y坐标 * * @param beginY */公共空间setBeginY (int beginY) { 这一点。beginY=beginY; }/* * * @return该对象在块[][]数组中第一维的索引值 */公共int getIndexX () { 返回indexX; }/* * *设置该对象在块[][]数组中第一维的索引值 * * @param indexX */公共空间setIndexX (int indexX) { 这一点。indexX=indexX; }/* * * @return该对象在块[][]数组中第二维的索引值 */公共int getIndexY () { 返回indexY; }/* * *设置该对象在块[][]数组中第二维的索引值 * * @param indexY */公共空间setIndexY (int indexY) { 这一点。indexY=indexY; }/* * * @return保存方块对象的所对应的图片 */公共PieceImage getPieceImage () { 返回pieceImage; }/* * *设置保存方块对象的所对应的图片 * * @param pieceImage */公共空间setPieceImage (PieceImage PieceImage) { 这一点。pieceImage=pieceImage; } }Android实现疯狂连连看游戏之开发游戏界面(二)