項目地址:[https://github.com/103style/QQ6.1GestureLock](https://github.com/103style/QQ6.1GestureLock)
該項目是仿照當前版本的QQ手勢鎖,主要實現設置手勢鎖和檢驗手勢鎖的功能。
廢話不多說 先上效果圖。

**主界面代碼:**
~~~
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
? ? public static String password;
? ? @Override
? ? protected void onCreate(Bundle savedInstanceState) {
? ? ? ? super.onCreate(savedInstanceState);
? ? ? ? setContentView(R.layout.activity_main);
? ? ? ? ViewUtils.inject(this);
? ? }
? ? @OnClick({R.id.bt, R.id.btCheck})
? ? @Override
? ? public void onClick(View v) {
? ? ? ? switch (v.getId()){
? ? ? ? ? ? case R.id.bt:
? ? ? ? ? ? ? ? startActivity(new Intent(MainActivity.this, SetGestureLockActivity.class));
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? case R.id.btCheck:
? ? ? ? ? ? ? ? if (password == null){
? ? ? ? ? ? ? ? ? ? Toast.makeText(MainActivity.this,"請先設置手勢鎖",Toast.LENGTH_SHORT).show();
? ? ? ? ? ? ? ? }else{
? ? ? ? ? ? ? ? ? ? Intent intent = new Intent(MainActivity.this, CheckActivity.class);
? ? ? ? ? ? ? ? ? ? intent.putExtra("password",password);
? ? ? ? ? ? ? ? ? ? startActivity(intent);
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? break;
? ? ? ? }
? ? }
}
~~~
**設置手勢密碼代碼:**
~~~
public class SetGestureLockActivity extends Activity {
? ? // 手勢密碼點的狀態
? ? public static final int POINT_STATE_NORMAL = 0; // 正常狀態
? ? public static final int POINT_STATE_SELECTED = 1; // 按下狀態
? ? public static final int POINT_STATE_WRONG = 2; // 錯誤狀態
? ? @ViewInject(R.id.lock_indicator)
? ? LockIndicator mLockIndicator;
? ? @ViewInject(R.id.text_tip)
? ? TextView mTextTip;
? ? @ViewInject(R.id.gesture_container)
? ? FrameLayout mGestureContainer;
? ? private GestureContentView mGestureContentView;
? ? private boolean mIsFirstInput = true;
? ? private String mFirstPassword = null;
? ? @Override
? ? protected void onCreate(Bundle savedInstanceState) {
? ? ? ? super.onCreate(savedInstanceState);
? ? ? ? setContentView(R.layout.activity_set_gesture_lock);
? ? ? ? ViewUtils.inject(this);
? ? ? ? initParam();
? ? }
? ? private void initParam() {
? ? ? ? mGestureContentView = new GestureContentView(this, false, "", new GestureDrawline.GestureCallBack() {
? ? ? ? ? ? @Override
? ? ? ? ? ? public void onGestureCodeInput(String inputCode) {
? ? ? ? ? ? ? ? if (!isInputPassValidate(inputCode)) {
? ? ? ? ? ? ? ? ? ? mTextTip.setText(Html.fromHtml("<font color='#c70c1e'>最少鏈接4個點, 請重新輸入</font>"));
? ? ? ? ? ? ? ? ? ? mGestureContentView.clearDrawlineState(0L);
? ? ? ? ? ? ? ? ? ? return;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? if (mIsFirstInput) {
? ? ? ? ? ? ? ? ? ? mFirstPassword = inputCode;
? ? ? ? ? ? ? ? ? ? updateCodeList(inputCode);
? ? ? ? ? ? ? ? ? ? mGestureContentView.clearDrawlineState(0L);
? ? ? ? ? ? ? ? } else {
? ? ? ? ? ? ? ? ? ? if (inputCode.equals(mFirstPassword)) {
? ? ? ? ? ? ? ? ? ? ? ? Toast.makeText(SetGestureLockActivity.this, "設置成功", Toast.LENGTH_SHORT).show();
? ? ? ? ? ? ? ? ? ? ? ? mGestureContentView.clearDrawlineState(0L);
? ? ? ? ? ? ? ? ? ? ? ? MainActivity.password =mFirstPassword;
? ? ? ? ? ? ? ? ? ? ? ? SetGestureLockActivity.this.finish();
? ? ? ? ? ? ? ? ? ? } else {
? ? ? ? ? ? ? ? ? ? ? ? mTextTip.setText(Html.fromHtml("<font color='#ff0000'>與上一次繪制不一致,請重新繪制</font>"));
? ? ? ? ? ? ? ? ? ? ? ? // 左右移動動畫
? ? ? ? ? ? ? ? ? ? ? ? Animation shakeAnimation = AnimationUtils.loadAnimation(SetGestureLockActivity.this, R.anim.shake);
? ? ? ? ? ? ? ? ? ? ? ? mTextTip.startAnimation(shakeAnimation);
? ? ? ? ? ? ? ? ? ? ? ? // 保持繪制的線,1.5秒后清除
? ? ? ? ? ? ? ? ? ? ? ? mGestureContentView.clearDrawlineState(1300L);
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? mIsFirstInput = false;
? ? ? ? ? ? }
? ? ? ? ? ? @Override
? ? ? ? ? ? public void checkedSuccess() {
? ? ? ? ? ? }
? ? ? ? ? ? @Override
? ? ? ? ? ? public void checkedFail() {
? ? ? ? ? ? }
? ? ? ? });
? ? ? ? // 設置手勢解鎖顯示到哪個布局里面
? ? ? ? mGestureContentView.setParentView(mGestureContainer);
? ? ? ? updateCodeList("");
? ? }
? ? private void updateCodeList(String inputCode) {
? ? ? ? // 更新選擇的圖案
? ? ? ? mLockIndicator.setPath(inputCode);
? ? }
? ? private boolean isInputPassValidate(String inputPassword) {
? ? ? ? if (TextUtils.isEmpty(inputPassword) || inputPassword.length() < 4) {
? ? ? ? ? ? return false;
? ? ? ? }
? ? ? ? return true;
? ? }
}
~~~
**檢驗手勢密碼代碼:**
~~~
public class CheckActivity extends AppCompatActivity {
? ? @ViewInject(R.id.text_tip)
? ? TextView mTextTip;
? ? @ViewInject(R.id.gesture_container)
? ? FrameLayout mGestureContainer;
? ? private GestureContentView mGestureContentView;
? ? //設置的手勢密碼
? ? private String password;
? ? @Override
? ? protected void onCreate(Bundle savedInstanceState) {
? ? ? ? super.onCreate(savedInstanceState);
? ? ? ? setContentView(R.layout.activity_check);
? ? ? ? ViewUtils.inject(this);
? ? ? ? Intent intent = getIntent();
? ? ? ? password = intent.getStringExtra("password");
? ? ? ? Log.e("Password", password);
? ? ? ? initViews();
? ? }
? ? private void initViews() {
? ? ? ? // 初始化一個顯示各個點的viewGroup
? ? ? ? mGestureContentView = new GestureContentView(this, true, password,
? ? ? ? ? ? ? ? new GestureDrawline.GestureCallBack() {
? ? ? ? ? ? ? ? ? ? @Override
? ? ? ? ? ? ? ? ? ? public void onGestureCodeInput(String inputCode) {
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? @Override
? ? ? ? ? ? ? ? ? ? public void checkedSuccess() {
? ? ? ? ? ? ? ? ? ? ? ? mGestureContentView.clearDrawlineState(0L);
? ? ? ? ? ? ? ? ? ? ? ? Toast.makeText(CheckActivity.this, "密碼正確", 1000).show();
? ? ? ? ? ? ? ? ? ? ? ? CheckActivity.this.finish();
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? @Override
? ? ? ? ? ? ? ? ? ? public void checkedFail() {
? ? ? ? ? ? ? ? ? ? ? ? mGestureContentView.clearDrawlineState(1300L);
? ? ? ? ? ? ? ? ? ? ? ? mTextTip.setVisibility(View.VISIBLE);
? ? ? ? ? ? ? ? ? ? ? ? mTextTip.setText(Html
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? .fromHtml("<font color='#ff0000'>密碼錯誤</font>"));
? ? ? ? ? ? ? ? ? ? ? ? // 左右移動動畫
? ? ? ? ? ? ? ? ? ? ? ? Animation shakeAnimation = AnimationUtils.loadAnimation(CheckActivity.this, R.anim.shake);
? ? ? ? ? ? ? ? ? ? ? ? mTextTip.startAnimation(shakeAnimation);
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? });
? ? ? ? // 設置手勢解鎖顯示到哪個布局里面
? ? ? ? mGestureContentView.setParentView(mGestureContainer);
? ? }
}
~~~
package com.hnpolice.luoxiaoke.gesturelock;
import android.widget.ImageView;
**手勢點的代碼:**
~~~
/**
?* Created by Luoxiaoke on 2015/12/2 14:32.
?*/
public class GesturePoint {
? ? /**
? ? ?* 左邊x的值
? ? ?*/
? ? private int leftX;
? ? /**
? ? ?* 右邊x的值
? ? ?*/
? ? private int rightX;
? ? /**
? ? ?* 上邊y的值
? ? ?*/
? ? private int topY;
? ? /**
? ? ?* 下邊y的值
? ? ?*/
? ? private int bottomY;
? ? /**
? ? ?* 這個點對應的ImageView控件
? ? ?*/
? ? private ImageView image;
? ? /**
? ? ?* 中心x值
? ? ?*/
? ? private int centerX;
? ? /**
? ? ?* 中心y值
? ? ?*/
? ? private int centerY;
? ? /**
? ? ?* 狀態值
? ? ?*/
? ? private int pointState;
? ? /**
? ? ?* 代表這個Point對象代表的數字,從1開始(直接感覺從1開始)
? ? ?*/
? ? private int num;
? ? public GesturePoint(int leftX, int rightX, int topY, int bottomY,
? ? ? ? ? ? ? ? ? ? ? ? ImageView image, int num) {
? ? ? ? super();
? ? ? ? this.leftX = leftX;
? ? ? ? this.rightX = rightX;
? ? ? ? this.topY = topY;
? ? ? ? this.bottomY = bottomY;
? ? ? ? this.image = image;
? ? ? ? this.centerX = (leftX + rightX) / 2;
? ? ? ? this.centerY = (topY + bottomY) / 2;
? ? ? ? this.num = num;
? ? }
? ? public int getLeftX() {
? ? ? ? return leftX;
? ? }
? ? public void setLeftX(int leftX) {
? ? ? ? this.leftX = leftX;
? ? }
? ? public int getRightX() {
? ? ? ? return rightX;
? ? }
? ? public void setRightX(int rightX) {
? ? ? ? this.rightX = rightX;
? ? }
? ? public int getTopY() {
? ? ? ? return topY;
? ? }
? ? public void setTopY(int topY) {
? ? ? ? this.topY = topY;
? ? }
? ? public int getBottomY() {
? ? ? ? return bottomY;
? ? }
? ? public void setBottomY(int bottomY) {
? ? ? ? this.bottomY = bottomY;
? ? }
? ? public ImageView getImage() {
? ? ? ? return image;
? ? }
? ? public void setImage(ImageView image) {
? ? ? ? this.image = image;
? ? }
? ? public int getCenterX() {
? ? ? ? return centerX;
? ? }
? ? public void setCenterX(int centerX) {
? ? ? ? this.centerX = centerX;
? ? }
? ? public int getCenterY() {
? ? ? ? return centerY;
? ? }
? ? public void setCenterY(int centerY) {
? ? ? ? this.centerY = centerY;
? ? }
? ? public int getPointState() {
? ? ? ? return pointState;
? ? }
? ? public void setPointState(int state) {
? ? ? ? pointState = state;
? ? ? ? switch (state) {
? ? ? ? ? ? case SetGestureLockActivity.POINT_STATE_NORMAL:
? ? ? ? ? ? ? ? this.image.setBackgroundResource(R.mipmap.gesturepassward_locus_round_original);
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? case SetGestureLockActivity.POINT_STATE_SELECTED:
? ? ? ? ? ? ? ? this.image.setBackgroundResource(R.mipmap.gesturepassward_locus_round_click);
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? case SetGestureLockActivity.POINT_STATE_WRONG:
? ? ? ? ? ? ? ? this.image.setBackgroundResource(R.mipmap.gesturepassward_locus_round_wrong);
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? default:
? ? ? ? ? ? ? ? break;
? ? ? ? }
? ? }
? ? public int getNum() {
? ? ? ? return num;
? ? }
? ? public void setNum(int num) {
? ? ? ? this.num = num;
? ? }
? ? @Override
? ? public int hashCode() {
? ? ? ? final int prime = 31;
? ? ? ? int result = 1;
? ? ? ? result = prime * result + bottomY;
? ? ? ? result = prime * result + ((image == null) ? 0 : image.hashCode());
? ? ? ? result = prime * result + leftX;
? ? ? ? result = prime * result + rightX;
? ? ? ? result = prime * result + topY;
? ? ? ? return result;
? ? }
? ? @Override
? ? public boolean equals(Object obj) {
? ? ? ? if (this == obj)
? ? ? ? ? ? return true;
? ? ? ? if (obj == null)
? ? ? ? ? ? return false;
? ? ? ? if (getClass() != obj.getClass())
? ? ? ? ? ? return false;
? ? ? ? GesturePoint other = (GesturePoint) obj;
? ? ? ? if (bottomY != other.bottomY)
? ? ? ? ? ? return false;
? ? ? ? if (image == null) {
? ? ? ? ? ? if (other.image != null)
? ? ? ? ? ? ? ? return false;
? ? ? ? } else if (!image.equals(other.image))
? ? ? ? ? ? return false;
? ? ? ? if (leftX != other.leftX)
? ? ? ? ? ? return false;
? ? ? ? if (rightX != other.rightX)
? ? ? ? ? ? return false;
? ? ? ? if (topY != other.topY)
? ? ? ? ? ? return false;
? ? ? ? return true;
? ? }
? ? @Override
? ? public String toString() {
? ? ? ? return "Point [leftX=" + leftX + ", rightX=" + rightX + ", topY="
? ? ? ? ? ? ? ? + topY + ", bottomY=" + bottomY + "]";
? ? }
}
~~~
**手勢密碼路徑繪制代碼:**
~~~
public class GestureDrawline extends View {
? ? private int mov_x;// 聲明起點坐標
? ? private int mov_y;
? ? private Paint paint;// 聲明畫筆
? ? private Canvas canvas;// 畫布
? ? private Bitmap bitmap;// 位圖
? ? private List<GesturePoint> list;// 裝有各個view坐標的集合
? ? private List<Pair<GesturePoint, GesturePoint>> lineList;// 記錄畫過的線
? ? private Map<String, GesturePoint> autoCheckPointMap;// 自動選中的情況點
? ? private boolean isDrawEnable = true; // 是否允許繪制
? ? /**
? ? ?* 屏幕的寬度和高度
? ? ?*/
? ? private int[] screenDispaly;
? ? /**
? ? ?* 手指當前在哪個Point內
? ? ?*/
? ? private GesturePoint currentPoint;
? ? /**
? ? ?* 用戶繪圖的回調
? ? ?*/
? ? private GestureCallBack callBack;
? ? /**
? ? ?* 用戶當前繪制的圖形密碼
? ? ?*/
? ? private StringBuilder passWordSb;
? ? /**
? ? ?* 是否為校驗
? ? ?*/
? ? private boolean isVerify;
? ? /**
? ? ?* 用戶傳入的passWord
? ? ?*/
? ? private String passWord;
? ? public GestureDrawline(Context context, List<GesturePoint> list, boolean isVerify,
? ? ? ? ? ? ? ? ? ? ? ? ? ?String passWord, GestureCallBack callBack) {
? ? ? ? super(context);
? ? ? ? screenDispaly = AppUtil.getScreenDispaly(context);
? ? ? ? paint = new Paint(Paint.DITHER_FLAG);// 創建一個畫筆
? ? ? ? bitmap = Bitmap.createBitmap(screenDispaly[0], screenDispaly[0], Bitmap.Config.ARGB_8888); // 設置位圖的寬高
? ? ? ? canvas = new Canvas();
? ? ? ? canvas.setBitmap(bitmap);
? ? ? ? paint.setStyle(Paint.Style.STROKE);// 設置非填充
? ? ? ? paint.setStrokeWidth(10);// 筆寬5像素
? ? ? ? paint.setColor(Color.rgb(245, 142, 33));// 設置默認連線顏色
? ? ? ? paint.setAntiAlias(true);// 不顯示鋸齒
? ? ? ? this.list = list;
? ? ? ? this.lineList = new ArrayList<Pair<GesturePoint, GesturePoint>>();
? ? ? ? initAutoCheckPointMap();
? ? ? ? this.callBack = callBack;
? ? ? ? // 初始化密碼緩存
? ? ? ? this.isVerify = isVerify;
? ? ? ? this.passWordSb = new StringBuilder();
? ? ? ? this.passWord = passWord;
? ? }
? ? private void initAutoCheckPointMap() {
? ? ? ? autoCheckPointMap = new HashMap<String,GesturePoint>();
? ? ? ? autoCheckPointMap.put("1,3", getGesturePointByNum(2));
? ? ? ? autoCheckPointMap.put("1,7", getGesturePointByNum(4));
? ? ? ? autoCheckPointMap.put("1,9", getGesturePointByNum(5));
? ? ? ? autoCheckPointMap.put("2,8", getGesturePointByNum(5));
? ? ? ? autoCheckPointMap.put("3,7", getGesturePointByNum(5));
? ? ? ? autoCheckPointMap.put("3,9", getGesturePointByNum(6));
? ? ? ? autoCheckPointMap.put("4,6", getGesturePointByNum(5));
? ? ? ? autoCheckPointMap.put("7,9", getGesturePointByNum(8));
? ? }
? ? private GesturePoint getGesturePointByNum(int num) {
? ? ? ? for (GesturePoint point : list) {
? ? ? ? ? ? if (point.getNum() == num) {
? ? ? ? ? ? ? ? return point;
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? return null;
? ? }
? ? // 畫位圖
? ? @Override
? ? protected void onDraw(Canvas canvas) {
? ? ? ? // super.onDraw(canvas);
? ? ? ? canvas.drawBitmap(bitmap, 0, 0, null);
? ? }
? ? // 觸摸事件
? ? @Override
? ? public boolean onTouchEvent(MotionEvent event) {
? ? ? ? if (isDrawEnable == false) {
? ? ? ? ? ? // 當期不允許繪制
? ? ? ? ? ? return true;
? ? ? ? }
? ? ? ? paint.setColor(Color.rgb(18, 195, 249));// 設置默認連線顏色
? ? ? ? switch (event.getAction()) {
? ? ? ? ? ? case MotionEvent.ACTION_DOWN:
? ? ? ? ? ? ? ? mov_x = (int) event.getX();
? ? ? ? ? ? ? ? mov_y = (int) event.getY();
? ? ? ? ? ? ? ? // 判斷當前點擊的位置是處于哪個點之內
? ? ? ? ? ? ? ? currentPoint = getPointAt(mov_x, mov_y);
? ? ? ? ? ? ? ? if (currentPoint != null) {
? ? ? ? ? ? ? ? ? ? currentPoint.setPointState(SetGestureLockActivity.POINT_STATE_SELECTED);
? ? ? ? ? ? ? ? ? ? passWordSb.append(currentPoint.getNum());
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? // canvas.drawPoint(mov_x, mov_y, paint);// 畫點
? ? ? ? ? ? ? ? invalidate();
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? case MotionEvent.ACTION_MOVE:
? ? ? ? ? ? ? ? clearScreenAndDrawList();
? ? ? ? ? ? ? ? // 得到當前移動位置是處于哪個點內
? ? ? ? ? ? ? ? GesturePoint pointAt = getPointAt((int) event.getX(), (int) event.getY());
? ? ? ? ? ? ? ? // 代表當前用戶手指處于點與點之前
? ? ? ? ? ? ? ? if (currentPoint == null && pointAt == null) {
? ? ? ? ? ? ? ? ? ? return true;
? ? ? ? ? ? ? ? } else {// 代表用戶的手指移動到了點上
? ? ? ? ? ? ? ? ? ? if (currentPoint == null) {// 先判斷當前的point是不是為null
? ? ? ? ? ? ? ? ? ? ? ? // 如果為空,那么把手指移動到的點賦值給currentPoint
? ? ? ? ? ? ? ? ? ? ? ? currentPoint = pointAt;
? ? ? ? ? ? ? ? ? ? ? ? // 把currentPoint這個點設置選中為true;
? ? ? ? ? ? ? ? ? ? ? ? currentPoint.setPointState(SetGestureLockActivity.POINT_STATE_SELECTED);
? ? ? ? ? ? ? ? ? ? ? ? passWordSb.append(currentPoint.getNum());
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? if (pointAt == null || currentPoint.equals(pointAt) || SetGestureLockActivity.POINT_STATE_SELECTED == pointAt.getPointState()) {
? ? ? ? ? ? ? ? ? ? // 點擊移動區域不在圓的區域,或者當前點擊的點與當前移動到的點的位置相同,或者當前點擊的點處于選中狀態
? ? ? ? ? ? ? ? ? ? // 那么以當前的點中心為起點,以手指移動位置為終點畫線
? ? ? ? ? ? ? ? ? ? canvas.drawLine(currentPoint.getCenterX(), currentPoint.getCenterY(), event.getX(), event.getY(), paint);// 畫線
? ? ? ? ? ? ? ? } else {
? ? ? ? ? ? ? ? ? ? // 如果當前點擊的點與當前移動到的點的位置不同
? ? ? ? ? ? ? ? ? ? // 那么以前前點的中心為起點,以手移動到的點的位置畫線
? ? ? ? ? ? ? ? ? ? canvas.drawLine(currentPoint.getCenterX(), currentPoint.getCenterY(), pointAt.getCenterX(), pointAt.getCenterY(), paint);// 畫線
? ? ? ? ? ? ? ? ? ? pointAt.setPointState(SetGestureLockActivity.POINT_STATE_SELECTED);
? ? ? ? ? ? ? ? ? ? // 判斷是否中間點需要選中
? ? ? ? ? ? ? ? ? ? GesturePoint betweenPoint = getBetweenCheckPoint(currentPoint, pointAt);
? ? ? ? ? ? ? ? ? ? if (betweenPoint != null && SetGestureLockActivity.POINT_STATE_SELECTED != betweenPoint.getPointState()) {
? ? ? ? ? ? ? ? ? ? ? ? // 存在中間點并且沒有被選中
? ? ? ? ? ? ? ? ? ? ? ? Pair<GesturePoint, GesturePoint> pair1 = new Pair<GesturePoint, GesturePoint>(currentPoint, betweenPoint);
? ? ? ? ? ? ? ? ? ? ? ? lineList.add(pair1);
? ? ? ? ? ? ? ? ? ? ? ? passWordSb.append(betweenPoint.getNum());
? ? ? ? ? ? ? ? ? ? ? ? Pair<GesturePoint, GesturePoint> pair2 = new Pair<GesturePoint, GesturePoint>(betweenPoint, pointAt);
? ? ? ? ? ? ? ? ? ? ? ? lineList.add(pair2);
? ? ? ? ? ? ? ? ? ? ? ? passWordSb.append(pointAt.getNum());
? ? ? ? ? ? ? ? ? ? ? ? // 設置中間點選中
? ? ? ? ? ? ? ? ? ? ? ? betweenPoint.setPointState(SetGestureLockActivity.POINT_STATE_SELECTED);
? ? ? ? ? ? ? ? ? ? ? ? // 賦值當前的point;
? ? ? ? ? ? ? ? ? ? ? ? currentPoint = pointAt;
? ? ? ? ? ? ? ? ? ? } else {
? ? ? ? ? ? ? ? ? ? ? ? Pair<GesturePoint, GesturePoint> pair = new Pair<GesturePoint, GesturePoint>(currentPoint, pointAt);
? ? ? ? ? ? ? ? ? ? ? ? lineList.add(pair);
? ? ? ? ? ? ? ? ? ? ? ? passWordSb.append(pointAt.getNum());
? ? ? ? ? ? ? ? ? ? ? ? // 賦值當前的point;
? ? ? ? ? ? ? ? ? ? ? ? currentPoint = pointAt;
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? invalidate();
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? case MotionEvent.ACTION_UP:// 當手指抬起的時候
? ? ? ? ? ? ? ? if (isVerify) {
? ? ? ? ? ? ? ? ? ? // 手勢密碼校驗
? ? ? ? ? ? ? ? ? ? // 清掉屏幕上所有的線,只畫上集合里面保存的線
? ? ? ? ? ? ? ? ? ? if (passWord.equals(passWordSb.toString())) {
? ? ? ? ? ? ? ? ? ? ? ? // 代表用戶繪制的密碼手勢與傳入的密碼相同
? ? ? ? ? ? ? ? ? ? ? ? callBack.checkedSuccess();
? ? ? ? ? ? ? ? ? ? } else {
? ? ? ? ? ? ? ? ? ? ? ? // 用戶繪制的密碼與傳入的密碼不同。
? ? ? ? ? ? ? ? ? ? ? ? callBack.checkedFail();
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? } else {
? ? ? ? ? ? ? ? ? ? callBack.onGestureCodeInput(passWordSb.toString());
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? default:
? ? ? ? ? ? ? ? break;
? ? ? ? }
? ? ? ? return true;
? ? }
? ? /**
? ? ?* 指定時間去清除繪制的狀態
? ? ?* @param delayTime 延遲執行時間
? ? ?*/
? ? public void clearDrawlineState(long delayTime) {
? ? ? ? if (delayTime > 0) {
? ? ? ? ? ? // 繪制紅色提示路線
? ? ? ? ? ? isDrawEnable = false;
? ? ? ? ? ? drawErrorPathTip();
? ? ? ? }
? ? ? ? new Handler().postDelayed(new clearStateRunnable(), delayTime);
? ? }
? ? /**
? ? ?* 清除繪制狀態的線程
? ? ?*/
? ? final class clearStateRunnable implements Runnable {
? ? ? ? public void run() {
? ? ? ? ? ? // 重置passWordSb
? ? ? ? ? ? passWordSb = new StringBuilder();
? ? ? ? ? ? // 清空保存點的集合
? ? ? ? ? ? lineList.clear();
? ? ? ? ? ? // 重新繪制界面
? ? ? ? ? ? clearScreenAndDrawList();
? ? ? ? ? ? for (GesturePoint p : list) {
? ? ? ? ? ? ? ? p.setPointState(SetGestureLockActivity.POINT_STATE_NORMAL);
? ? ? ? ? ? }
? ? ? ? ? ? invalidate();
? ? ? ? ? ? isDrawEnable = true;
? ? ? ? }
? ? }
? ? /**
? ? ?* 通過點的位置去集合里面查找這個點是包含在哪個Point里面的
? ? ?*
? ? ?* @param x
? ? ?* @param y
? ? ?* @return 如果沒有找到,則返回null,代表用戶當前移動的地方屬于點與點之間
? ? ?*/
? ? private GesturePoint getPointAt(int x, int y) {
? ? ? ? for (GesturePoint point : list) {
? ? ? ? ? ? // 先判斷x
? ? ? ? ? ? int leftX = point.getLeftX();
? ? ? ? ? ? int rightX = point.getRightX();
? ? ? ? ? ? if (!(x >= leftX && x < rightX)) {
? ? ? ? ? ? ? ? // 如果為假,則跳到下一個對比
? ? ? ? ? ? ? ? continue;
? ? ? ? ? ? }
? ? ? ? ? ? int topY = point.getTopY();
? ? ? ? ? ? int bottomY = point.getBottomY();
? ? ? ? ? ? if (!(y >= topY && y < bottomY)) {
? ? ? ? ? ? ? ? // 如果為假,則跳到下一個對比
? ? ? ? ? ? ? ? continue;
? ? ? ? ? ? }
? ? ? ? ? ? // 如果執行到這,那么說明當前點擊的點的位置在遍歷到點的位置這個地方
? ? ? ? ? ? return point;
? ? ? ? }
? ? ? ? return null;
? ? }
? ? private GesturePoint getBetweenCheckPoint(GesturePoint pointStart, GesturePoint pointEnd) {
? ? ? ? int startNum = pointStart.getNum();
? ? ? ? int endNum = pointEnd.getNum();
? ? ? ? String key = null;
? ? ? ? if (startNum < endNum) {
? ? ? ? ? ? key = startNum + "," + endNum;
? ? ? ? } else {
? ? ? ? ? ? key = endNum + "," + startNum;
? ? ? ? }
? ? ? ? return autoCheckPointMap.get(key);
? ? }
? ? /**
? ? ?* 清掉屏幕上所有的線,然后畫出集合里面的線
? ? ?*/
? ? private void clearScreenAndDrawList() {
? ? ? ? canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
? ? ? ? for (Pair<GesturePoint, GesturePoint> pair : lineList) {
? ? ? ? ? ? canvas.drawLine(pair.first.getCenterX(), pair.first.getCenterY(),
? ? ? ? ? ? ? ? ? ? pair.second.getCenterX(), pair.second.getCenterY(), paint);// 畫線
? ? ? ? }
? ? }
? ? /**
? ? ?* 校驗錯誤/兩次繪制不一致提示
? ? ?*/
? ? private void drawErrorPathTip() {
? ? ? ? canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
? ? ? ? paint.setColor(Color.rgb(255,0,0));// 設置默認線路顏色
? ? ? ? for (Pair<GesturePoint, GesturePoint> pair : lineList) {
? ? ? ? ? ? pair.first.setPointState(SetGestureLockActivity.POINT_STATE_WRONG);
? ? ? ? ? ? pair.second.setPointState(SetGestureLockActivity.POINT_STATE_WRONG);
? ? ? ? ? ? canvas.drawLine(pair.first.getCenterX(), pair.first.getCenterY(),
? ? ? ? ? ? ? ? ? ? pair.second.getCenterX(), pair.second.getCenterY(), paint);// 畫線
? ? ? ? }
? ? ? ? invalidate();
? ? }
? ? public interface GestureCallBack {
? ? ? ? /**
? ? ? ? ?* 用戶設置/輸入了手勢密碼
? ? ? ? ?*/
? ? ? ? public abstract void onGestureCodeInput(String inputCode);
? ? ? ? /**
? ? ? ? ?* 代表用戶繪制的密碼與傳入的密碼相同
? ? ? ? ?*/
? ? ? ? public abstract void checkedSuccess();
? ? ? ? /**
? ? ? ? ?* 代表用戶繪制的密碼與傳入的密碼不相同
? ? ? ? ?*/
? ? ? ? public abstract void checkedFail();
? ? }
}
~~~
**手勢密碼圖案提示代碼:**
~~~
public class LockIndicator extends View {
? ? private int numRow = 3; ? ?// 行
? ? private int numColum = 3; // 列
? ? private int patternWidth = 40;
? ? private int patternHeight = 40;
? ? private int f = 5;
? ? private int g = 5;
? ? private int strokeWidth = 3;
? ? private Paint paint = null;
? ? private Drawable patternNoraml = null;
? ? private Drawable patternPressed = null;
? ? private String lockPassStr; // 手勢密碼
? ? public LockIndicator(Context paramContext) {
? ? ? ? super(paramContext);
? ? }
? ? public LockIndicator(Context paramContext, AttributeSet paramAttributeSet) {
? ? ? ? super(paramContext, paramAttributeSet, 0);
? ? ? ? paint = new Paint();
? ? ? ? paint.setAntiAlias(true);
? ? ? ? paint.setStrokeWidth(strokeWidth);
? ? ? ? paint.setStyle(Paint.Style.STROKE);
? ? ? ? patternNoraml = getResources().getDrawable(R.mipmap.gesturepasswar_original);
? ? ? ? patternPressed = getResources().getDrawable(R.mipmap.gesturepassward_click);
? ? ? ? if (patternPressed != null) {
? ? ? ? ? ? patternWidth = patternPressed.getIntrinsicWidth();
? ? ? ? ? ? patternHeight = patternPressed.getIntrinsicHeight();
? ? ? ? ? ? this.f = (patternWidth / 4);
? ? ? ? ? ? this.g = (patternHeight / 4);
? ? ? ? ? ? patternPressed.setBounds(0, 0, patternWidth, patternHeight);
? ? ? ? ? ? patternNoraml.setBounds(0, 0, patternWidth, patternHeight);
? ? ? ? }
? ? }
? ? @Override
? ? protected void onDraw(Canvas canvas) {
? ? ? ? if ((patternPressed == null) || (patternNoraml == null)) {
? ? ? ? ? ? return;
? ? ? ? }
? ? ? ? // 繪制3*3的圖標
? ? ? ? for (int i = 0; i < numRow; i++) {
? ? ? ? ? ? for (int j = 0; j < numColum; j++) {
? ? ? ? ? ? ? ? paint.setColor(-16777216);
? ? ? ? ? ? ? ? int i1 = j * patternHeight + j * this.g;
? ? ? ? ? ? ? ? int i2 = i * patternWidth + i * this.f;
? ? ? ? ? ? ? ? canvas.save();
? ? ? ? ? ? ? ? canvas.translate(i1, i2);
? ? ? ? ? ? ? ? String curNum = String.valueOf(numColum * i + (j + 1));
? ? ? ? ? ? ? ? if (!TextUtils.isEmpty(lockPassStr)) {
? ? ? ? ? ? ? ? ? ? if (lockPassStr.indexOf(curNum) == -1) {
? ? ? ? ? ? ? ? ? ? ? ? // 未選中
? ? ? ? ? ? ? ? ? ? ? ? patternNoraml.draw(canvas);
? ? ? ? ? ? ? ? ? ? } else {
? ? ? ? ? ? ? ? ? ? ? ? // 被選中
? ? ? ? ? ? ? ? ? ? ? ? patternPressed.draw(canvas);
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? } else {
? ? ? ? ? ? ? ? ? ? // 重置狀態
? ? ? ? ? ? ? ? ? ? patternNoraml.draw(canvas);
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? canvas.restore();
? ? ? ? ? ? }
? ? ? ? }
? ? }
? ? @Override
? ? protected void onMeasure(int paramInt1, int paramInt2) {
? ? ? ? if (patternPressed != null)
? ? ? ? ? ? setMeasuredDimension(numColum * patternHeight + this.g
? ? ? ? ? ? ? ? ? ? * (-1 + numColum), numRow * patternWidth + this.f
? ? ? ? ? ? ? ? ? ? * (-1 + numRow));
? ? }
? ? /**
? ? ?* 請求重新繪制
? ? ?* @param paramString 手勢密碼字符序列
? ? ?*/
? ? public void setPath(String paramString) {
? ? ? ? lockPassStr = paramString;
? ? ? ? invalidate();
? ? }
}
~~~
**手勢密碼容器類代碼:**
~~~
public class GestureContentView extends ViewGroup {
? ? private int baseNum = 6;
? ? private int[] screenDispaly;
? ? /**
? ? ?* 每個點區域的寬度
? ? ?*/
? ? private int blockWidth;
? ? /**
? ? ?* 聲明一個集合用來封裝坐標集合
? ? ?*/
? ? private List<GesturePoint> list;
? ? private Context context;
? ? private boolean isVerify;
? ? private GestureDrawline gestureDrawline;
? ? /**
? ? ?* 包含9個ImageView的容器,初始化
? ? ?*
? ? ?* @param context
? ? ?* @param isVerify 是否為校驗手勢密碼
? ? ?* @param passWord 用戶傳入密碼
? ? ?* @param callBack 手勢繪制完畢的回調
? ? ?*/
? ? public GestureContentView(Context context, boolean isVerify, String passWord, GestureDrawline.GestureCallBack callBack) {
? ? ? ? super(context);
? ? ? ? screenDispaly = AppUtil.getScreenDispaly(context);
? ? ? ? blockWidth = screenDispaly[0] / 3;
? ? ? ? this.list = new ArrayList<GesturePoint>();
? ? ? ? this.context = context;
? ? ? ? this.isVerify = isVerify;
? ? ? ? // 添加9個圖標
? ? ? ? addChild();
? ? ? ? // 初始化一個可以畫線的view
? ? ? ? gestureDrawline = new GestureDrawline(context, list, isVerify, passWord, callBack);
? ? }
? ? private void addChild() {
? ? ? ? for (int i = 0; i < 9; i++) {
? ? ? ? ? ? ImageView image = new ImageView(context);
? ? ? ? ? ? image.setBackgroundResource(R.mipmap.gesturepassward_locus_round_original);
? ? ? ? ? ? this.addView(image);
? ? ? ? ? ? invalidate();
? ? ? ? ? ? // 第幾行
? ? ? ? ? ? int row = i / 3;
? ? ? ? ? ? // 第幾列
? ? ? ? ? ? int col = i % 3;
? ? ? ? ? ? // 定義點的每個屬性
? ? ? ? ? ? int leftX = col * blockWidth + blockWidth / baseNum;
? ? ? ? ? ? int topY = row * blockWidth + blockWidth / baseNum;
? ? ? ? ? ? int rightX = col * blockWidth + blockWidth - blockWidth / baseNum;
? ? ? ? ? ? int bottomY = row * blockWidth + blockWidth - blockWidth / baseNum;
? ? ? ? ? ? GesturePoint p = new GesturePoint(leftX, rightX, topY, bottomY, image, i + 1);
? ? ? ? ? ? this.list.add(p);
? ? ? ? }
? ? }
? ? public void setParentView(ViewGroup parent) {
? ? ? ? // 得到屏幕的寬度
? ? ? ? int width = screenDispaly[0];
? ? ? ? LayoutParams layoutParams = new LayoutParams(width, width);
? ? ? ? this.setLayoutParams(layoutParams);
? ? ? ? gestureDrawline.setLayoutParams(layoutParams);
? ? ? ? parent.addView(gestureDrawline);
? ? ? ? parent.addView(this);
? ? }
? ? @Override
? ? protected void onLayout(boolean changed, int l, int t, int r, int b) {
? ? ? ? for (int i = 0; i < getChildCount(); i++) {
? ? ? ? ? ? //第幾行
? ? ? ? ? ? int row = i / 3;
? ? ? ? ? ? //第幾列
? ? ? ? ? ? int col = i % 3;
? ? ? ? ? ? View v = getChildAt(i);
? ? ? ? ? ? v.layout(col * blockWidth + blockWidth / baseNum, row * blockWidth + blockWidth / baseNum,
? ? ? ? ? ? ? ? ? ? col * blockWidth + blockWidth - blockWidth / baseNum, row * blockWidth + blockWidth - blockWidth / baseNum);
? ? ? ? }
? ? }
? ? @Override
? ? protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
? ? ? ? super.onMeasure(widthMeasureSpec, heightMeasureSpec);
? ? ? ? // 遍歷設置每個子view的大小
? ? ? ? for (int i = 0; i < getChildCount(); i++) {
? ? ? ? ? ? View v = getChildAt(i);
? ? ? ? ? ? v.measure(widthMeasureSpec, heightMeasureSpec);
? ? ? ? }
? ? }
? ? /**
? ? ?* 保留路徑delayTime時間長
? ? ?*
? ? ?* @param delayTime
? ? ?*/
? ? public void clearDrawlineState(long delayTime) {
? ? ? ? gestureDrawline.clearDrawlineState(delayTime);
? ? }
}
~~~
源代碼下載地址:[http://download.csdn.net/detail/lxk_1993/9368281](http://download.csdn.net/detail/lxk_1993/9368281)
- 前言
- 面試中關于 layout_weight 筆記
- 關于安裝APK到Genymation 模擬器報 install failed cpu abi incompatible
- Error:Execution failed for task ':app:packageDebug'. > Duplicate files copied in APK META-INF/LICENS
- 仿QQ6.1手勢鎖
- 當expandlistview的getGroupView或者getChildView中包含checkbox時,前者點擊不可用
- 在genymotion官網下載genymotion-2.6.0-vbox.exe安裝完成模擬器不可用的問題解決
- android studio logcat 打印不出信息
- 去掉友盟分享里面不需要的平臺
- 在有EditText的界面 默認情況下不獲取焦點(不彈出輸入框)
- android studio 導入第三方庫的記錄
- Error:Execution failed for task ':app:transformClassesWithDexForDebug'解決記錄
- Edittext In Listview,當listview的item中有edittext時,怎么保存edittext的值?