九宮格手勢密碼案例
九宮格手勢密碼使用自定View實作,例:
package com.example.getturepwdline;
import com.example.util.CommonUtil;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Cap;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
public class PointNineView extends View{
Paint linePaint = new Paint();
Paint whiteLinePaint = new Paint();
Paint textPaint = new Paint();
Bitmap defaultBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.lock);
int defaultBitmapRadius = defaultBitmap.getWidth() / 2;
Bitmap selectedBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.indicator_lock_area);
int selectedBitmapDiameter = selectedBitmap.getWidth();
int selectedBitmapRadius = selectedBitmapDiameter / 2;
PointInfo[] points = new PointInfo[9];
PointInfo startPoint = null;
int width,height;
int moveX,moveY;
private int startX = 0,startY = 0;
boolean isUP = false;
Context cxt;
StringBuffer lockString = new StringBuffer();
public PointNineView(Context context) {
super(context);
cxt = context;
initPaint();
}
public PointNineView(Context context,AttributeSet attrs) {
super(context, attrs);
initPaint();
}
public PointNineView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
initPaint();
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
width = getWidth();
height = getHeight();
if (width !=0 && height != 0) {
//初始化一個九宮格
initPoints(points);
}
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
@Override
protected void onLayout(boolean changed, int left, int top, int right,
int bottom) {
super.onLayout(changed, left, top, right, bottom);
}
@Override
protected void onDraw(Canvas canvas) {
if (moveX != 0 && moveY != 0 && startX != 0 && startY != 0) {
}
drawNinePoint(canvas);
super.onDraw(canvas);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
boolean flag = true;
if (isUP) {
finishDraw();
flag= false;
}
else{
handlingEvent(event);
flag = true;
}
return flag;
}
private void handlingEvent(MotionEvent event){
//MotionEven手勢事件
switch (event.getAction()) {
case MotionEvent.ACTION_MOVE: //移動事件
moveX = (int)event.getX();
moveY = (int)event.getY();
for (PointInfo temp: points) {
if (temp.isInMyPlace(moveX, moveY) && temp.isSelected() == false) {
temp.setSelected(true);
startX = temp.getCenterX();
startY = temp.getCenterY();
int len = lockString.length();
if (len != 0) {
char charTemp = lockString.charAt(len - 1);
int preId = charTemp - 48;
points[preId].setNextId(temp.getId());
}
lockString.append(temp.getId());
break;
}
}
//重新整理View ,左、上、右、下
invalidate(0,height - width,width,height);
break;
//按下事件
case MotionEvent.ACTION_DOWN:
int downX = (int) event.getX();
int downY = (int) event.getY();
for (PointInfo temp : points) {
if (temp.isInMyPlace(downX, downY)) {
temp.setSelected(true);
startPoint = temp;
startX = temp.getCenterX();
startY = temp.getCenterY();
lockString.append(temp.getId());
break;
}
}
invalidate(0, height - width, width, height);
break;
//擡起事件
case MotionEvent.ACTION_UP:
startX = startY = moveX = moveY = 0;
isUP = true;
invalidate();
savePwd();
break;
default:
break;
}
}
private void finishDraw(){
for (PointInfo temp : points) {
temp.setSelected(false);
temp.setNextId(temp.getId());
}
lockString.delete(0, lockString.length());
isUP = false;
invalidate();
}
private void initPoints(PointInfo[] points){
int len = points.length;
int seletedSpacing = (width - selectedBitmapDiameter * 3) / 4;
int seletedX = seletedSpacing;
int seletedY = height - width + seletedSpacing;
int defaultX = seletedX + selectedBitmapRadius - defaultBitmapRadius;
int defaultY = seletedY + selectedBitmapRadius - defaultBitmapRadius;
for (int i = 0; i < len; i++) {
if (i == 3 || i == 6) {
seletedX = seletedSpacing;
seletedY += selectedBitmapDiameter + seletedSpacing;
defaultX = seletedX + selectedBitmapRadius
- defaultBitmapRadius;
defaultY += selectedBitmapDiameter + seletedSpacing;
}
points[i] = new PointInfo(i, defaultX, defaultY, seletedX, seletedY);
seletedX += selectedBitmapDiameter + seletedSpacing;
defaultX += selectedBitmapDiameter + seletedSpacing;
}
}
private void initPaint(){
initLinePaint(linePaint);
initTextPaint(textPaint);
initWhiteLinePaint(whiteLinePaint);
}
private void initLinePaint(Paint paint){
paint.setColor(Color.GRAY);
//設定兩個點之間的線的背景的寬度
paint.setStrokeWidth(30);
paint.setAntiAlias(true);
//畫筆筆刷類型
paint.setStrokeCap(Cap.ROUND);
//防拌動
//paint.setDither(true);
}
private void initTextPaint(Paint paint){
paint.setTextSize(30);;
paint.setAntiAlias(true);
//設定字型樣式,monospace字型
paint.setTypeface(Typeface.MONOSPACE);
}
private void initWhiteLinePaint(Paint paint){
paint.setColor(Color.WHITE);
paint.setStrokeWidth(20);//設定兩個點之間的線的寬度
paint.setAntiAlias(true);
paint.setStrokeCap(Cap.ROUND);
}
private void drawNinePoint(Canvas canvas){
if (startPoint != null) {
drawEachLine(canvas, startPoint);
}
for (PointInfo pointInfo : points) {
if (pointInfo != null) {
if (pointInfo.isSelected()) {
canvas.drawBitmap(selectedBitmap, pointInfo.getSeletedX(),pointInfo.getSeletedY(), null);
}
canvas.drawBitmap(defaultBitmap, pointInfo.getDefaultX(), pointInfo.getDefaultY(),null);
}
}
}
private void drawEachLine(Canvas canvas, PointInfo point) {
if (point.hasNextId()) {
int n = point.getNextId();
drawLine(canvas,point.getCenterX(),point.getCenterY(),points[n].getCenterX(),points[n].getCenterY());
drawEachLine(canvas, points[n]);
}
}
private void drawLine(Canvas canvas,float startX,float startY,float stopX,float stopY){
canvas.drawLine(startX, startY, stopX, stopY, linePaint);
canvas.drawLine(startX, startY, stopX, stopY, whiteLinePaint);
}
private class PointInfo{
private int id;
private int nextId;
private boolean selected;
private int defaultX;
private int defaultY;
private int seletedX;
private int seletedY;
public PointInfo(int id, int defaultX,int defaultY, int seletedX, int seletedY) {
this.id = id;
this.nextId = id;
this.defaultX = defaultX;
this.defaultY = defaultY;
this.seletedX = seletedX;
this.seletedY = seletedY;
}
public boolean isSelected() {
return selected;
}
public void setSelected(boolean selected) {
this.selected = selected;
}
public int getId() {
return id;
}
public int getDefaultX() {
return defaultX;
}
public int getDefaultY() {
return defaultY;
}
public int getSeletedX() {
return seletedX;
}
public int getSeletedY() {
return seletedY;
}
public int getCenterX() {
return seletedX + selectedBitmapRadius;
}
public int getCenterY() {
return seletedY + selectedBitmapRadius;
}
public boolean hasNextId() {
return nextId != id;
}
public int getNextId() {
return nextId;
}
public void setNextId(int nextId) {
this.nextId = nextId;
}
public boolean isInMyPlace(int x, int y) {
boolean inX = x > seletedX && x < (seletedX + selectedBitmapDiameter);
boolean inY = y > seletedY && y < (seletedY + selectedBitmapDiameter);
return (inX && inY);
}
}
public String getPwd(){
return lockString.toString();
}
public void savePwd(){
Intent intent = new Intent();
//SharedPreferences shareDate = cxt.getSharedPreferences("GUE_PWD", Activity.MODE_PRIVATE);
//是否第一次設定密碼
//boolean isSetFirst = shareDate.getBoolean("IS_SET_FIRST", false);
boolean isSetFirst = CommonUtil.getIsSetFirst(cxt);
//如果第一次已經設定密碼,驗證第二次和第一次是否一緻
if (isSetFirst) {
String pwd = this.getPwd();
//得到第一次設定的密碼
//String first_pwd = shareDate.getString("FIRST_PWD", "NO HAVE PWD");
String first_pwd = CommonUtil.getFirstPwd(cxt);
//第二次密碼和第一次密碼一樣 設定成功
if (pwd.equals(first_pwd)) {
//shareDate.edit().clear().commit();
//shareDate.edit().putBoolean("IS_SET", true).commit();
//shareDate.edit().putString("GUE_PWD", pwd).commit();
//CommonUtil.clearData(cxt);
CommonUtil.saveIsSetFirst(cxt, true);
CommonUtil.saveFistPwd(cxt, pwd);
intent.setClass(cxt,SetPwdActivity.class);
}
//第二次輸入的密碼和第一次輸入的密碼不一緻
else{
//shareDate.edit().putBoolean("SECOND_ERROR", true).commit();
CommonUtil.saveSecondError(cxt, true);
intent.setClass(cxt, MainActivity.class);
}
}//第一次設定手勢密碼
else{
//shareDate.edit().clear().commit();
//shareDate.edit().putString("FIRST_PWD", this.getPwd()).commit();
//shareDate.edit().putBoolean("IS_SET_FIRST", true).commit();
//-------------
//CommonUtil.clearData(cxt);
CommonUtil.saveFistPwd(cxt, this.getPwd());
CommonUtil.saveIsSetFirst(cxt, true);
intent.setClass(cxt, MainActivity.class);
}
cxt.startActivity(intent);
((Activity)cxt).finish();
}
}
Javadoc文檔注釋
如何使用Eclipse生成Javadoc
1. 首先,選中Java目标工程,點選下拉菜單File,選中Export,會彈出Export對話框。
2. 選中“Javadoc”,點選Next。
3. Javadoc command輸入框指定javadoc.exe所在路徑。這裡,Javadoc的對象既可以是整個工程,也可以是部分package或class。根據需要,選擇适合的類型。Destination:這個路徑是我們存放生成後的文檔的路徑。點選Finish。
具體實作請看附件......
注:附件中還有不同樣式手勢操作