自定义控件之九宫格


自定义控件手写九宫格

//属性封装部分

public class Circle {
    public  int x;
    public  int y;
    public  int radio;
    public  int color;
    public  boolean isSelect;

    //用来作为个人标示使用
    public  int number;
}


//自定义逻辑部分

package view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/1/13.
 */

public class Sudoku extends View {
   //画笔颜色

    public final  static  int Before_Color=Color.parseColor("#000000");
    //画之后的颜色
    public final  static  int After_Color=Color.parseColor("#ff0000");

    //点的集合
    List<Circle> circleList=new ArrayList<>();

    //线的集合
    List<Circle> lineList=new ArrayList<>();

    private int width;
    private  int height;
    private  int radio;
    Paint paint;

    public Sudoku(Context context) {
        super(context);
    }
    public Sudoku(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public Sudoku(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        width=getWidth();
        height=getHeight();
        //如果宽或高为0,则无意义
        if (height==0||width==0){
            return;
        }
        //得到半径,把宽平均分为10份
        radio=width/10;
        addList();
        Circle(canvas);

    }


    @Override
    public boolean onTouchEvent(MotionEvent event) {

        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:
                break;
            case MotionEvent.ACTION_MOVE:
                 move(event);
                break;
            case MotionEvent.ACTION_UP:
                up();
                break;
        }

        return true;
    }

  public void move(MotionEvent event){
      int x= (int) event.getX();
      int y= (int) event.getY();

      for (int i = 0; i < circleList.size(); i++) {

          //xx x的平法
          int xx= (int) Math.pow((x-circleList.get(i).x),2);
          int yy= (int) Math.pow((y-circleList.get(i).y),2);
          int sum=xx+yy;
          //点击在园内
          if (sum<=Math.pow(radio,2)){
              //将状态设置为选中
           circleList.get(i).isSelect=true;
              //滑动时改变画笔颜色
             circleList.get(i).color=After_Color;
              //设置判断避免重复
              boolean isFlag=false;
              for (int j = 0; j < lineList.size(); j++) {
                  //通过数字判断存在不存在
                  if (lineList.get(j).number==circleList.get(i).number)
                  {
                      isFlag=true;
                  }
              }
              //如果不存在就加入
              if (isFlag==false){
                  Circle circle=new Circle();
                  circle.x=circleList.get(i).x;
                  circle.y=circleList.get(i).y;
                  circle.color=circleList.get(i).color;
                  circle.number=circleList.get(i).number;
                  lineList.add(circle);
              }
              //同时刷新ui
              postInvalidate();

          }
      }
  }

     public  void up(){
        String password="";
        for (int i = 0; i < lineList.size(); i++) {
            password+=lineList.get(i).number+"";
        }
        Toast.makeText(getContext(),"当前密码是"+password,Toast.LENGTH_LONG).show();
        //清理屏幕
        for (int i = 0; i < circleList.size(); i++) {
            circleList.get(i).color=Before_Color;
            circleList.get(i).isSelect=false;
        }
        //清空
        lineList.clear();

        //刷新
        postInvalidate();


    }


    public void addList(){

        for (int i = 1; i <=9 ; i++) {
            Circle cirle=new Circle();
            //当i<=3时,画第一排三个点的纵坐标相同
            if (i<=3){
               //当i<=3时,画第一排三个点的横坐标相同
                cirle.y=height/2-radio*3;

            }else if (i>3 && i<=6){
                //当i>3&&i<6时,画第二排三个点的纵坐标相同
                cirle.y=height/2;

            }else {
                //画第三排的圆
                cirle.y=height/2+radio*3;
            }
            //i==1、4、7每个圆的横坐标相同
            if (i==1||i==4||i==7){

                cirle.x=radio*2;

            }else if (i==2||i==5||i==8){

                cirle.x=radio*5;

            }else{

                cirle.x=radio*8;
            }
            //把点和半径存入集合
            cirle.radio=radio;
            //默认点的颜色为黑色
            cirle.color=Before_Color;
            //默认所有点不选中
            cirle.isSelect=false;
            //把点的数量页作为属性存入集合
            cirle.number=i;
            //把点装入集合
            circleList.add(cirle);

        }

    }
    //绘制圆
    public void Circle(Canvas canvas) {

        //实例化画笔
        paint=new Paint();
        paint.setStrokeWidth(3);
        paint.setAntiAlias(true);
        paint.setStyle(Paint.Style.STROKE);
        //循环点的集合,然后画圆
        for (int i = 0; i < circleList.size(); i++) {
            //设置画圆的颜色
            paint.setColor(circleList.get(i).color);
            //绘制圆
            canvas.drawCircle(circleList.get(i).x,circleList.get(i).y,circleList.get(i).radio,paint);
        }
        
        boolean isFirst=false;
        int startX=0;
        int startY=0;
        //划线的方法
        for (int i = 0; i < lineList.size(); i++) {
            if (isFirst==false){
                isFirst=true;
                startX=lineList.get(i).x;
                startY=lineList.get(i).y;
            }else{
                paint.setColor(lineList.get(i).color);
                canvas.drawLine(startX,startY,lineList.get(i).x,lineList.get(i).y,paint);
                startX=lineList.get(i).x;
                startY=lineList.get(i).y;
            }
        }

    }

}


//xml布局部分

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <view.Sudoku
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />
</LinearLayout>





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值