标签:
公司项目中需要做一个手势密码,效果如图:
上面小的手势图与下面大的联动,效果:
由于没有合用的第三方,就只好自己写了一个,根据需求,下面的每连上一个点就需要通知上方小密码盘,做相应改变,并且绘制完成后,需要获取手势密码的内容,所以设计了如下接口:
public interface GesturesPasswordListener { /** * * @param list 绘制完成的密码 */ void getGesturesPassword(List<Integer> list); /** * * @param list 每绘制完成一个密码点,返回当前绘制密码 */ void changeGesturesPassword(List<Integer> list); }
public class GesturesPasswordView extends View
public void setListener(GesturesPasswordListener listener) { this.listener = listener; }
private GesturesPasswordListener listener = new GesturesPasswordListener() { @Override public void getGesturesPassword(List<Integer> list) { if (resoure == 0) { if (cur == 0) { if(list.size()<4){ initList = new ArrayList<Integer>(); small_gesture.setList(initList); sjh.setTextColor(Color.RED); sjh.setText("至少绘制四个点"); }else{ sjh.setTextColor(Color.parseColor("#d6d6d6")); sjh.setText("再次绘制解锁图案"); initList = new ArrayList<Integer>(); initList = list; jump.setVisibility(View.VISIBLE); jump.setText("重新输入"); cur = 1; } } else { if (initList.equals(list)) { SharePrefUtil.saveBoolean(SetGestureActivity.this, SharePrefUtil.GEST_SP_NAME, SharePrefUtil.IS_SAVEGESTURE, true); SharePrefUtil.saveObj(SetGestureActivity.this, SharePrefUtil.GEST_SP_NAME, SharePrefUtil.GESTURE, initList); SharePrefUtil.saveBoolean(SetGestureActivity.this, SharePrefUtil.GEST_SP_NAME, SharePrefUtil.EVER_LOGIN, true); Intent intent = new Intent(); intent.setClass(SetGestureActivity.this, HomeActivity.class); startActivity(intent); } else { sjh.setTextColor(Color.RED); sjh.setText("与上次输入不一致,请重新输入"); } } } } @Override public void changeGesturesPassword(List<Integer> list) { if (cur == 0) { small_gesture.setList(list); } } };
large_gesture.setListener(listener);
@Override public boolean onTouchEvent(MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: float dx = event.getX(); float dy = event.getY(); int dlocation = getLocation(dx, dy); if (dlocation != 0) { list.add(dlocation); if(listener != null) listener.changeGesturesPassword(list); postInvalidate(); } break; case MotionEvent.ACTION_MOVE: float mx = event.getX(); float my = event.getY(); int mlocation = getLocation(mx, my); if (mlocation != 0) { if (list.size() > 0) { if (mlocation != list.get(list.size() - 1)) { list.add(mlocation); if(listener != null) listener.changeGesturesPassword(list); } } else { list.add(mlocation); if(listener != null) listener.changeGesturesPassword(list); } } postInvalidate(); break; case MotionEvent.ACTION_UP: if(list.size() > 0){ if(listener != null) listener.getGesturesPassword(list); } list = new ArrayList<Integer>(); postInvalidate(); break; default: break; } return true; }
标签:
原文地址:http://blog.csdn.net/qq_18833399/article/details/45561239