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;
/**
* Created by Administrator on 2017/6/24.
*/
public class Wuziqipanl extends View {
private int mPanelWidth;
private float mLineheight;
private int MAX_LINE = 10;
private Paint mPaint = new Paint();
private Bitmap mWhitePiece;
private Bitmap mBlackPiece;
//限制棋子是棋格的3/4
private float ratioPieceOfLineHeight = 3*1.0f/4;
//当前轮到白棋
private boolean mIsWhite = true;
private List<Point> mWhiteArray = new ArrayList<Point>();
private List<Point> mBlackArray = new ArrayList<Point>();
public Wuziqipanl(Context context, AttributeSet attrs) {
super(context, attrs);
setBackgroundColor(0x44ff0000);
init();
}
private void init() {
mPaint.setColor(0x88000000);
mPaint.setAntiAlias(true);
mPaint.setDither(true);
mPaint.setStyle(Paint.Style.STROKE);
//黑白棋初始化
mWhitePiece = BitmapFactory.decodeResource(getResources(),R.drawable.bai);
mBlackPiece = BitmapFactory.decodeResource(getResources(),R.drawable.hei);
}
@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(widthSize,heightSize);
if(widthMode == MeasureSpec.UNSPECIFIED){
width = heightSize;
}else if( heightMode == MeasureSpec.UNSPECIFIED){
width = widthSize;
}
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(mBlackPiece,pieceWidth,pieceWidth,false);
}
//触摸事件
@Override
public boolean onTouchEvent(MotionEvent event) {
int cation = event.getAction();
if (cation == 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);
}
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));
}
//绘制棋盘
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
drawBoard(canvas);
drawpieces(canvas);
}
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 +(1-ratioPieceOfLineHeight)/2)*mLineheight,null);
}
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 +(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 startX = (int) (lineheight / 2);
int endX = (int) (w - (lineheight / 2));
int y = (int) ((0.5 +i)*lineheight);
canvas.drawLine(startX,y,endX,y,mPaint);
canvas.drawLine(y,startX,y,endX,mPaint);
}
}
}