增加的代碼
private Bitmap mWhitePiece;
private Bitmap mBlackPiece;
//棋子的大小比例
private float ratioPieceOfLineHeigh=3*1.0f/4;
//白棋先走,當(dāng)前輪到白棋
private boolean mIsWhite=true;
private List<Point> mWhiteArray=new ArrayList<>();
private List<Point> mBlackArray=new ArrayList<>();
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
//棋子寬度
int pieceWidth= (int) (mLineHeight*ratioPieceOfLineHeigh);
mWhitePiece=Bitmap.createScaledBitmap(mWhitePiece,pieceWidth,pieceWidth,false);
mBlackPiece=Bitmap.createScaledBitmap(mBlackPiece,pieceWidth,pieceWidth, false);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
int action = event.getAction();
//不能ACTION_DOWN 因?yàn)槿绻灞P是可滾動(dòng)的 在滾動(dòng)過程中也會(huì)執(zhí)行此事件
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;
}
return true;
}
/**
* 獲取點(diǎn)擊在合理范圍的點(diǎn)
* @param x
* @param y
* @return
*/
private Point getValidPoint(int x, int y) {
return new Point((int)(x/mLineHeight),(int)(y/mLineHeight));
}
完整代碼
package myapplication4.xt.com.wuziqidemo;
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.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import java.util.ArrayList;
import java.util.List;
/**
* Created by TONG on 2017/6/7.
*/
public class WuziqiPanel extends View {
//棋盤寬度
private int mPanelWidth;
//每格行高
private float mLineHeight;
//格子數(shù)
private int MAX_LINE=10;
private Paint mPaint=new Paint();
private Bitmap mWhitePiece;
private Bitmap mBlackPiece;
//棋子的大小比例
private float ratioPieceOfLineHeigh=3*1.0f/4;
//白棋先走贸营,當(dāng)前輪到白棋
private boolean mIsWhite=true;
private List<Point> mWhiteArray=new ArrayList<>();
private List<Point> mBlackArray=new ArrayList<>();
public WuziqiPanel(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
setBackgroundColor(0x44ff0000);
//初始化
init();
}
private void init() {
mPaint.setColor(0x88000000);
//抗鋸齒
mPaint.setAntiAlias(true);
//防抖動(dòng)
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 heightSize=MeasureSpec.getSize(heightMeasureSpec);
int heightMode=MeasureSpec.getMode(heightMeasureSpec);
int width=Math.min(widthSize,heightSize);
//嵌套在scrollow中寬度或高度不確定
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);
//設(shè)置棋盤寬度
mPanelWidth=w;
//設(shè)置格子線的高度
mLineHeight=mPanelWidth*1.0f/MAX_LINE;
//棋子寬度
int pieceWidth= (int) (mLineHeight*ratioPieceOfLineHeigh);
mWhitePiece=Bitmap.createScaledBitmap(mWhitePiece,pieceWidth,pieceWidth,false);
mBlackPiece=Bitmap.createScaledBitmap(mBlackPiece,pieceWidth,pieceWidth, false);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
int action = event.getAction();
//不能ACTION_DOWN 因?yàn)槿绻灞P是可滾動(dòng)的 在滾動(dòng)過程中也會(huì)執(zhí)行此事件
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;
}
return true;
}
/**
* 獲取點(diǎn)擊在合理范圍的點(diǎn)
* @param x
* @param y
* @return
*/
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);
}
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);
}
}
}