請(qǐng)問(wèn)各位,為什么我的代碼不顯示棋子呢?
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();// 繪制直線所使用的畫(huà)筆。
? ?private Bitmap mWhitePiece;
? ?private Bitmap mBlackPiece;
? ?private float ratioPieceOfLineheight = 3 * 1.0f / 4;
? ?private boolean mIsWhite = true;// 當(dāng)前是輪到白棋
? ?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畫(huà)筆 、棋子
? ? * </PRE>
? ? */
? ?private void init() {
? ? ? ?mPaint.setColor(0x88000000);
? ? ? ?mPaint.setAntiAlias(true);// 圖像邊緣清晰一點(diǎn),鋸齒邊緣不那么明顯
? ? ? ?mPaint.setDither(true);// 設(shè)置防抖動(dòng),使圖像柔和點(diǎn)
? ? ? ?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>
? ? * 寬高改變的函數(shù)
? ? * </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);
? ? ? ?// 根據(jù)原來(lái)的位圖創(chuàng)建一個(gè)新的位圖;把棋子尺寸設(shè)置為行高的3/4
? ?}
? ?/**
? ? * <PRE>
? ? * 繪制函數(shù)
? ? * </PRE>
? ? */
? ?@Override
? ?protected void onDraw(Canvas canvas) {
? ? ? ?super.onDraw(canvas);
? ? ? ?drawBoard(canvas);
? ? ? ?drawPieces(canvas);
? ? ? ?checkIsGameOver();
? ?}
? ?private void checkIsGameOver() {
? ? ? ?// 理論上此處應(yīng)該換成一個(gè)接口,使外部可以回調(diào)該函數(shù)
? ? ? ?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;
? ? ? ? ? ?// 獲得棋子在棋盤(pán)上的位置
? ? ? ? ? ?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:圖片對(duì)象,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:圖片對(duì)象,left:偏移左邊的位置,top: 偏移頂部的位置
? ? ? ?}
? ?}
? ?/**
? ? * <PRE>
? ? * 繪制棋盤(pán)
? ? * </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;// 如果游戲結(jié)束,則不允許該子view處理以下動(dòng)作
? ? ? ?int action = event.getAction();
? ? ? ?if (action == MotionEvent.ACTION_UP) {// 當(dāng)出現(xiàn)...動(dòng)作時(shí)將該事件交給子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();// 請(qǐng)求重繪
? ? ? ? ? ?mIsWhite = !mIsWhite;
? ? ? ? ? ?return true;
? ? ? ?}
? ? ? ?return super.onTouchEvent(event);
? ?}
? ?private Point getValidPoint(int x, int y) {
? ? ? ?return new Point((int) (x / mLineHeight), (int) (y / mLineHeight));
? ?}
}
2017-03-22
???????return super.onTouchEvent(event);
2016-08-28
onTouchEvent的返回值有問(wèn)題