package com.imooc.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 java.util.ArrayList;
import java.util.List;
public class WuziqiPanel 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 List<Point> mWhiteArray = new ArrayList<>();
private List<Point> mBlackArray = new ArrayList<>();
public WuziqiPanel(Context context, AttributeSet attrs) {
super(context,attrs);
setBackgroundColor(0x50C0C0C0);
init();
}
private void init() {
//设置画的图形样子,颜色
mPaint.setColor(0x88000000);
mPaint.setAntiAlias(true);//
mPaint.setDither(true);
mPaint.setStyle(Paint.Style.STROKE);
//对棋子初始化
mWhitePiece = BitmapFactory.decodeResource(getResources(), R.drawable.stone_w);
mBlackPiece = BitmapFactory.decodeResource(getResources(), R.drawable.stone_b);
}
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int widthSize = MeasureSpec.getSize(widthMeasureSpec);
int widthMode = MeasureSpec.getMode(widthMeasureSpec);
int heightSize = MeasureSpec.getSize(widthMeasureSpec);
int heightMode = MeasureSpec.getMode(widthMeasureSpec);
int width = Math.min(widthSize, heightSize);
if (widthMode == MeasureSpec.UNSPECIFIED) {
width = heightSize;
} else if (heightMode == MeasureSpec.UNSPECIFIED) {
width = heightSize;
}
setMeasuredDimension(width, width);
}
@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(mWhitePiece, pieceWidth, pieceWidth, false);
}
public boolean onTouchEvent(MotionEvent event) {
int action = event.getAction();
if (action == MotionEvent.ACTION_UP) {
//获取点击坐标
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);
}
mIsWhite = !mIsWhite;
return true;
}
return true;
}
private Point getValidPoint(int x, int y) {
return new Point((int) (x / mLineHeight), (int) (y / mLineHeight));
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
drawBoard(canvas);
drawPiece(canvas);
}
private void drawPiece(Canvas canvas) {
for (int i = 0, j = mWhiteArray.size(); i < j; i++) {
Point whitePoint = mWhiteArray.get(i);
canvas.drawBitmap(mWhitePiece,
(whitePoint.x + (1 - ratioPieceOFLineHeight) / 2) * mLineHeight,
(whitePoint.y + (1 - ratioPieceOFLineHeight) / 2) * mLineHeight,
null);
}
for (int i = 0, j = mBlackArray.size(); i < j; i++) {
Point blackPoint = mBlackArray.get(i);
canvas.drawBitmap(mBlackPiece,
(blackPoint.x + (1 - ratioPieceOFLineHeight) / 2) * mLineHeight,
(blackPoint.y + (1 - ratioPieceOFLineHeight) / 2) * mLineHeight,
null);
}
}
private void drawBoard(Canvas canvas) {
int w = mPanelWidth;
float lineheight = mLineHeight;
for (int i = 0; i < MAX_Line; i++) {
int starX = (int) lineheight / 2;//左边棋盘开始画的x坐标
int endX = (int) (w - lineheight / 2);//右边棋盘的最后终止坐标
int y = (int) ((0.5 + i) * lineheight);
canvas.drawLine(starX, y, endX, y, mPaint);
canvas.drawLine(y, starX, y, endX, mPaint);
}
}
}