当前位置 主页 > 网站技术 > 代码类 >

    android简单自定义View实现五子棋

    栏目:代码类 时间:2019-11-03 15:52

    本文实例为大家分享了android自定义View实现五子棋的具体代码,供大家参考,具体内容如下

    先说一下吧,android的自定义View就是自己实现一个类去继承View,实现其中的方法,这里面我最感兴趣的就是onDraw方法了,因为你要的样式都要在这里面进实现,看一下效果图吧:

    大概就是介个样子的,长得丑不要紧,能用就行,毕竟只是简单的了解一下嘛!

    *protected void onDraw(Canvas canvas) {
      super.onDraw(canvas);
      RectF rectF;
      for(int i= pandingx ; i < pandingx*7 ; i+=size){
        for(int z= pandingy ; z < pandingy*30 ; z+=size){
          rectF = new RectF(i , z , i+size , z +size);
          canvas.drawRoundRect(rectF , 1 ,1 ,paint);
        }
      }
      for(int i = 0 ; i <list.size() ; i++){
        int h = (int) list.get(i).get("x");
        int s = (int) list.get(i).get("y");
        if((int )list.get(i).get("flag") == 1) {
          rectF = new RectF(h - size/2, s - size/2, h + size/2, s + size/2);
          canvas.drawRoundRect(rectF, 50, 50, black);
        }else{
          rectF = new RectF(h - size/2, s - size/2, h + size/2, s + size/2);
          canvas.drawRoundRect(rectF, 50, 50, white);
        }
      }
      if(flag == 0) {
        rectF = new RectF(x - size / 2, y - size / 2, x + size / 2, y + size / 2);
        canvas.drawRoundRect(rectF, 50, 50, white);
      }else{
        rectF = new RectF(x - size / 2, y - size / 2, x + size / 2, y + size / 2);
        canvas.drawRoundRect(rectF, 50, 50, black);
      }
    }

    这里首先画出了一个棋盘,下的棋子就放在了一个ArrayList中,里面放的是一个个的map,分别放了x,y坐标还有他的属性(黑棋还是白棋),还有就是边界的处理问题,很简单我就不多说啦,毕竟是下棋的这个控件一定要是可以点的,不能只能看看那么简单。

    public boolean onTouchEvent(MotionEvent event) {
      int action = event.getAction();
      switch (action) {
        case MotionEvent.ACTION_DOWN://按下
          x = pandingx+size;
          y = pandingy+size;
          xx = event.getX();
          yy = event.getY();
          invalidate();
          break;
        case MotionEvent.ACTION_MOVE://移动
          if(abs(event.getRawX()-xx)>=30){
            if(event.getRawX()>xx&&JudgeRight() == true){
              x += size;
              xx = event.getRawX();
              yy = event.getRawY();
            }else if(JudgeLeft() == true){
              x -= size;
              xx = event.getRawX();
              yy = event.getRawY();
            }
            invalidate();
          }
          if(abs(event.getRawY()-yy)>=30){
            if(event.getRawY()>yy&&JudgeButtom() == true){
              y += size;
              xx = event.getRawX();
              yy = event.getRawY();
            }else if(JudgeTop() == true){
              y -= size;
              xx = event.getRawX();
              yy = event.getRawY();
            }
            invalidate();
          }
          break;
        case MotionEvent.ACTION_UP://松开
          int i;
          for( i = 0 ; i < list.size() ; i++){
            if(x == (int)list.get(i).get("x")&&y == (int )list.get(i).get("y")){
              toast.repice();
              break;
            }
          }
          if(i == list.size()) {
            Map map = new HashMap();
            map.put("x", x);
            map.put("y", y);
            map.put("flag", flag);
            list.add(map);
            flag = (flag + 1) % 2;
            if(deal.setPiont((x-pandingx)/size , (y-pandingy)/size , flag+1)!=0){
              if(deal.setPiont((x-pandingx)/size , (y-pandingy)/size , flag+1) ==1){
                toast.winblack();
              }
              if(deal.setPiont((x-pandingx)/size , (y-pandingy)/size , flag+1) == 2){
                toast.winwhite();
              }
              list.clear();
              init();
            }
            x = -100;
            y = -100;
            invalidate();
          }
          break;
      }
      return true;
    }