Android自定义View之五子棋实现

1,434 阅读2分钟

前言

学完自定义view后总想着写个项目来练练手总结下之前所学的,思前想后做一个简单的五子棋来熟练一下之前所学的知识,算是给自己一个总结吧

先上一张成品图

思路

1、画棋盘
2、监听手指松开操作 确定触摸点
3、根据触摸点进行判断(判断当前点是否在棋盘内  判断该点是否有无棋子)
4、绘制棋子
5、进行胜利判断

1、画棋盘

思路:
1、获取屏幕最小宽度
2、计算每个格子间的间距
3、计算绘制起始点
4、将绘制棋盘的路径进行保存

 @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        //最小长度
        int minLength;
        if (w<=h){
            minLength = w;
        }else{
            minLength = h;
        }
        chessSpace = minLength/ (chessNum+1);
        //计算起始点 (左右各空一个)
        if (w<h) {//垂直
            leftStart = chessSpace;
            topStart = (h - minLength - 2 * chessSpace) / 2;
        }else{//水平
            leftStart = (w - minLength - 2 * chessSpace) / 2;
            topStart = chessSpace;
        }
        //计算画线的终点
        int end = minLength - 2 * chessSpace;
       //确定横竖线   
       for (int i = 0; i<chessNum; i++){
            mPathLine.moveTo(i* chessSpace,0);
            mPathLine.lineTo(i* chessSpace,end);

            mPathLine.moveTo(0,i* chessSpace);
            mPathLine.lineTo(end,i* chessSpace);
        }
    }

2、监听手指松开操作 确定触摸点 画棋子

1、根据松开的触摸点确定当前在几行几列 (是浮点型 更具四舍五入确定准确的坐标)
2、根据算出的行列确定是否在期盼内
3、用一个二维数组存储棋子 判断当前点是否存在棋子

  @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()){
            case MotionEvent.ACTION_UP:
                drawChessPieces(event.getX(),event.getY());
                break;
        }
        return true;
    }

    //确定圆心 画棋子
    private void drawChessPieces(float x, float y){
        float x1 = (x- leftStart)/ chessSpace;
        float y1 = (y- topStart)/ chessSpace;
        //棋子行列
        int posx = Math.round(x1);
        int posy = Math.round(y1);
        //棋子圆心坐标
        float chessPiecesX = posx * chessSpace;
        float chessPiecesY = posy * chessSpace;

        //确定是否在范围内
        if (posx>=0 && posx<chessNum && posy>=0 && posy<chessNum){
            //判断当前点是否没有棋子
            if (chessPieces[posx][posy] == NULL_CHESS){
                if (isBlack) {//是黑棋
                    chessPieces[posx][posy] = BLACK_CHESS;
                    mPathDrawBlack.addCircle(chessPiecesX, chessPiecesY, chessSpace / 3, Path.Direction.CCW);
                    isBlack = false;
                }else{
                    chessPieces[posx][posy] = RED_CHESS;
                    mPathDrawRed.addCircle(chessPiecesX, chessPiecesY, chessSpace / 3, Path.Direction.CCW);
                    isBlack = true;
                }
                //可以在此处做胜利判断 (未作)
                invalidate();
            }
        }
    }

onDraw()方法

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //移动到绘制起点
        canvas.translate(leftStart, topStart);
        //画棋盘
        canvas.drawPath(mPathLine,mPaintLine);
        //画黑色棋子
        canvas.drawPath(mPathDrawBlack, mPaintBlack);
        //画红色棋子
        canvas.drawPath(mPathDrawRed, mPaintRed);
    }

求关注,求点赞,求star

源码下载