[Android][五子棋]②--初始化onTouch事件

增加的代码

  private Bitmap mWhitePiece;

    private  Bitmap mBlackPiece;

    //棋子的大小比例
    private float ratioPieceOfLineHeigh=3*1.0f/4;

    //白棋先走,当前轮到白棋
    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 因为如果棋盘是可滚动的 在滚动过程中也会执行此事件
        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;
    }

    /**
     * 获取点击在合理范围的点
     * @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;

    //格子数
    private int MAX_LINE=10;

    private Paint mPaint=new Paint();

    private Bitmap mWhitePiece;

    private  Bitmap mBlackPiece;

    //棋子的大小比例
    private float ratioPieceOfLineHeigh=3*1.0f/4;

    //白棋先走,当前轮到白棋
    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);
        //防抖动
        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);

        //设置棋盘宽度
        mPanelWidth=w;
        //设置格子线的高度
        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 因为如果棋盘是可滚动的 在滚动过程中也会执行此事件
        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;
    }

    /**
     * 获取点击在合理范围的点
     * @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);


        }

    }
}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 176,012评论 25 709
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 14,461评论 4 61
  • 中国人讲究亲情、友情、面子,为朋友可以两肋插刀。但是在现今社会里人与人之间关系变得越来越复杂,帮助他人有时候不会得...
    冬青一内蒙阅读 3,798评论 0 0
  • 有那么转身的一刻 我依然是我 身后早已不见你的婀娜 继续顾自的走着 哼着街头响起的歌 我想不起我有什么曾丢失过 亦...
    几处留白向阳花开阅读 1,625评论 2 4
  • 豆瓣:8.3 个人:8.6 看完之后忍不住发了条微信,洗澡的时候想为什么要发微信,之于我,微信只不过是个工具,而不...
    水_印阅读 2,047评论 0 1

友情链接更多精彩内容