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 android.view.ViewTreeObserver;
import java.util.ArrayList;
import java.util.List;
public class WuziqiPanel extends View {
private int mPanelWidth;
private float mLineHight;
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(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.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 heighSize = MeasureSpec.getSize(heightMeasureSpec);
int heighMode = MeasureSpec.getMode(heightMeasureSpec);
int width = Math.min(widthSize, heighSize);
if (widthMode == MeasureSpec.UNSPECIFIED) {
width = heighSize;
} else if (heighMode == MeasureSpec.UNSPECIFIED) {
width = widthSize;
}
setMeasuredDimension(width, width);
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
// TODO Auto-generated method stub
super.onSizeChanged(w, h, oldw, oldh);
mPanelWidth = w;
mLineHight =mPanelWidth* 1.0f / MAX_LINE;
int PieceWidth=(int) (ratioPieceOfLineHeight);
mWhitePiece=Bitmap.createScaledBitmap(mWhitePiece,PieceWidth,PieceWidth,false);
mBlackPiece=Bitmap.createScaledBitmap(mBlackPiece,PieceWidth,PieceWidth,false);
}
@Override
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);
}
invalidate();
mIsWhite=! mIsWhite;
}
return true;
}
private Point getVaLidPoint(int x,int y)
{
return new Point((int)(x/mLineHight),(int)(y/mLineHight));
}
@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 WhitePiont=mWhiteArray.get(i);
canvas.drawBitmap(mWhitePiece,
(WhitePiont.x+(1-ratioPieceOfLineHeight)/2)*mLineHight,
(WhitePiont.y+(1-ratioPieceOfLineHeight)/2)*mLineHight,null);
}
for(int i=0,n=mBlackArray.size();i<n;i++){
Point BlackPiont=mBlackArray.get(i);
canvas.drawBitmap(mBlackPiece,
(BlackPiont.x+(1-ratioPieceOfLineHeight)/2)*mLineHight,
(BlackPiont.y+(1-ratioPieceOfLineHeight)/2)*mLineHight,null);
}
}
private void drawBoard(Canvas canvas) {
int w = mPanelWidth;
float lineHeight =mLineHight ;
//绘制棋盘的横线和竖线
for (int i = 0; i < MAX_LINE; i++) {
int startX = (int) (lineHeight * 0.5);
int endX = w - startX;
int y = (int) (lineHeight * (0.5 + i));//倍数增长,如果是加,长度就会是完全错误的
//绘制横线
canvas.drawLine(startX, y, endX, y, mPaint);
//绘制竖线
canvas.drawLine(y, startX, y, endX, mPaint);
}
}
}