下载路径:http://download.csdn.net/detail/menglele1314/8800209
package com.activity;
import android.app.Activity;
import android.os.Bundle;
import android.text.Html;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.FrameLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.view.GestureContentView;
import com.view.GestureDrawline.GestureCallBack;
import com.view.LockIndicator;
/**
*
* 手势密码设置界面
*
*/
public class GestureEditActivity extends Activity implements OnClickListener {
private TextView mTextCancel;
private LockIndicator mLockIndicator;
private TextView mTextTip;
private FrameLayout mGestureContainer;
private GestureContentView mGestureContentView;
private TextView mTextReset;
private boolean mIsFirstInput = true;
private String mFirstPassword = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gesture_edit);
setUpViews();
setUpListeners();
}
private void setUpViews() {
mTextCancel = (TextView) findViewById(R.id.text_cancel);
mTextReset = (TextView) findViewById(R.id.text_reset);
mTextReset.setClickable(false);
mLockIndicator = (LockIndicator) findViewById(R.id.lock_indicator);
mTextTip = (TextView) findViewById(R.id.text_tip);
mGestureContainer = (FrameLayout) findViewById(R.id.gesture_container);
// 初始化一个显示各个点的viewGroup
/**
* 1 2 3
* 4 5 6
* 7 8 9
*/
mGestureContentView = new GestureContentView(this, false, "",// ""这里指的是密码字符串
new 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);
mTextReset.setClickable(true);
mTextReset.setText("重新设置手势密码");
} else {
if (inputCode.equals(mFirstPassword)) {
MainActivity.str = mFirstPassword;// 绘制好的密码
Toast.makeText(GestureEditActivity.this,
"设置成功", Toast.LENGTH_SHORT).show();
mGestureContentView.clearDrawlineState(0L);
GestureEditActivity.this.finish();
MainActivity.off = true;
} else {
mTextTip.setText(Html
.fromHtml("<font color=‘#c70c1e‘>与上一次绘制不一致,请重新绘制</font>"));
// 左右移动动画
Animation shakeAnimation = AnimationUtils
.loadAnimation(
GestureEditActivity.this,
R.anim.shake);
mTextTip.startAnimation(shakeAnimation);
// 保持绘制的线,1.5秒后清除
mGestureContentView.clearDrawlineState(100L);
}
}
mIsFirstInput = false;
}
@Override
public void checkedSuccess() {
}
@Override
public void checkedFail() {
}
});
// 设置手势解锁显示到哪个布局里面
mGestureContentView.setParentView(mGestureContainer);
updateCodeList("");
}
private void setUpListeners() {
mTextCancel.setOnClickListener(this);
mTextReset.setOnClickListener(this);
}
private void updateCodeList(String inputCode) {
// 更新选择的图案
mLockIndicator.setPath(inputCode);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.text_cancel:
this.finish();
break;
case R.id.text_reset:
mIsFirstInput = true;
updateCodeList("");
mTextTip.setText("设置手势密码,防止他人未经授权查看");
break;
default:
break;
}
}
private boolean isInputPassValidate(String inputPassword) {
if (TextUtils.isEmpty(inputPassword) || inputPassword.length() < 4) {
return false;
}
return true;
}
}
package com.activity;
import android.app.Activity;
import android.os.Bundle;
import android.text.Html;
import android.text.TextUtils;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.FrameLayout;
import android.widget.TextView;
import com.view.GestureContentView;
import com.view.GestureDrawline.GestureCallBack;
/**
*
* 手势绘制/校验界面
*
*/
public class GestureVerifyActivity extends Activity implements
android.view.View.OnClickListener {
private TextView mTextCancel;
private TextView mTextTip;
private FrameLayout mGestureContainer;
private GestureContentView mGestureContentView;
private TextView mTextForget;
private TextView mTextOther;
private TextView phone;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gesture_verify);
setUpViews();
}
private void setUpViews() {
mTextCancel = (TextView) findViewById(R.id.text_cancel);
mTextCancel.setOnClickListener(this);
mTextTip = (TextView) findViewById(R.id.text_tip);
mGestureContainer = (FrameLayout) findViewById(R.id.gesture_container);
mTextForget = (TextView) findViewById(R.id.text_forget_gesture);
mTextForget.setOnClickListener(this);
mTextOther = (TextView) findViewById(R.id.text_other_account);
mTextOther.setOnClickListener(this);
phone = (TextView) findViewById(R.id.text_phone_number);
phone.setText(getProtectedMobile("15854265825"));
// 初始化一个显示各个点的viewGroup
mGestureContentView = new GestureContentView(this, true,
MainActivity.str, new GestureCallBack() {
@Override
public void onGestureCodeInput(String inputCode) {
}
@Override
public void checkedSuccess() {
mGestureContentView.clearDrawlineState(0L);
MainActivity.off = false;
GestureVerifyActivity.this.finish();
}
@Override
public void checkedFail() {
mGestureContentView.clearDrawlineState(100L);// 绘制完清除线
mTextTip.setVisibility(View.VISIBLE);
mTextTip.setText(Html
.fromHtml("<font color=‘#c70c1e‘>密码错误</font>"));
// 左右移动动画
Animation shakeAnimation = AnimationUtils
.loadAnimation(GestureVerifyActivity.this,
R.anim.shake);
mTextTip.startAnimation(shakeAnimation);
}
});
// 设置手势解锁显示到哪个布局里面
mGestureContentView.setParentView(mGestureContainer);
}
private String getProtectedMobile(String phoneNumber) {
if (TextUtils.isEmpty(phoneNumber) || phoneNumber.length() < 11) {
return "";
}
StringBuilder builder = new StringBuilder();
builder.append(phoneNumber.subSequence(0, 3));
builder.append("****");
builder.append(phoneNumber.subSequence(7, 11));
return builder.toString();
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.text_cancel:
this.finish();
break;
default:
break;
}
}
}
布局
activity_gesture_edit
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#9b0b02" >
<RelativeLayout
android:id="@+id/top_layout"
android:layout_width="match_parent"
android:layout_height="46dip"
android:background="#000000"
android:paddingLeft="20dip"
android:paddingRight="20dip"
android:layout_alignParentTop="true" >
<TextView
android:id="@+id/text_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:gravity="center"
android:text="设置手势密码"
android:textSize="20sp"
android:textColor="#ffffff" />
<TextView
android:id="@+id/text_cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:gravity="right"
android:text="取消"
android:textSize="18sp" />
</RelativeLayout>
<LinearLayout
android:id="@+id/gesture_tip_layout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/top_layout"
android:gravity="center"
android:orientation="vertical" >
<com.view.LockIndicator
android:id="@+id/lock_indicator"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dip" />
<TextView
android:id="@+id/text_tip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="绘制解锁图案"
android:textColor="#F98F12"
android:layout_marginTop="10dip" />
</LinearLayout>
<FrameLayout
android:id="@+id/gesture_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="30dp"
android:layout_below="@id/gesture_tip_layout"
android:layout_gravity="center_horizontal"
android:layout_marginTop="20dip" >
</FrameLayout>
<TextView
android:id="@+id/text_reset"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:layout_below="@id/gesture_container"
android:layout_marginTop="20dip"
android:text="设置手势密码,防止他人未经授权查看"
android:textColor="#816E6A" />
</RelativeLayout>
activity_gesture_verify
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#9b0b02" >
<RelativeLayout
android:id="@+id/top_layout"
android:layout_width="match_parent"
android:layout_height="46dip"
android:background="#000000"
android:paddingLeft="20dip"
android:paddingRight="20dip"
android:layout_alignParentTop="true"
android:visibility="visible" >
<TextView
android:id="@+id/text_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:gravity="center"
android:text="设置手势密码"
android:textSize="20sp"
android:textColor="#000000" />
<TextView
android:id="@+id/text_cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:gravity="right"
android:text="取消"
android:textSize="18sp"/>
</RelativeLayout>
<LinearLayout
android:id="@+id/gesture_tip_layout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/top_layout"
android:orientation="vertical"
android:paddingTop="20dip" >
<ImageView
android:id="@+id/user_logo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:src="@drawable/user_logo" />
<TextView
android:id="@+id/text_phone_number"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dip"
android:gravity="center_horizontal"
android:text="152****8888"
android:textColor="#8C806F" />
<TextView
android:id="@+id/text_tip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dip"
android:gravity="center_horizontal"
android:textColor="#000000"
android:visibility="invisible" />
</LinearLayout>
<FrameLayout
android:id="@+id/gesture_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="30dp"
android:layout_gravity="center_horizontal"
android:layout_below="@id/gesture_tip_layout" >
</FrameLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="50dip"
android:orientation="horizontal" >
<TextView
android:id="@+id/text_forget_gesture"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:gravity="center"
android:textSize="16sp"
android:text="忘记手势密码?"
android:textColor="#E7E7E6" />
<TextView
android:id="@+id/text_other_account"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:gravity="center"
android:textSize="16sp"
android:text="用其他账号登录"
android:textColor="#E7E7E6" />
</LinearLayout>
</RelativeLayout>
anim
shake.xml
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXDelta="0"
android:toXDelta="10"
android:duration="120"
android:interpolator="@android:anim/cycle_interpolator"
android:repeatMode="restart"
android:repeatCount="2"/>
完整的Demo下载后导入就可以用了
下载路径:http://download.csdn.net/detail/menglele1314/8800209