public class Bean { private int x; private int y; private int radue;//半径 private boolean isSelect; private int color; private int number; public Bean(int x, int y, int radue, boolean isSelect, int color, int number) { this.x = x; this.y = y; this.radue = radue; this.isSelect = isSelect; this.color = color; this.number = number; } public int getX() { return x; } public void setX(int x) { this.x = x; } public int getY() { return y; } public void setY(int y) { this.y = y; } public int getRadue() { return radue; } public void setRadue(int radue) { this.radue = radue; } public boolean isSelect() { return isSelect; } public void setSelect(boolean select) { isSelect = select; } public int getColor() { return color; } public void setColor(int color) { this.color = color; } public int getNumber() { return number; } public void setNumber(int number) { this.number = number; } @Override public String toString() { return "Bean{" + "x=" + x + ", y=" + y + ", radue=" + radue + ", isSelect=" + isSelect + ", color=" + color + ", number=" + number + '}'; } public Bean() { super(); } }
自定义类的实现
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; /** * 类的用途: * Created by * on 2017/1/13 */ public class MyView extends View { // 初始化两个颜色 private static int before_color = Color.parseColor("#000000"); private static int after_color = Color.parseColor("#ff0000"); //存放圆的集合 ArrayList<Bean> criclelist = new ArrayList<Bean>(); // 划线的集合 ArrayList<Bean> lineList = new ArrayList<Bean>(); private int width; private int heigh; private int radio; private Paint paint; public MyView(Context context) { this(context, null); } public MyView(Context context, AttributeSet attrs) { this(context, attrs, 0); } public MyView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); // 初始化控件的宽高半径 width = getWidth(); heigh = getHeight(); // 半径 if (heigh == 0 || width == 0) {//如果宽和高为空时,返回 return; } radio = getWidth() / 10; // 获取绘制的数据 initDrawData(); // 绘制圆 DrawCricle(canvas); } // 触摸事件 @Override public boolean onTouchEvent(MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: break; case MotionEvent.ACTION_MOVE: // 移动划线 onMove(event); break; case MotionEvent.ACTION_UP: // 手指抬起 downMove(); break; } return true; } // 手指抬起 private void downMove() { // 记录密码 String password=""; for (int i = 0; i < lineList.size() ; i++) { password+= lineList.get(i).getNumber()+""; } Toast.makeText(getContext(), "密码是"+password, Toast.LENGTH_SHORT).show(); // 手指抬起,恢复 for (int i = 0; i <criclelist.size() ; i++) { criclelist.get(i).setColor(before_color); criclelist.get(i).setSelect(false); } // 清除线 lineList.clear(); // 刷新重绘 postInvalidate(); } //手指移动 private void onMove(MotionEvent event) { // 每次滑动都会得到一个新的x,y坐标 int newX = (int) event.getX(); int newY = (int) event.getY(); // 判断点击的点,到各个圆的距离。是否在圆内, for (int i = 0; i < criclelist.size(); i++) { int xx = (int) Math.pow(newX - criclelist.get(i).getX(), 2);//得到x的平方 int yy = (int) Math.pow(newY - criclelist.get(i).getY(), 2);//得到Y的平方 int num=xx+yy; if(num<=radio*radio){ // 将设置状态设为选中 criclelist.get(i).setSelect(true); // 画笔的颜色 criclelist.get(i).setColor(Color.BLUE); // 设置判断避免加重复 boolean flag=false; for (int j = 0; j <lineList.size() ; j++) { // 通过数字判断是否存在 if(lineList.get(j).getNumber()==criclelist.get(i).getNumber()){ flag=true; } } // 创建一个对象 if(flag==false){ Bean bean = new Bean(); bean.setSelect(true); bean.setX(criclelist.get(i).getX()); bean.setY(criclelist.get(i).getY()); bean.setNumber(criclelist.get(i).getNumber()); lineList.add(bean); } // 刷新UI postInvalidate(); } } } // 绘制圆 private void DrawCricle(Canvas canvas) { // 实例化画笔 paint = new Paint(); paint.setAntiAlias(true); paint.setStyle(Paint.Style.STROKE); paint.setColor(before_color); paint.setStrokeWidth(3); // 循环绘制圆 for (int i = 0; i < criclelist.size(); i++) { paint.setColor(criclelist.get(i).getColor()); canvas.drawCircle(criclelist.get(i).getX(), criclelist.get(i).getY(), criclelist.get(i).getRadue(), paint); } // 绘制线。先定义一个标志记录是否第一次绘制 boolean flag = false; // 定义一个初始化的x,y起始坐标 int startX = 0; int startY = 0; for (int i = 0; i < lineList.size(); i++) { if (flag == false) {//第一次划线 flag = true; startX = lineList.get(i).getX(); startY = lineList.get(i).getY(); } else {//有了起始xy坐标。开始划线 paint.setColor(Color.RED); canvas.drawLine(startX, startY, lineList.get(i).getX(), lineList.get(i).getY(), paint); // 将终点位置再重新赋值给起始点 startX = lineList.get(i).getX(); startY = lineList.get(i).getY(); } } } // 获取绘制的数据 private void initDrawData() { // (一共九个圆) 先绘制中间行 for (int i = 1; i < 10; i++) { // 实例化对象 Bean bean = new Bean(); // 获取Y的值 if (i <= 3) { bean.setY((width / 2) - (radio * 3)); } else if (i > 3 && i <= 6) { bean.setY(width / 2); } else { bean.setY((width / 2) + (radio * 3)); } // 获取x的值 if ((i == 1) || (i == 4) || (i == 7)) { bean.setX(radio * 2); } else if (i == 2 || i == 5 || i == 8) { bean.setX(radio * 5); } else { bean.setX(radio * 8); } bean.setColor(before_color); bean.setNumber(i); bean.setRadue(radio); bean.setSelect(false); // 将设置的属性存进集合 criclelist.add(bean); } } }