Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> android 九宮格滑動解鎖開機實例源碼學習

android 九宮格滑動解鎖開機實例源碼學習

編輯:關於Android編程

效果圖由於網站占時不能上傳,以後補上。
NinePointLineView.java
復制代碼 代碼如下:
package org.demo.custon_view;
import org.demo.utils.MLog;
import android.content.Context;
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 NinePointLineView 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;
// 定義好9個點的數組
PointInfo[] points = new PointInfo[9];
// 相應ACTION_DOWN的那個點
PointInfo startPoint = null;
// 屏幕的寬高
int width, height;
// 當ACTION_MOVE時獲取的X,Y坐標
int moveX, moveY;
// 是否發生ACTION_UP
boolean isUp = false;
// 最終生成的用戶鎖序列
StringBuffer lockString = new StringBuffer();
public NinePointLineView(Context context) {
super(context);
this.setBackgroundColor(Color.WHITE);
initPaint();
}
public NinePointLineView(Context context, AttributeSet attrs) {
super(context, attrs);
this.setBackgroundColor(Color.WHITE);
initPaint();
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
MLog.i("onMeasure");
// 初始化屏幕大小
width = getWidth();
height = getHeight();
if (width != 0 && height != 0) {
initPoints(points);
}
MLog.i("width、height = " + width + "、" + height);
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
@Override
protected void onLayout(boolean changed, int left, int top, int right,
int bottom) {
MLog.i("onLayout");
super.onLayout(changed, left, top, right, bottom);
}
private int startX = 0, startY = 0;
@Override
protected void onDraw(Canvas canvas) {
canvas.drawText("用戶的滑動順序:" + lockString, 0, 40, textPaint);
if (moveX != 0 && moveY != 0 && startX != 0 && startY != 0) {
// 繪制當前活動的線段
drawLine(canvas, startX, startY, moveX, moveY);
}
drawNinePoint(canvas);
super.onDraw(canvas);
}
// 記住,這個DOWN和MOVE、UP是成對的,如果沒從UP釋放,就不會再獲得DOWN;
// 而獲得DOWN時,一定要確認消費該事件,否則MOVE和UP不會被這個View的onTouchEvent接收
@Override
public boolean onTouchEvent(MotionEvent event) {
boolean flag = true;
if (isUp) {// 如果已滑完,重置每個點的屬性和lockString
finishDraw();
// 當UP後,要返回false,把事件釋放給系統,否則無法獲得Down事件
flag = false;
} else {// 沒滑完,則繼續繪制
handlingEvent(event);
// 這裡要返回true,代表該View消耗此事件,否則不會收到MOVE和UP事件
flag = true;
}
return flag;
}
private void handlingEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_MOVE:
moveX = (int) event.getX();
moveY = (int) event.getY();
MLog.i("onMove:" + moveX + "、" + moveY);
for (PointInfo temp : points) {
if (temp.isInMyPlace(moveX, moveY) && temp.isNotSelected()) {
temp.setSelected(true);
startX = temp.getCenterX();
startY = temp.getCenterY();
int len = lockString.length();
if (len != 0) {
int preId = lockString.charAt(len - 1) - 48;
points[preId].setNextId(temp.getId());
}
lockString.append(temp.getId());
break;
}
}
invalidate(0, height - width, width, height);
break;
case MotionEvent.ACTION_DOWN:
int downX = (int) event.getX();
int downY = (int) event.getY();
MLog.i("onDown:" + downX + "、" + downY);
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:
MLog.i("onUp");
startX = startY = moveX = moveY = 0;
isUp = true;
invalidate();
break;
default:
MLog.i("收到其他事件!!");
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);
}
/**
* 初始化文本畫筆
* @param paint
*/
private void initTextPaint(Paint paint) {
textPaint.setTextSize(30);
textPaint.setAntiAlias(true);
textPaint.setTypeface(Typeface.MONOSPACE);
}
/**
* 初始化黑線畫筆
*
* @param paint
*/
private void initLinePaint(Paint paint) {
paint.setColor(Color.GRAY);
paint.setStrokeWidth(defaultBitmap.getWidth());
paint.setAntiAlias(true);
paint.setStrokeCap(Cap.ROUND);
}
/**
* 初始化白線畫筆
*
* @param paint
*/
private void initWhiteLinePaint(Paint paint) {
paint.setColor(Color.WHITE);
paint.setStrokeWidth(defaultBitmap.getWidth() - 5);
paint.setAntiAlias(true);
paint.setStrokeCap(Cap.ROUND);
}
/**
* 繪制已完成的部分
*
* @param canvas
*/
private void drawNinePoint(Canvas canvas) {
if (startPoint != null) {
drawEachLine(canvas, startPoint);
}
// 繪制每個點的圖片
for (PointInfo pointInfo : points) {
if (pointInfo.isSelected()) {// 繪制大圈
canvas.drawBitmap(selectedBitmap, pointInfo.getSeletedX(),
pointInfo.getSeletedY(), null);
}
// 繪制點
canvas.drawBitmap(defaultBitmap, pointInfo.getDefaultX(),
pointInfo.getDefaultY(), null);
}
}
/**
* 遞歸繪制每兩個點之間的線段
*
* @param canvas
* @param point
*/
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]);
}
}
/**
* 先繪制黑線,再在上面繪制白線,達到黑邊白線的效果
*
* @param canvas
* @param startX
* @param startY
* @param stopX
* @param stopY
*/
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);
}
/**
* 用來表示一個點
*
* @author zkwlx
*
*/
private class PointInfo {
// 一個點的ID
private int id;
// 當前點所指向的下一個點的ID,當沒有時為自己ID
private int nextId;
// 是否被選中
private boolean selected;
// 默認時圖片的左上角X坐標
private int defaultX;
// 默認時圖片的左上角Y坐標
private int defaultY;
// 被選中時圖片的左上角X坐標
private int seletedX;
// 被選中時圖片的左上角Y坐標
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 boolean isNotSelected() {
return !isSelected();
}
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;
}
/**
* 坐標(x,y)是否在當前點的范圍內
*
* @param x
* @param y
* @return
*/
public boolean isInMyPlace(int x, int y) {
boolean inX = x > seletedX
&& x < (seletedX + selectedBitmapDiameter);
boolean inY = y > seletedY
&& y < (seletedY + selectedBitmapDiameter);
return (inX && inY);
}
}
}

NinePointView.java
復制代碼 代碼如下:
package org.demo.custon_view;
import org.demo.utils.MLog;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Paint.Cap;
import android.graphics.Path;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
public class NinePointView extends View {
Paint linePaint = new Paint();
Paint textPaint = new Paint();
Path path = new Path();
// 由於兩個圖片都是正方形,所以獲取一個長度就行了
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;
// 初始化指示器的圖片
Bitmap indicateBitmap = BitmapFactory.decodeResource(getResources(),
R.drawable.indicator_lock_area_next);
Bitmap tempBitmap = null;
// 定義好9個點的數組
PointInfo[] points = new PointInfo[9];
// 屏幕的寬高
int width, height;
// 當ACTION_MOVE時獲取的X,Y坐標
int moveX, moveY;
// 是否發生ACTION_UP
boolean isUp = false;
// 最終生成的用戶鎖序列
StringBuffer lockString = new StringBuffer();
Matrix matrix = new Matrix();
public NinePointView(Context context) {
super(context);
this.setBackgroundColor(Color.WHITE);
initLinePaint(linePaint);
initTextPaint(textPaint);
}
public NinePointView(Context context, AttributeSet attrs) {
super(context, attrs);
this.setBackgroundColor(Color.WHITE);
initLinePaint(linePaint);
initTextPaint(textPaint);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
MLog.i("onMeasure");
width = getWidth();
height = getHeight();
if (width != 0 && height != 0) {
initPoints(points);
}
MLog.i("width、height = " + width + "、" + height);
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
@Override
protected void onLayout(boolean changed, int left, int top, int right,
int bottom) {
MLog.i("onLayout");
super.onLayout(changed, left, top, right, bottom);
}
private int startX = 0, startY = 0;
@Override
protected void onDraw(Canvas canvas) {
canvas.drawText("用戶的滑動順序:" + lockString, 0, 40, textPaint);
if (moveX != 0 && moveY != 0 && startX != 0 && startY != 0) {
// 繪制當前活動的線段
canvas.drawLine(startX, startY, moveX, moveY, linePaint);
}
drawNinePoint(canvas, linePaint);
super.onDraw(canvas);
}
// 記住,這個DOWN和MOVE、UP是成對的,如果沒從UP釋放,就不會再獲得DOWN;
// 而獲得DOWN時,一定要確認消費該事件,否則MOVE和UP不會被這個VIEW的onTouchEvent接收
@Override
public boolean onTouchEvent(MotionEvent event) {
boolean flag = true;
if (isUp) {// 如果已滑完,則把整個Canvas重置
finishDraw();
// 當UP後,要返回false,把事件釋放給系統,否則無法獲得Down事件
flag = false;
} else {// 沒滑完,則繼續繪制
handlingEvent(event);
// 這裡要返回true,否則代表該View不消耗此事件,交給系統處理,則不會再收到MOVE和UP事件
flag = true;
}
return flag;
}
private void handlingEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_MOVE:
moveX = (int) event.getX();
moveY = (int) event.getY();
MLog.i("onMove:" + moveX + "、" + moveY);
for (PointInfo temp : points) {
if (temp.isInMyPlace(moveX, moveY) && temp.isNotSelected()) {
temp.setSelected(true);
startX = temp.getCenterX();
startY = temp.getCenterY();
int len = lockString.length();
if (len != 0) {
int preId = lockString.charAt(len - 1) - 48;
points[preId].setNextId(temp.getId());
}
lockString.append(temp.getId());
break;
}
}
invalidate(0, height - width, width, height);
break;
case MotionEvent.ACTION_DOWN:
int downX = (int) event.getX();
int downY = (int) event.getY();
MLog.i("onDown:" + downX + "、" + downY);
for (PointInfo temp : points) {
if (temp.isInMyPlace(downX, downY)) {
temp.setSelected(true);
startX = temp.getCenterX();
startY = temp.getCenterY();
lockString.append(temp.getId());
break;
}
}
invalidate(0, height - width, width, height);
break;
case MotionEvent.ACTION_UP:
MLog.i("onUp");
startX = startY = moveX = moveY = 0;
isUp = true;
invalidate();
break;
default:
MLog.i("收到其他事件!!");
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 initTextPaint(Paint paint) {
textPaint.setTextSize(30);
textPaint.setAntiAlias(true);
textPaint.setTypeface(Typeface.MONOSPACE);
}
/**
* 初始化線畫筆
*
* @param paint
*/
private void initLinePaint(Paint paint) {
paint.setColor(Color.GRAY);
paint.setStrokeWidth(defaultBitmap.getWidth());
paint.setAntiAlias(true);
paint.setStrokeCap(Cap.ROUND);
}
/**
* 繪制已完成的部分
*
* @param canvas
*/
private void drawNinePoint(Canvas canvas, Paint paint) {
// 先把用戶畫出的線繪制好
for (PointInfo pointInfo : points) {
if (pointInfo.hasNextId()) {
int n = pointInfo.getNextId();
canvas.drawLine(pointInfo.getCenterX(), pointInfo.getCenterY(),
points[n].getCenterX(), points[n].getCenterY(), paint);
}
}
// 繪制每個點的圖片
for (PointInfo pointInfo : points) {
if (pointInfo.isSelected()) {
if (pointInfo.hasNextId()) {
matrix.reset();
int i = (int) Math.abs(Math.random() * 1000 - 640);
MLog.i("隨機到的角度:" + i);
matrix.setRotate(i);
tempBitmap = Bitmap.createBitmap(indicateBitmap, 0, 0,
indicateBitmap.getWidth(),
indicateBitmap.getHeight(), matrix, false);
canvas.drawBitmap(tempBitmap, pointInfo.getSeletedX(),
pointInfo.getSeletedY(), paint);
} else {
canvas.drawBitmap(selectedBitmap, pointInfo.getSeletedX(),
pointInfo.getSeletedY(), paint);
}
}
canvas.drawBitmap(defaultBitmap, pointInfo.getDefaultX(),
pointInfo.getDefaultY(), paint);
}
}
private class PointInfo {
// 一個點的ID
private int id;
// 當前點所指向的下一個點的ID,當沒有時為自己ID
private int nextId;
// 是否被選中
private boolean selected;
// 默認時圖片的左上角X坐標
private int defaultX;
// 默認時圖片的左上角Y坐標
private int defaultY;
// 被選中時圖片的左上角X坐標
private int seletedX;
// 被選中時圖片的左上角Y坐標
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 boolean isNotSelected() {
return !isSelected();
}
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;
}
/**
* 坐標(x,y)是否在當前點的范圍內
*
* @param x
* @param y
* @return
*/
public boolean isInMyPlace(int x, int y) {
boolean inX = x > seletedX
&& x < (seletedX + selectedBitmapDiameter);
boolean inY = y > seletedY
&& y < (seletedY + selectedBitmapDiameter);
if (inX && inY) {
return true;
} else {
return false;
}
}
}
}
  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved