接着第一个Android UI手势密码设计的基础上继续改进,效果图如下
activity_main.xml
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("密码错误"); lock.errorPoint(); } }else { Toast.makeText(MainActivity.this, "至少连接5点", 0).show(); } } @Override public void onPatterStart(boolean isStart) { if (isStart) { text.setText("请绘制图案"); } } }
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 ListpointList = new ArrayList (); private OnPatterChangeLister onPatterChangeLister; /** * 构造函数 */ public LockPatterView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } public LockPatterView(Context context, AttributeSet attrs) { super(context, attrs); } public LockPatterView(Context context) { super(context); } /********************************************************* * 绘制9宫格 * movePoint代表鼠标在移动,但是不是9宫格里面的点 * isInit是否初始化过9个点 * isSelect 点位是否被选中状态 * isFinish 是否绘制完毕 */ private boolean isInit, isSelect, isFinish, movePoint; @Override protected void onDraw(Canvas canvas) { // 第一次没有初始化就进行初始化,一旦初始化就不在初始化工作了,isInit的意思是---默认没有初始化过 if (!isInit) { // 初始化9个点 initPoints(); } // 绘制9个点 points2Canvas(canvas); if (pointList.size() > 0) { Point a = pointList.get(0); // 绘制九宫格坐标点 for (int i = 0; i 0) {// 绘制错误 errorPoint(); if (onPatterChangeLister != null) { onPatterChangeLister.onPatterChange(null); } } else { if (onPatterChangeLister != null) { String pass = ""; for (int i = 0; i
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。