Android 项目开发实战:图案解锁

第一个窗体:

MainActivity.java:

package com.example.screenlock;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//setContentView(new GestureLock(this));
//跳转到设置密码窗体
Button btn_setting=(Button) findViewById(R.id.button1);
   Button btn_lock=(Button) findViewById(R.id.button2);
   btn_setting.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//向另一个窗体跳转
Intent intent=new Intent(MainActivity.this,SettingActivity.class);
startActivity(intent);
}
});
   btn_lock.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//向另一个窗体跳转
Intent intent=new Intent(MainActivity.this,LockMainActivity.class);
startActivity(intent);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}

第二个窗体:


SettingActivity.java:

package com.example.screenlock;


import android.os.Bundle;
import android.app.Activity;
import android.content.SharedPreferences;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;


public class SettingActivity extends Activity {
    int a[]=new int[10];
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_setting);
Button save=(Button) findViewById(R.id.btn_save);
   Button clear=(Button) findViewById(R.id.btn_clear);
   final GestureLock lock=(GestureLock) findViewById(R.id.gestureLock1);
   
   lock.setonDrawFinishedListener(new GestureLock.onDrawFinishedListener() {

@Override
public boolean onDrawFinished(int[] a) {

if(a.length<3){
Toast.makeText(SettingActivity.this, "密码不能少于3个点", Toast.LENGTH_LONG).show();

   return false;
}
else{
SettingActivity.this.a=a;

return true;
}
}
});
   save.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
//保存密码
 if (a != null)  
               {  
                   String sb=null;  
                   for (int i=0;i<a.length;++i)  
                   {  
                       sb+=""+a[i];  
                   }  
                   SharedPreferences sp = SettingActivity.this.getSharedPreferences("password", SettingActivity.this.MODE_PRIVATE);  
                   SharedPreferences.Editor editor = sp.edit();  
                   editor.putString("password", sb.toString());  
                   editor.commit();  
 
                   Toast.makeText(SettingActivity.this, "保存完成", Toast.LENGTH_SHORT).show();  
               }
}
});
   clear.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
//重置
lock.resetPoints();
}
});
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.setting, menu);
return true;
}
}

第三个窗体:


LockMainActivity.java:

package com.example.screenlock;
import com.example.screenlock.GestureLock.onDrawFinishedListener;
import android.os.Bundle;
import android.app.Activity;
import android.content.SharedPreferences;
import android.view.Menu;
import android.widget.Toast;
public class LockMainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_lock_main);
SharedPreferences sp = getSharedPreferences("password", this.MODE_PRIVATE);  
        final String password = sp.getString("password", "");  
  
        GestureLock lock = (GestureLock)findViewById(R.id.gestureLock1);  
        lock.setonDrawFinishedListener(new onDrawFinishedListener() {  
            @Override  
            public boolean onDrawFinished(int[] a) {  
                String sb = null;  
                for (int i=0;i<a.length;++i)  
                {  
                    sb+=""+a[i];  
                }  
                if (sb.toString().equals(password)){  
                    Toast.makeText(LockMainActivity.this, "正确", Toast.LENGTH_SHORT).show();  
                    return true;  
                }  
                else  
                {  
                    Toast.makeText(LockMainActivity.this, "错误", Toast.LENGTH_SHORT).show();  
                    return false;  
                }  
            }


 
        });  
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.lock_main, menu);
return true;
}
}

另外的两个类:

Point.java:

package com.example.screenlock;
public class Point {
//当前点的xy值
float x;
float y;
//三种状态:正常、选择、错误
public static int STATE_NORMAL=0;
public static int STATE_PRESS=1;
public static int STATE_ERROR=2;
int state=STATE_NORMAL;
public Point(float x,float y){
this.x=x;
this.y=y;
}
public float distance(Point pMouse) {
float distance =(float)Math.sqrt((x-pMouse.x)*(x-pMouse.x)+(y-pMouse.y)*(y-pMouse.y));
return distance;
}
}



这是一个自定义控件

GestureLock.java:

package com.example.screenlock;
import java.util.ArrayList;
import java.util.List;
import android.R.integer;
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.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;


public class GestureLock extends View {
private onDrawFinishedListener listener;
//放置所有的点
private Point[][]points=new Point[3][3];
//初始化标志
private boolean inited=false;

private int[]a=new int[10];
int t=0; 

//三个图片
private Bitmap bitmapPointError;
private Bitmap bitmapPointNormal;
private Bitmap bitmapPointPress;
//将选中的一个序列存到里面
private ArrayList<integer>passList=new ArrayList<integer>();
//图片的半径
private float bitmapR;
//一个按下的画笔,一个错误的画笔
Paint pressPaint=new Paint();
Paint errorPaint=new Paint();
//将按下并且没按过的点放在一个集合中
private ArrayList<Point>pointList=new ArrayList<Point>();
//不在绘制状态
private boolean isDraw=false;
Paint paint=new Paint(Paint.ANTI_ALIAS_FLAG);
//按下时鼠标点击的位置
    float mouseX,mouseY;

public GestureLock(Context context) {
super(context);
}
public GestureLock(Context context,AttributeSet attrs){
super(context,attrs);
}
public GestureLock(Context context,AttributeSet attrs,int defStyle){
super(context,attrs,defStyle);
}

@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//如果没有初始化就初始化。
if(!inited){
Init();
}
drawPoints(canvas);
//如果list中有点
if(pointList.size()>0){
//得到他的起点
Point a=pointList.get(0);

for(int i=1;i<pointList.size();++i){
//得到另一个点
Point b=pointList.get(i);
//a、b两点连线
drawLine(canvas, a, b);
//起点不断地变化
a=b;
}if(isDraw){
drawLine(canvas, a, new Point(mouseX, mouseY));
}
}
}
private void drawLine(Canvas canvas,Point a,Point b){
if(a.state==Point.STATE_PRESS){
canvas.drawLine(a.x, a.y, b.x, b.y,pressPaint);
}
else if(a.state==Point.STATE_ERROR){
canvas.drawLine(a.x, a.y, b.x, b.y,errorPaint);
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
mouseX=event.getX();
mouseY=event.getY();
int []ij;
int i;
int j;
switch(event.getAction()){
case MotionEvent.ACTION_DOWN:
//在 再一次触摸的时候,重置
resetPoints();
ij=getSelectedPoint();
if(ij!=null){
isDraw=true;
i=ij[0];
j=ij[1];
points[i][j].state=Point.STATE_PRESS;
   pointList.add(points[i][j]);
   a[++t]=i*3+j;
}
break;
case MotionEvent.ACTION_MOVE:
if(isDraw){
ij=getSelectedPoint();
if(ij!=null){
i=ij[0];
j=ij[1];
if(!pointList.contains(points[i][j])){
points[i][j].state=Point.STATE_PRESS;
pointList.add(points[i][j]);
//将一个二维数组转化成一位数组
a[++t]=i*3+j;
}
}
}
break;
case MotionEvent.ACTION_UP:
boolean valid=false;
if(listener!=null&& isDraw){
valid=listener.onDrawFinished(a);
}if(!valid){
for(Point p:pointList){
p.state=Point.STATE_ERROR;
}
}
isDraw=false;
break;
}
//及时的更新,重新绘制
this.postInvalidate();
return true;
}

private int[]getSelectedPoint(){
Point pMouse=new Point(mouseX,mouseY);
for(int i=0;i<points.length;++i){
for(int j=0;j<points[i].length;++j){
if(points[i][j].distance(pMouse)<bitmapR){
int []result=new int[2];
result[0]=i;
result[1]=j;
return result;
}
}
}
return null;
}
private void drawPoints(Canvas canvas){
for(int i=0;i<points.length;++i){
for(int j=0;j<points[i].length;++j){
if(points[i][j].state==Point.STATE_NORMAL){
//Normal
canvas.drawBitmap(bitmapPointNormal, points[i][j].x-bitmapR,points[i][j].y-bitmapR, paint);
}
else if(points[i][j].state==Point.STATE_PRESS){
//PRESS
canvas.drawBitmap(bitmapPointPress, points[i][j].x-bitmapR,points[i][j].y-bitmapR, paint);
}
else if(points[i][j].state==Point.STATE_ERROR){
//error
canvas.drawBitmap(bitmapPointError, points[i][j].x-bitmapR,points[i][j].y-bitmapR, paint);
}
}
}
}

private void Init(){
pressPaint.setColor(Color.YELLOW);
pressPaint.setStrokeWidth(5);
errorPaint.setColor(Color.RED);
errorPaint.setStrokeWidth(5);
bitmapPointError=BitmapFactory.decodeResource(getResources(), R.drawable.error);
bitmapPointNormal=BitmapFactory.decodeResource(getResources(), R.drawable.normal);
bitmapPointPress=BitmapFactory.decodeResource(getResources(), R.drawable.press);
bitmapR=bitmapPointNormal.getHeight()/2;
int width=getWidth();
int height=getHeight();
int offset=Math.abs(width-height)/2;
int offsetX,offsetY;
int space;
//横屏、竖屏,竖直的方向始终是height
//小方格的边长
//横屏
if(width>height){
space=height/4;
offsetX=offset;
offsetY=0;
}else{
space=width/4;
offsetX=0;
offsetY=offset;
}
//对每个点进行赋值
//第一行的点
points[0][0]=new Point(offsetX+space,offsetY+space);
points[0][1]=new Point(offsetX+space*2,offsetY+space);
points[0][2]=new Point(offsetX+space*3,offsetY+space);

points[1][0]=new Point(offsetX+space,offsetY+space*2);
points[1][1]=new Point(offsetX+space*2,offsetY+space*2);
points[1][2]=new Point(offsetX+space*3,offsetY+space*2);
   
points[2][0]=new Point(offsetX+space,offsetY+space*3);
points[2][1]=new Point(offsetX+space*2,offsetY+space*3);
points[2][2]=new Point(offsetX+space*3,offsetY+space*3);
//执行过该方法后,就说明已经初始化了
inited=true;
}
//触摸结束后,调用重置的方法
public void resetPoints(){
t=0;
pointList.clear();
for(int i=0;i<points.length;++i){
for(int j=0;j<points[i].length;++j){
points[i][j].state=Point.STATE_NORMAL;
}
}
this.postInvalidate();
}
//将选中的一个序列放进来
public interface onDrawFinishedListener{
boolean onDrawFinished(int a[]);
}
public void setonDrawFinishedListener(onDrawFinishedListener listener){
this.listener=listener;
}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值