package com.xiaoyu.shoushi_pass_exercise;import android.content.Context;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.Paint;import android.util.AttributeSet;import android.util.Log;import android.view.MotionEvent;import android.view.View;import android.widget.Toast;import java.util.ArrayList;import java.util.List;/** * 1.类的用途: * 2.作者:段玉 * 3.时间:2017/2/8 07 * 4.邮箱:1239959892@QQ.com */public class Shou_Shi extends View { //画笔颜色 public final static int Before_Color = Color.parseColor("#FAEB76"); public final static int After_Color = Color.parseColor("#000000"); //画圆集合 List<Circle> circleList = new ArrayList<>(); //画线的集合 List<Circle> lineList = new ArrayList<Circle>(); //获取宽高 PRivate int width; private int height; //半径 private int radio; private Paint paint; public Shou_Shi(Context context) { super(context); } public Shou_Shi(Context context, AttributeSet attrs) { super(context, attrs); } public Shou_Shi(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); width = getWidth(); height = getHeight(); radio = width / 10; addlist(); drawCircle(canvas); } @Override public boolean onTouchEvent(MotionEvent event) { switch (event.getAction()){ case MotionEvent.ACTION_DOWN: { } break; case MotionEvent.ACTION_MOVE: { int x = (int) event.getX(); int y = (int) event.getY(); Log.d("段玉",x+"---"+y); //坐标转换 for (int i = 0; i < circleList.size(); i++) { int xx = (int) Math.pow((x - circleList.get(i).x), 2); int yy = (int) Math.pow((y - circleList.get(i).y),2); int num = xx + yy; //根据勾股定力判断 if (num <= Math.pow(radio,2)){ //重新设置画笔颜色 circleList.get(i).color = After_Color; //设置为选中状态 circleList.get(i).isSelect = true; //判断是否存在,避免加重复 Boolean isflag = false; for (int j = 0; j < lineList.size(); j++) { //通过数字判断存在不存在 if (circleList.get(i).number == lineList.get(j).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(); } } } break; case MotionEvent.ACTION_UP: { String passWord = ""; for (int i = 0; i < lineList.size(); i++) { passWord += lineList.get(i).number+""; } Toast.makeText(getContext(),"当前密码是"+passWord, Toast.LENGTH_SHORT).show(); //清理屏幕 for (int i = 0; i < circleList.size(); i++) { circleList.get(i).color = Before_Color; circleList.get(i).isSelect = false; } //清空集合 lineList.clear(); //同时刷新ui postInvalidate(); } break; } return true; } private void drawCircle(Canvas canvas) { //实例化画笔 paint = new Paint(); //设置画笔抗锯齿 paint.setAntiAlias(true); //设置画笔宽度 paint.setStrokeWidth(3); //设置空心圆 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; } } } private void addlist() { for (int i = 1; i <= 9; i++) { //实例化对象 Circle circle = new Circle(); if (i <= 3){ circle.y = height/2 - radio*3; }else if (i > 3 && i <= 6){ circle.y = height / 2; }else{ circle.y = height/2 +radio*3; } if (i == 1 || i == 4 || i == 7){ circle.x = radio * 2; }else if (i == 2 || i == 5 || i == 8){ circle.x = radio * 5; }else{ circle.x = radio * 8; } circle.radio = radio; circle.isSelect = false; circle.color = Before_Color; circle.number = i; circleList.add(circle); } }}//一下就是简单的xml<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#00ff00" tools:context="com.xiaoyu.shoushi_pass_exercise.MainActivity"> <com.baway.shoushi_pass_exercise.Shou_Shi android:layout_width="match_parent" android:layout_height="match_parent"/></RelativeLayout>
新闻热点
疑难解答