最后一个参数,有的是null,有的是new Paint出来的。这是为何? paint = new Paint();//初始化画笔 public void onDraw(Canvas canvas){//自己写的绘制方法 canvas.drawColor(Color.WHITE); canvas.drawBitmap(background, 0,0, null);//清背景 canvas.drawBitmap(qiPan, 10, 10, null);//绘制棋盘 for(int i=0; i<qizi.length; i++){ for(int j=0; j<qizi[i].length; j++){//绘制棋子 if(qizi[i][j] != 0){ canvas.drawBitmap(qizibackground, 9+j*34, 10+i*35, null);//绘制棋子的背景 if(qizi[i][j] == 1){//为黑帅时 canvas.drawBitmap(heiZi[0], 12+j*34, 13+i*35, paint); } else if(qizi[i][j] == 2){//为黑车时 canvas.drawBitmap(heiZi[1], 12+j*34, 13+i*35, paint); } else if(qizi[i][j] == 3){//为黑马时 canvas.drawBitmap(heiZi[2], 12+j*34, 13+i*35, paint); } else if(qizi[i][j] == 4){//为黑炮时 canvas.drawBitmap(heiZi[3], 12+j*34, 13+i*35, paint); } else if(qizi[i][j] == 5){//为黑士时 canvas.drawBitmap(heiZi[4], 12+j*34, 13+i*35, paint); } else if(qizi[i][j] == 6){//为黑象时 canvas.drawBitmap(heiZi[5], 12+j*34, 13+i*35, paint); } else if(qizi[i][j] == 7){//为黑兵时 canvas.drawBitmap(heiZi[6], 12+j*34, 13+i*35, paint); } else if(qizi[i][j] == 8){//为红将时 canvas.drawBitmap(hongZi[0], 12+j*34, 13+i*35, paint); } else if(qizi[i][j] == 9){//为红车时 canvas.drawBitmap(hongZi[1], 12+j*34, 13+i*35, paint); } else if(qizi[i][j] == 10){//为红马时 canvas.drawBitmap(hongZi[2], 12+j*34, 13+i*35, paint); } else if(qizi[i][j] == 11){//为红砲时 canvas.drawBitmap(hongZi[3], 12+j*34, 13+i*35, paint); } else if(qizi[i][j] == 12){//为红仕时 canvas.drawBitmap(hongZi[4], 12+j*34, 13+i*35, paint); } else if(qizi[i][j] == 13){//为红相时 canvas.drawBitmap(hongZi[5], 12+j*34, 13+i*35, paint); } else if(qizi[i][j] == 14){//为红卒时 canvas.drawBitmap(hongZi[6], 12+j*34, 13+i*35, paint); } } } } canvas.drawBitmap(vs, 10, 360, paint);//绘制VS背景图 //绘制黑方的时间 canvas.drawBitmap(time, 81, 411, paint);//绘制冒号 int temp = this.heiTime/60;//换算时间 String timeStr = temp+"";//转换成字符串 if(timeStr.length()<2){//当不足两位时前面填0 timeStr = "0" + timeStr; } for(int i=0;i<2;i++){//循环绘制时间 int tempScore=timeStr.charAt(i)-""0""; canvas.drawBitmap(number[tempScore], 65+i*7, 412, paint); } //画分钟 temp = this.heiTime%60; timeStr = temp+"";//转换成字符串 if(timeStr.length()<2){ timeStr = "0" + timeStr;//当长度小于2时在前面添加一个0 } for(int i=0;i<2;i++){//循环 int tempScore=timeStr.charAt(i)-""0""; canvas.drawBitmap(number[tempScore], 85+i*7, 412, paint);//绘制 } //开始绘制红方时间 canvas.drawBitmap(this.redtime, 262, 410, paint);//红方的冒号 int temp2 = this.hongTime/60;//换算时间 String timeStr2 = temp2+"";//转换成字符串 if(timeStr2.length()<2){//当不足两位时前面填0 timeStr2 = "0" + timeStr2; } for(int i=0;i<2;i++){//循环绘制时间 int tempScore=timeStr2.charAt(i)-""0""; canvas.drawBitmap(redNumber[tempScore], 247+i*7, 411, paint);//绘制 } //画分钟 temp2 = this.hongTime%60;//求出当前的秒数 timeStr2 = temp2+"";//转换成字符串 if(timeStr2.length()<2){//不足两位时前面用0补 timeStr2 = "0" + timeStr2; } for(int i=0;i<2;i++){//循环绘制 int tempScore=timeStr2.charAt(i)-""0""; canvas.drawBitmap(redNumber[tempScore], 267+i*7, 411, paint);//绘制时间数字 } if(caiPan == true){//当该玩家走棋时,即红方走棋 canvas.drawBitmap(right, 155, 420, paint);//绘制向右的指针 } else{//黑方走棋,即电脑走棋时 canvas.drawBitmap(left, 120, 420, paint);//绘制向左的指针 } canvas.drawBitmap(current, 138, 445, paint);//绘制当前文字 canvas.drawBitmap(sound2, 10, 440, paint);//绘制声音 if(activity.isSound){//如果正在播放声音 canvas.drawBitmap(sound3, 80, 452, paint);//绘制 } canvas.drawBitmap(exit2, 250, 440, paint);//绘制退出按钮 if(status == 1){//当胜利时 canvas.drawBitmap(win, 85, 150, paint);//绘制胜利图片 canvas.drawBitmap(ok, 113, 240, paint); } if(status == 2){//失败后 canvas.drawBitmap(lost, 85, 150, paint);//绘制失败界面 canvas.drawBitmap(ok, 113, 236, paint); } } |
|
另外这两个for循环是啥回事?
int[][] qizi = new int[][]{//棋盘 {2,3,6,5,1,5,6,3,2}, {0,0,0,0,0,0,0,0,0}, {0,4,0,0,0,0,0,4,0}, {7,0,7,0,7,0,7,0,7}, {0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0}, {14,0,14,0,14,0,14,0,14}, {0,11,0,0,0,0,0,11,0}, {0,0,0,0,0,0,0,0,0}, {9,10,13,12,8,12,13,10,9}, }; for(int i=0; i<qizi.length; i++){ for(int j=0; j<qizi[i].length; j++){// |
|
canvas相当于画布,第一个参数表示一个什么样的画布,你这里就是背景和棋盘的画布,当不在这块画布上画东西时,就不需要paint了,所以传null,当要在上面画棋子时,就要用到画笔paint了。
两个for循环就是画出棋盘的棋子,也就是象棋开始时的布局。 |
|
1、drawBitmap(…, Paint paint)的最后一个参数是画笔,可以通过Paint对象来设置画笔的颜色,笔锋大小等等。
2、qizi是一个二维数组,也就是数组的数组。第一层for循环拿出的是诸如:{2,3,6,5,1,5,6,3,2}这样的第一层数组,第二层for循环会拿出数组中具体的值。所以你的for循环中的,当i=0,j=1时,取出的值是:{2,3,6,5,1,5,6,3,2}中的第二个值,即3。 |
|
30分 |
弄错了,直接无视上面的。 |
Paint为null是默认使用new Paint()默认值;如果不为空,则需要自己new出来,主要设置如文字的颜色、图片透明色等
|
|
改变下面的参数后,点击棋子,棋子没反应是什么原因呀
canvas.drawBitmap(qizibackground, 9+j*34, 10+i*35, null);//绘制棋子的背景 |