Android Custom UI gesture Password Improved version _android

Source: Internet
Author: User
Tags abs static class

Then the first Android UI gesture cipher design continues to improve on the basis of the results shown below

Activity_main.xml

<linearlayout 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:orientation= "vertical" 
 tools:context= ". Mainactivity "> 
 
 <textview 
 android:layout_width=" match_parent " 
 android:layout_height=" Wrap_ Content " 
 android:gravity=" center " 
 android:text=" Please enter the password 
 android:id= "@+id/text" 
 /> 
 
 <com.example.lockpatterview.lockpatterview 
 android:id= "@+id/lock" 
 android:layout_weight= "1" 
 Android:layout_width= "Match_parent" 
 android:layout_height= "0DP"/> 
 
</LinearLayout> 

Mainactivity

Package Com.example.lockpatterview; 
 
Import Com.example.lockpatterview.LockPatterView.OnPatterChangeLister; 
Import Android.os.Bundle; 
Import Android.text.TextUtils; 
Import Android.widget.TextView; 
Import Android.widget.Toast; 
 
Import android.app.Activity; 
 public class Mainactivity extends activity implements Onpatterchangelister {Lockpatterview lock; 
 TextView text; 
 
 String p = "14789"; 
 @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); 
 Setcontentview (R.layout.activity_main); 
 Text = (TextView) Findviewbyid (R.id.text); 
 Lock = (Lockpatterview) Findviewbyid (R.id.lock); Lock. 
 Setonpatterchangelister (this); @Override public void Onpatterchange (String passwordstr) {if (! 
  Textutils.isempty (PASSWORDSTR)) {if (Passwordstr.equals (p)) {text.settext (PASSWORDSTR); 
  else {text.settext ("password error"); 
  Lock.errorpoint (); 
 }else {toast.maketext (mainactivity.this, "Connect at least 5 points", 0). Show (); }
 
 @Override public void Onpatterstart (Boolean isstart) {if (Isstart) {Text.settext ("draw pattern"); 
 } 
 } 
 
}

Lockpatterview

Package Com.example.lockpatterview; 
Import java.util.ArrayList; 
 
Import java.util.List; 
Import Android.content.Context; 
Import Android.graphics.Bitmap; 
Import Android.graphics.BitmapFactory; 
Import Android.graphics.Canvas; 
Import Android.graphics.Matrix; 
Import Android.graphics.Paint; 
Import Android.text.TextUtils; 
Import Android.util.AttributeSet; 
Import android.view.MotionEvent; 
 
Import Android.view.View; 
 
 public class Lockpatterview extends View {private static final int point_size = 5; 
 
 Private point[][] points = new point[3][3]; 
 
 Private Matrix matrix = new Matrix (); 
 
 Private float width, height, offstarty, MoveX, Movey; 
 
 Private Bitmap bitmap_pressed, Bitmap_normal, Bitmap_error, Bitmap_line, Bitmap_line_error; 
 
 Private Paint Paint = new Paint (Paint.anti_alias_flag); 
 
 Private list<point> pointlist = new arraylist<lockpatterview.point> (); 
 
 Private Onpatterchangelister Onpatterchangelister; /** * Constructor/public LoCkpatterview (context, AttributeSet attrs, int defstyle) {Super (context, attrs, Defstyle); 
 Public Lockpatterview (context, AttributeSet attrs) {Super (context, attrs); 
 Public Lockpatterview {Super (context); /********************************************************* * Draw 9 Sudoku * Movepoint represents the mouse in the move, but not the 9 in the house inside the point * isinit whether the initial 
 
 9 points * Isselect is selected state * Isfinish whether the drawing is completed * * Private Boolean Isinit, Isselect, Isfinish, Movepoint;  @Override protected void OnDraw (Canvas Canvas) {//initialization is not initialized for the first time, once initialization is not done in the initial chemical, isinit means---Default has not initialized the IF (!isinit) 
 {//initialization of 9 points initpoints (); 
 
 //Draw 9 points Points2canvas (canvas); 
  if (pointlist.size () > 0) {Point a = pointlist.get (0); 
  Draw Nine Sudoku sit punctuation for (int i = 0; i < pointlist.size (); i++) {point b = pointlist.get (i); 
  Line2canvas (canvas, A, b); 
  A = b; 
  //Draw mouse coordinates if (movepoint) {Line2canvas (canvas, A, new Point (MoveX, Movey)); 
} 
 } /** * Initializes 9 points to obtain 2 states of the 3 state line of the point bit and the coordinate position of 9 points and the initialization password operation isinit= * True setting state-the next time you do not have to initialize the words to work/private void Initpoin 
 TS () {//Get layout width high width = getwidth (); 
 
 Height = getheight (); 
 
 Horizontal screen and vertical screen Offstarty = (height-width)/2; 
 Picture Resource Bitmap_normal = Bitmapfactory.decoderesource (Getresources (), r.drawable.btn_circle_normal); 
 bitmap_pressed = Bitmapfactory.decoderesource (Getresources (), r.drawable.btn_circle_pressed); 
 Bitmap_error = Bitmapfactory.decoderesource (Getresources (), r.drawable.btn_circle_selected); 
 Bitmap_line = Bitmapfactory.decoderesource (Getresources (), r.drawable.ddd); 
 
 Bitmap_line_error = Bitmapfactory.decoderesource (Getresources (), R.DRAWABLE.QQQ); 
 Points[0][0] = new Point (WIDTH/4, Offstarty + WIDTH/4); 
 POINTS[0][1] = new Point (WIDTH/2, Offstarty + WIDTH/4); 
 
 POINTS[0][2] = new Point (WIDTH/4 * 3, Offstarty + WIDTH/4); 
 Points[1][0] = new Point (WIDTH/4, Offstarty + WIDTH/4 * 2); POINTS[1][1] = new Point(WIDTH/2, Offstarty + WIDTH/4 * 2); 
 
 POINTS[1][2] = new Point (WIDTH/4 * 3, Offstarty + WIDTH/4 * 2); 
 Points[2][0] = new Point (WIDTH/4, Offstarty + WIDTH/4 * 3); 
 POINTS[2][1] = new Point (WIDTH/2, Offstarty + WIDTH/4 * 3); 
 
 POINTS[2][2] = new Point (WIDTH/4 * 3, Offstarty + WIDTH/4 * 3); 
 Set Password 1--9 int index = 1; 
  For (point[] points:this.points) {for (point point:points) {point.index = index; 
  index++; 
 }//Initialization completed isinit = true; 
 /** * Draws 9 points to the canvas, loops through 9 points, draws 3 different 9 dots according to 3 different states */private void Points2canvas (Canvas Canvas) {//loops traverse 9 dots for (int i = 0; i < points.length i++) {//Loop traversal 3 bit for each row for (int j = 0; J < Points[i].length; J +) {//Get 
  In turn, point points = points[i][j]; if (point.state = = point.state_pressed) {//(Bitmap Bitmap, float left, float top, Paint Paint) canvas.drawbitmap (Bitmap_pressed, Point.x-bitmap_normal.getwidth ()/2, Point.y-bitmap_normal.getheigHT ()/2, paint);  else if (point.state = = Point.state_error) {canvas.drawbitmap (Bitmap_error, Point.x-bitmap_normal.getwidth () 
  /2, Point.y-bitmap_normal.getheight ()/2, paint); else {canvas.drawbitmap (Bitmap_normal, Point.x-bitmap_normal.getwidth ()/2, POINT.Y-BITMAP_NORMAL.G 
  Etheight ()/2, paint);  /** * Draw Line/public void Line2canvas (Canvas Canvas, points A, point B) {//Length of line--distance between 2 dots float 
 LineLength = (float) point.distance (A, b); 
 Gets the angle between 2 points float degress = Getdegrees (A, b); 
 
 Rotate Canvas.rotate According to point A (degress, a.x, A.Y); 
  if (a.state = = point.state_pressed) {//XY direction scaling ratio matrix.setscale (Linelength/bitmap_line.getwidth (), 1); 
  Matrix.posttranslate (A.x-bitmap_line.getwidth ()/2, A.y-bitmap_line.getheight ()/2); 
 Canvas.drawbitmap (Bitmap_line, Matrix, paint); 
  else {Matrix.setscale (Linelength/bitmap_line.getwidth (), 1); Matrix.posttranslate (A.x-bitmap_linE.getwidth ()/2, A.y-bitmap_line.getheight ()/2); 
 Canvas.drawbitmap (Bitmap_line_error, Matrix, paint); 
 //Draw line complete regression angle canvas.rotate (-degress, a.x, A.Y); //Get angle public float getdegrees (Point Pointa, points pointb) {return (float) math.todegrees math.atan2 (pointb.y 
 -POINTA.Y, pointb.x-pointa.x)); /**************************************************************************** * Ontouch Event handling * * @Override P 
 Ublic boolean ontouchevent (Motionevent event) {MoveX = Event.getx (); 
 
 Movey = Event.gety (); 
 Movepoint = false; 
 
 Isfinish = false; 
 
 Point point = NULL; 
  Switch (event.getaction ()) {//As soon as the action is pressed, it represents redrawing the interface case MotionEvent.ACTION_DOWN:if (onpatterchangelister!= null) { 
  Onpatterchangelister.onpatterstart (TRUE); 
  //Every time you press, you need to empty the previous set Resetpoint (); 
  Detection is not in the nine Gongne point = Chechselectpoint (); 
  if (point!= null) {//If the position is pressed in the 9 house, it is changed to a state of true isselect = true; 
 } break; 
Case Motionevent.action_move:  if (isselect) {//detection is not in the nine Gongne point = Chechselectpoint (); 
  if (point = = null) {Movepoint = true; 
 }} break; 
  Case MOTIONEVENT.ACTION_UP://Drawing completed, the point status changed to unchecked Isfinish = true; 
  Isselect = false; 
 
 Break //If not finished, if nine is in the checked state if (!isfinish && isselect && point!= null) {//intersection if CrossPoint (PO 
  int)) {Movepoint = true; 
  else {//new point point.state = point.state_pressed; 
  Pointlist.add (point); 
  }//Draw End if (isfinish) {//Draw not set if (pointlist.size () = = 1) {//Resetpoint (); 
  Errorpoint (); 
  else if (pointlist.size () < point_size && pointlist.size () > 0) {//Draw error errorpoint (); 
  if (onpatterchangelister!= null) {onpatterchangelister.onpatterchange (null); 
   } else {if (onpatterchangelister!= null) {String pass = ""; 
   for (int i = 0; i < pointlist.size (); i++) {pass = pass + pointlist.get (i). Index; } if (! 
  Textutils.isempty (pass)) { Onpatterchangelister.onpatterchange (pass); 
 }}} postinvalidate (); 
 return true; /** * Re-draw/public void Resetpoint () {for (int i = 0; i < pointlist.size (); i++) {Point point = PO 
  Intlist.get (i); 
 Point.state = Point.state_normal; 
 } pointlist.clear (); /** * Check to see if/private point Chechselectpoint () {for (int i = 0; i < points.length; i++) {for (int j = 0; J < Points[i].length; 
  J + +) {point point = Points[i][j]; 
  if (Point.with (Point.x, Point.y, Bitmap_normal.getwidth ()/2, MoveX, Movey)) {return point; 
 }} return null; 
 /** * Intersection/Private Boolean crosspoint (point point) {if (Pointlist.contains) {return true; 
 else {return false; }/** * Draw error */public void Errorpoint () {for (point point:pointlist) {point.state = Point.state_er 
 ROR; }}/*********************************************************************** * selfThe defined point */public static class Point {//normal public static int state_normal = 0; 
 Select public static int state_pressed = 1; 
 Error public static int state_error = 2; 
 public float x, y; 
 
 public int index = 0, state = 0; 
 
 Public point () {}; 
  Public point (float x, float y) {this.x = x; 
 This.y = y;  /** * Two point distance/public static double distance (points A, point B) {return math.sqrt (Math.Abs (a.x-b.x) * 
 Math.Abs (a.x-b.x) + math.abs (a.y-b.y) * Math.Abs (A.Y-B.Y));  /** */public static Boolean with (float paintx, float pointy, float R, float MoveX, float movey) {return 
 Math.sqrt ((Paintx-movex) * (Paintx-movex) + (Pointy-movey) * (Pointy-movey)) < R; 
 
 }/** * Pattern Listener */public static interface Onpatterchangelister {void Onpatterchange (String passwordstr); 
 void Onpatterstart (Boolean isstart); /** * Set pattern listener/public void Setonpatterchangelister (Onpatterchangelister chanGelister) {if (Changelister!= null) {this.onpatterchangelister = Changelister; 
 } 
 } 
}

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.