请问各位,为什么我的代码不显示棋子呢?
package com.duke.wuziqi;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Point;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Toast;
import java.util.ArrayList;
public class mainPanel extends View {
private int mPanelWidth;
private float mLineHeight;
private int MAX_LINE = 10;
private Paint mPaint = new Paint();// 绘制直线所使用的画笔。
private Bitmap mWhitePiece;
private Bitmap mBlackPiece;
private float ratioPieceOfLineheight = 3 * 1.0f / 4;
private boolean mIsWhite = true;// 当前是轮到白棋
private ArrayList<Point> mWhiteArray = new ArrayList<>();
private ArrayList<Point> mBlackArray = new ArrayList<>();
private boolean mIsGameOver = false;
private boolean mIsWhiteWinner;// 判断是否白方胜出
public mainPanel(Context context, AttributeSet attrs) {
super(context, attrs);
setBackgroundColor(0x44ff0000);
init();
}
/**
* <PRE>
* 初始化mPaint画笔 、棋子
* </PRE>
*/
private void init() {
mPaint.setColor(0x88000000);
mPaint.setAntiAlias(true);// 图像边缘清晰一点,锯齿边缘不那么明显
mPaint.setDither(true);// 设置防抖动,使图像柔和点
mPaint.setStyle(Paint.Style.STROKE);// STROKE划线;FILL填充
mWhitePiece = BitmapFactory.decodeResource(getResources(), R.drawable.stone_w2);
mBlackPiece = BitmapFactory.decodeResource(getResources(), R.drawable.stone_b1);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int widthSize = MeasureSpec.getSize(widthMeasureSpec);
int widthMode = MeasureSpec.getMode(widthMeasureSpec);
int heightSize = MeasureSpec.getSize(heightMeasureSpec);
int heightMode = MeasureSpec.getMode(heightMeasureSpec);
int width = Math.min(heightSize, widthSize);
// 避免TextView处于ScrollView中而heightSize=0;
if (widthMode == MeasureSpec.UNSPECIFIED) {
width = heightSize;
} else if (heightMode == MeasureSpec.UNSPECIFIED) {
width = widthSize;
}
setMeasuredDimension(width, width);
}
/**
* <PRE>
* 宽高改变的函数
* </PRE>
*/
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
mPanelWidth = w;
mLineHeight = mPanelWidth * 1.0f / MAX_LINE;
int pieceWidth = (int) (mLineHeight * ratioPieceOfLineheight);
mWhitePiece = Bitmap.createScaledBitmap(mWhitePiece, pieceWidth, pieceWidth, false);
mBlackPiece = Bitmap.createScaledBitmap(mBlackPiece, pieceWidth, pieceWidth, false);
// 根据原来的位图创建一个新的位图;把棋子尺寸设置为行高的3/4
}
/**
* <PRE>
* 绘制函数
* </PRE>
*/
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
drawBoard(canvas);
drawPieces(canvas);
checkIsGameOver();
}
private void checkIsGameOver() {
// 理论上此处应该换成一个接口,使外部可以回调该函数
boolean whiteWin = checkIsFiveInLine(mWhiteArray);
boolean blackWin = checkIsFiveInLine(mBlackArray);
if (whiteWin || blackWin) {
mIsGameOver = true;
mIsWhiteWinner = whiteWin;
String text = mIsWhiteWinner ? "白棋胜利" : "黑棋胜利";
Toast.makeText(getContext(), text, Toast.LENGTH_SHORT).show();
}
}
private int MAX_COUNT_IN_LINE = 5;
private boolean checkIsFiveInLine(ArrayList<Point> points) {
boolean result = false;
for (Point p : points) {
int x = p.x;
int y = p.y;
// 获得棋子在棋盘上的位置
boolean win = checkHorizontal(x, y, points);
if (win)
return result;
win = checkVertical(x, y, points);
if (win)
return result;
win = checkLeftDiagonal(x, y, points);
if (win)
return result;
win = checkRightDiagonal(x, y, points);
if (win)
return result;
}
return result;
}
private boolean checkHorizontal(int x, int y, ArrayList<Point> points) {
int count = 1;
for (int i = 1; i < MAX_COUNT_IN_LINE; i++) {
if (points.contains(new Point(x - i, y))) {
count++;
} else {
break;
}
}
if (count == MAX_COUNT_IN_LINE)
return true;
for (int i = 1; i < MAX_COUNT_IN_LINE; i++) {
if (points.contains(new Point(x + i, y))) {
count++;
} else {
break;
}
}
if (count == MAX_COUNT_IN_LINE)
return true;
return false;
}
private boolean checkVertical(int x, int y, ArrayList<Point> points) {
int count = 1;
for (int i = 1; i < MAX_COUNT_IN_LINE; i++) {
if (points.contains(new Point(x, y - i))) {
count++;
} else {
break;
}
}
if (count == MAX_COUNT_IN_LINE)
return true;
for (int i = 1; i < MAX_COUNT_IN_LINE; i++) {
if (points.contains(new Point(x, y + i))) {
count++;
} else {
break;
}
}
if (count == MAX_COUNT_IN_LINE)
return true;
return false;
}
private boolean checkLeftDiagonal(int x, int y, ArrayList<Point> points) {
int count = 1;
for (int i = 1; i < MAX_COUNT_IN_LINE; i++) {
if (points.contains(new Point(x - i, y + i))) {
count++;
} else {
break;
}
}
if (count == MAX_COUNT_IN_LINE)
return true;
for (int i = 1; i < MAX_COUNT_IN_LINE; i++) {
if (points.contains(new Point(x + i, y - i))) {
count++;
} else {
break;
}
}
if (count == MAX_COUNT_IN_LINE)
return true;
return false;
}
private boolean checkRightDiagonal(int x, int y, ArrayList<Point> points) {
int count = 1;
for (int i = 1; i < MAX_COUNT_IN_LINE; i++) {
if (points.contains(new Point(x - i, y - i))) {
count++;
} else {
break;
}
}
if (count == MAX_COUNT_IN_LINE)
return true;
for (int i = 1; i < MAX_COUNT_IN_LINE; i++) {
if (points.contains(new Point(x + i, y + i))) {
count++;
} else {
break;
}
}
if (count == MAX_COUNT_IN_LINE)
return true;
return false;
}
/**
* <PRE>
* 绘制棋子
* </PRE>
*/
private void drawPieces(Canvas canvas) {
for (int i = 0, n = mWhiteArray.size(); i < n; i++) {
Point whitePoint = mWhiteArray.get(i);
canvas.drawBitmap(mWhitePiece, (whitePoint.x + (1 - ratioPieceOfLineheight) / 2) * mLineHeight,
(whitePoint.y + ratioPieceOfLineheight / 2) * mLineHeight, null);
// drawBitmap(Bitmap bitmap, float left, float top, Paint paint)
// Bitmap:图片对象,left:偏移左边的位置,top: 偏移顶部的位置
}
for (int i = 0, n = mBlackArray.size(); i < n; i++) {
Point blackPoint = mBlackArray.get(i);
canvas.drawBitmap(mBlackPiece, (blackPoint.x + (1 - ratioPieceOfLineheight) / 2) * mLineHeight,
(blackPoint.y + ratioPieceOfLineheight / 2) * mLineHeight, null);
// drawBitmap(Bitmap bitmap, float left, float top, Paint paint)
// Bitmap:图片对象,left:偏移左边的位置,top: 偏移顶部的位置
}
}
/**
* <PRE>
* 绘制棋盘
* </PRE>
*/
private void drawBoard(Canvas canvas) {
int w = mPanelWidth;
float lineHeight = mLineHeight;
for (int i = 0; i < MAX_LINE; i++) {
int startX = (int) (lineHeight / 2);
int endX = (int) (w - lineHeight / 2);
int y = (int) ((0.5 + i) * lineHeight);
canvas.drawLine(startX, y, endX, y, mPaint);// 绘制横线
}
for (int i = 0; i < MAX_LINE; i++) {
int startY = (int) (lineHeight / 2);
int endY = (int) (w - lineHeight / 2);
int x = (int) ((0.5 + i) * lineHeight);
canvas.drawLine(x, startY, x, endY, mPaint);// 绘制纵线
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (mIsGameOver)
return false;// 如果游戏结束,则不允许该子view处理以下动作
int action = event.getAction();
if (action == MotionEvent.ACTION_UP) {// 当出现...动作时将该事件交给子view处理
int x = (int) event.getX();
int y = (int) event.getY();
Point p = getValidPoint(x, y);
if (mWhiteArray.contains(p) || mBlackArray.contains(p)) {
return false;
}
if (mIsWhite) {
mWhiteArray.add(p);
} else {
mBlackArray.add(p);
}
invalidate();// 请求重绘
mIsWhite = !mIsWhite;
return true;
}
return super.onTouchEvent(event);
}
private Point getValidPoint(int x, int y) {
return new Point((int) (x / mLineHeight), (int) (y / mLineHeight));
}
}