`
bogongjie
  • 浏览: 236238 次
  • 性别: Icon_minigender_1
  • 来自: 苏州
社区版块
存档分类
最新评论

android 仿写 screen lock

 
阅读更多

程序参考com.android.settings:

程序中使用:import com.android.internal.widget.LockPatternUtils;

import com.android.internal.widget.LockPatternView;

import com.android.internal.widget.LockPatternView.Cell;

这三个类属于隐藏类,程序需要在源码中build。(该程序需系统权限)

   

package com.wistron.screen.lock;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.Vibrator;
import android.R.integer;
import android.app.Activity;
import android.app.admin.DevicePolicyManager;
import android.content.DialogInterface;
import android.util.Log;
import android.view.Gravity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.android.internal.widget.LockPatternUtils;
import com.android.internal.widget.LockPatternView;
import com.android.internal.widget.LockPatternView.Cell;
import com.wistron.screen.lock.CustomPattern.OnChangePointListenter;

public class ScreenLock extends Activity implements OnClickListener, OnChangePointListenter, OnItemSelectedListener {
	private final int SCREEN_LOCK_NONE = 0;
	private final int SCREEN_LOCK_SLIDE = 1;
	private final int SCREEN_LOCK_PATTERN = 2;
	private final int SCREEN_LOCK_PIN = 3;
	private final int SCREEN_LOCK_PASSWORD = 4;

	private DevicePolicyManager mDevicePolicyManager;
	private LockPatternUtils mLockPatternUtils;
	private Spinner mScreenModeSpinner;
	private Button mUpdateButton;
	private CustomPattern mCustomPattern;

	private int mQuilty;
	private boolean mMode;
	private int mSelectID = -1;

	protected List<LockPatternView.Cell> mChosenPattern = null;
	private int mRequestedQuality = DevicePolicyManager.PASSWORD_QUALITY_NUMERIC;
	private ArrayList<PatternPoint> mPatternPointsList = new ArrayList<ScreenLock.PatternPoint>();
	private Vibrator mVibrator;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_screen_lock);
		findView();

		mDevicePolicyManager = (DevicePolicyManager) getSystemService(DEVICE_POLICY_SERVICE);
		mLockPatternUtils = new LockPatternUtils(ScreenLock.this);
		mVibrator = (Vibrator)getSystemService(VIBRATOR_SERVICE);

		mChosenPattern = new ArrayList<LockPatternView.Cell>();
//		mChosenPattern.add(LockPatternView.Cell.of(0, 0));
//		mChosenPattern.add(LockPatternView.Cell.of(0, 1));
//		mChosenPattern.add(LockPatternView.Cell.of(1, 1));
//		mChosenPattern.add(LockPatternView.Cell.of(2, 1));

	}

	public void findView() {
		mScreenModeSpinner = (Spinner) findViewById(R.id.select_mode);
		mUpdateButton = (Button) findViewById(R.id.update);

		String[] mode = getResources().getStringArray(R.array.screen_lock_mode);
		List<String> mModeList = new ArrayList<String>();
		mModeList = Arrays.asList(mode);
		SpinnerTest mSpinnerTest = new SpinnerTest(mModeList);
		mScreenModeSpinner.setAdapter(mSpinnerTest);
		
		mScreenModeSpinner.setOnItemSelectedListener(this);
		
		mCustomPattern = (CustomPattern)findViewById(R.id.pattern_view);
		mCustomPattern.SetOnChangePointListener(this);
		mCustomPattern.setVisibility(View.GONE);

		mUpdateButton.setOnClickListener(this);
	}

	private void updateUnlockMethodAndFinish(int quality, boolean disabled) {
		// TODO Auto-generated method stub

		int minQuality = mDevicePolicyManager.getPasswordQuality(null);
		if (quality < minQuality) {
			quality = minQuality;
		}
		quality = upgradeQualityForEncryption(quality);

		if (quality >= DevicePolicyManager.PASSWORD_QUALITY_NUMERIC) {
			mRequestedQuality = Math.max(quality, mLockPatternUtils.getRequestedPasswordQuality());
			mLockPatternUtils.clearLock(true);
			mLockPatternUtils.saveLockPassword("123456", mRequestedQuality);
		} else if (quality == DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED) {
			mLockPatternUtils.clearLock(true);
			mLockPatternUtils.setLockScreenDisabled(disabled);
		} else if (quality == DevicePolicyManager.PASSWORD_QUALITY_SOMETHING) {
			saveChosenPatternAndFinish();
		}
	}

	private int upgradeQualityForEncryption(int quality) {
		int encryptionStatus = mDevicePolicyManager.getStorageEncryptionStatus();
		boolean encrypted = (encryptionStatus == DevicePolicyManager.ENCRYPTION_STATUS_ACTIVE)
				|| (encryptionStatus == DevicePolicyManager.ENCRYPTION_STATUS_ACTIVATING);
		if (encrypted) {
			if (quality < DevicePolicyManager.PASSWORD_QUALITY_NUMERIC) {
				quality = DevicePolicyManager.PASSWORD_QUALITY_NUMERIC;
			}
		}
		return quality;
	}

	public class SpinnerTest extends BaseAdapter {
		private List<String> mList;

		public SpinnerTest(List<String> list) {
			mList = list;
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return mList.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return mList.get(position);
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			TextView mView = new TextView(ScreenLock.this);
			mView.setText(mList.get(position));
			mView.setTextSize(25);
			mView.setGravity(Gravity.CENTER);
			return mView;
		}

	}

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

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		if (v == mUpdateButton) {
			getScreenLockMode();
			updateUnlockMethodAndFinish(mQuilty, mMode);
			if(mSelectID == SCREEN_LOCK_PATTERN){
				mCustomPattern.ClearTrack();
				mCustomPattern.RetryTrack();
			}
		}
	}

	public void getScreenLockMode() {
		int id = -1;
		id = (int) mScreenModeSpinner.getSelectedItemId();
		mSelectID = id;
		Log.i("Flag", "id= " + id);
		switch (id) {
		case SCREEN_LOCK_NONE:
			mQuilty = DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED;
			mMode = true;
			break;
		case SCREEN_LOCK_SLIDE:
			mQuilty = DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED;
			mMode = false;
			break;
		case SCREEN_LOCK_PATTERN:
			mQuilty = DevicePolicyManager.PASSWORD_QUALITY_SOMETHING;
			mMode = false;
			break;
		case SCREEN_LOCK_PIN:
			mQuilty = DevicePolicyManager.PASSWORD_QUALITY_NUMERIC;
			mMode = false;
			break;
		case SCREEN_LOCK_PASSWORD:
			mQuilty = DevicePolicyManager.PASSWORD_QUALITY_ALPHABETIC;
			mMode = false;
			break;
		}
	}

	private void saveChosenPatternAndFinish() {
		LockPatternUtils utils = mLockPatternUtils;
		final boolean lockVirgin = !utils.isPatternEverChosen();
		Log.i("Flag", "lockVirgin= " + lockVirgin);

		utils.saveLockPattern(mChosenPattern);
		utils.setLockPatternEnabled(true);

		if (lockVirgin) {
			// utils.setVisiblePatternEnabled(true);
			// utils.setTactileFeedbackEnabled(false);
		}

	}

	@Override
	public void isVibration(int x, int y) {
		// TODO Auto-generated method stub
		PatternPoint mPatternPoint = new PatternPoint();
		mPatternPoint.mXValue = x;
		mPatternPoint.mYValue = y;
		mPatternPointsList.add(mPatternPoint);
		mHandler.sendEmptyMessage(0);
	}

	@Override
	public void isDone() {
		// TODO Auto-generated method stub
		mHandler.sendEmptyMessage(2);
	}

	@Override
	public void isTouchDown() {
		// TODO Auto-generated method stub
		mHandler.sendEmptyMessage(1);
	}
	
	public Handler mHandler = new Handler(){

		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			super.handleMessage(msg);
			if(msg.what == 0){
				mVibrator.vibrate(50);
			}else if(msg.what == 1){
				mUpdateButton.setEnabled(false);
			}else if(msg.what == 2){
				mUpdateButton.setEnabled(true);
				if(mPatternPointsList.size() < 4){
					Toast.makeText(ScreenLock.this, getString(R.string.pattern_point_less),Toast.LENGTH_LONG).show();
				}else {
					for(int i = 0 ; i < mPatternPointsList.size() ; i ++){
						mChosenPattern.add(LockPatternView.Cell.of(mPatternPointsList.get(i).mXValue, mPatternPointsList.get(i).mYValue));
					}
				}
			}
		}
		
	};
	
	public class PatternPoint{
		int mXValue;
		int mYValue;
	}

	@Override
	public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		// TODO Auto-generated method stub
		if(arg2 == SCREEN_LOCK_PATTERN){
			mCustomPattern.setVisibility(View.VISIBLE);
		}else {
			mCustomPattern.setVisibility(View.GONE);
		}
	}

	@Override
	public void onNothingSelected(AdapterView<?> arg0) {
		// TODO Auto-generated method stub
		
	}
}

 

 

自定义Pattern:

    

package com.wistron.screen.lock;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

public class CustomPattern extends View {
	private Paint mPaint, mSizePaint, mLinePaint;
	private int mScreenWidth;
	private int mScreenHeight;
	private float mXActionDown, mYActionDown;
	private int mCircleRadiu = 25;
	private int mSide = 20 + mCircleRadiu;
	private boolean isFirstPoint = false;
	private float mOldPointX, mOldPointY;
	private ArrayList<CirclePoint> mCirclePointsList = new ArrayList<CustomPattern.CirclePoint>();
	private boolean isActionUp = false;
	private boolean isClear = true;

	public CustomPattern(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
		init(context);
	}

	public CustomPattern(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
		init(context);
	}

	public CustomPattern(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		// TODO Auto-generated constructor stub
		init(context);
	}

	public void init(Context context) {
		mPaint = new Paint();
		mPaint.setColor(Color.WHITE);
		mPaint.setStyle(Style.STROKE);

		mSizePaint = new Paint();
		mSizePaint.setColor(Color.GREEN);
		mSizePaint.setStyle(Style.STROKE);

		mLinePaint = new Paint();
		mLinePaint.setColor(Color.GRAY);

		invalidate();
	}

	@Override
	protected void onDraw(Canvas canvas) {
		// TODO Auto-generated method stub
		super.onDraw(canvas);
		mScreenWidth = getWidth();
		mScreenHeight = getHeight();

		canvas.drawColor(Color.BLACK);

		for (int j = 0; j < 3; j++) {
			for (int i = 0; i < 3; i++) {
				canvas.drawCircle(mSide + i * mScreenWidth / 3, mSide + j * mScreenHeight / 3, 5, mPaint);
			}
		}

		for (int j = 0; j < 3; j++) {
			for (int i = 0; i < 3; i++) {
				float distance = Distance(mSide + i * mScreenWidth / 3, mSide + j * mScreenHeight / 3, mXActionDown, mYActionDown);
				if (distance < mCircleRadiu) {

					float mTempX = mSide + i * mScreenWidth / 3;
					float mTempY = mSide + j * mScreenHeight / 3;
					boolean isHave = false;
					for (int k = 0; k < mCirclePointsList.size(); k++) {
						if (mCirclePointsList.get(k).x == mTempX) {
							if (mCirclePointsList.get(k).y == mTempY) {
								isHave = true;
							}
						}
					}
					if (!isHave && isClear) {
						mOldPointX = mTempX;
						mOldPointY = mTempY;
						CirclePoint mCirclePoint = new CirclePoint();
						mCirclePoint.x = mOldPointX;
						mCirclePoint.y = mOldPointY;
						mCirclePointsList.add(mCirclePoint);
						isFirstPoint = true;

						Message message = new Message();
						message.what = 0;
						message.arg1 = j;
						message.arg2 = i;
						handler.sendMessage(message);

					}
				}
			}
		}
		for (int i = 0; i < mCirclePointsList.size(); i++) {
			canvas.drawCircle(mCirclePointsList.get(i).x, mCirclePointsList.get(i).y, mCircleRadiu, mSizePaint);
		}
		if (mCirclePointsList.size() >= 2) {
			for (int i = 0; i < mCirclePointsList.size() - 1; i++) {
				canvas.drawLine(mCirclePointsList.get(i).x, mCirclePointsList.get(i).y, mCirclePointsList.get(i + 1).x,
						mCirclePointsList.get(i + 1).y, mLinePaint);
			}
		}
		if (isFirstPoint && !isActionUp) {
			canvas.drawLine(mOldPointX, mOldPointY, mXActionDown, mYActionDown, mLinePaint);
		}
	}

	public float Distance(float x0, float y0, float x1, float y1) {
		return (float) Math.sqrt(((x1 - x0) * (x1 - x0)) + ((y1 - y0) * (y1 - y0)));
	}
	
	public void RetryTrack(){
		isClear = true;
	}
	
	public void ClearTrack(){
		isClear = false;
		mCirclePointsList.clear();
		invalidate();
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		// TODO Auto-generated method stub
		addTouchEvent(event);
		return true;
	}

	public void addTouchEvent(MotionEvent event) {
		mXActionDown = event.getX();
		mYActionDown = event.getY();

		postInvalidate();
		if (event.getAction() == MotionEvent.ACTION_UP) {
			isActionUp = true;
			handler.sendEmptyMessage(1);
		}
		if (event.getAction() == MotionEvent.ACTION_DOWN) {
			handler.sendEmptyMessage(2);
			mCirclePointsList.clear();
		}
	}

	public class CirclePoint {
		float x;
		float y;
	}

	public OnChangePointListenter mChangePointListenter;

	public void SetOnChangePointListener(OnChangePointListenter listenter) {
		this.mChangePointListenter = listenter;
	}

	private Handler handler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			super.handleMessage(msg);
			if (msg.what == 0) {
				mChangePointListenter.isVibration(msg.arg1, msg.arg2);
			} else if (msg.what == 1) {
				mChangePointListenter.isDone();
			} else if (msg.what == 2) {
				mChangePointListenter.isTouchDown();
			}
		}

	};

	public abstract interface OnChangePointListenter {
		public abstract void isVibration(int x, int y);

		public abstract void isDone();

		public abstract void isTouchDown();
	}

}

 

分享到:
评论

相关推荐

    Android 仿写的微信的源码分享.rar

    Android 仿写的微信的源码分享,控件中用了腾讯微信的资源文件~实现启动界面、注册微信、搜索好友、登录微信,气泡聊天、通讯录、朋友圈、摇一尧设置界面、发超聊天、听筒模式、登录网页版、扫一扫等众多功能,可以...

    android仿写3DM游戏资讯客户端

    【Android 仿写3DM游戏资讯客户端】 在Android开发领域,构建一款类似3DM游戏资讯客户端的项目是一项挑战,涉及到多个技术层面。本项目旨在模仿3DM游戏资讯客户端,实现其核心功能,如新闻浏览、评论互动等,帮助...

    Android仿写QQ用户注册,登录界面实现.rar

    Android仿写QQ用户注册,登录界面实现,不过这个QQ登录界面好像有些老了,是前几年的,但是也挺漂亮的,开发其它Android应用时或许可以做个参考。这个登录界面上有登录框、面包宵菜单、复选框等基本的界面控件,另外...

    Android 仿写抖音视频切换功能.rar

    Android 仿写抖音视频APP端清淅度切换功能,初始化播放器(只需调用一次即可,建议在application中初始化),在本例子中,滑屏时SurfaceView出现白屏和黑屏问题得到了解决,准备成功之后可以调用start方法开始播放,...

    Android 仿写Siri声波震动波形效果-彩色曲线.rar

    Android彩色动态曲线生成例子,模拟实现了Siri声波震动效果,动态适时绘制并生成彩色的波形图,源代码中涉及到ios9之前版本及ios9上的两种效果。SiriWaveDemo是仿ios9之前的效果,SiriWaveView.java,另外是仿ios9...

    android模仿qq设置界面的源码

    在Android开发中,模仿QQ设置界面是一个常见的学习和实践项目,可以帮助开发者深入理解Android UI设计、自定义控件以及布局管理。本项目源码提供了创建类似QQ设置界面的方法,通过分析和实现这些代码,我们可以学到...

    Android-仿写酷界面HenCoder仿小米运动的运动记录界面

    这里提到的"仿写酷界面HenCoder仿小米运动的运动记录界面"是一个实战项目,旨在帮助开发者学习如何创建类似小米运动应用中的运动记录界面。该项目可能会涵盖以下知识点: 1. **UI设计**:Android Studio中的布局...

    仿写linearlayout

    在Android开发中,`LinearLayout` 是一个非常基础且重要的布局组件,它允许我们将子视图按照垂直或水平方向线性排列。在这个“仿写LinearLayout”的项目中,我们显然是要实现一个自定义视图组(`ViewGroup`)来模仿...

    用android仿写一个可视化语音信箱界面且保证点击item动画展开可以完全显示

    在Android开发中,创建一个可视化的语音信箱界面是一项常见的任务,尤其当涉及到用户交互和界面设计时。这个项目的目标是模仿语音信箱的功能,并确保点击列表项(item)时能展开完全显示所有相关信息。这里我们将...

    Android 高仿360软件详情页

    【Android 高仿360软件详情页】是Android开发中的一个高级实践,涉及到界面设计、用户交互以及组件的组合运用。在这个项目中,开发者通常会利用ScrollView和ViewPager这两个核心组件来实现类似360软件详情页的效果。...

    仿写scrollview

    本项目名为“仿写scrollview”,意味着我们将探讨如何从零开始实现一个类似于原生Android ScrollView的功能。 ScrollView通常被用来包裹其他视图,如LinearLayout、RelativeLayout或ConstraintLayout等,以实现可...

    Android-TapeHencoder高度自定义刻度尺HenCoder仿写系列

    "Android-TapeHencoder高度自定义刻度尺HenCoder仿写系列"就是这样一个项目,它专注于实现一个高度可定制的刻度尺控件,灵感来源于HenCoder的教学系列。HenCoder是一个广受欢迎的Android开发学习资源,它提供了许多...

    Android高级应用源码-仿qq通讯录联系人.zip

    这个压缩包文件“Android高级应用源码-仿qq通讯录联系人.zip”是关于在Android平台上构建一个类似于QQ通讯录的高级应用的源代码。它包含了一个完整的项目结构,可以帮助开发者学习和理解如何在Android中实现高效且...

    仿写QQ的,Android入门学习的可以看看

    在Android开发领域,仿写流行的应用是初学者学习和进阶的一个常见方法。"仿写QQ"项目就是这样一个实例,它旨在帮助初学者通过模仿腾讯QQ的用户界面和功能,来掌握Android应用开发的基本技能。这个项目对于刚接触...

    仿写薄荷健康体重选择尺子控件

    "仿写薄荷健康体重选择尺子控件"是一个旨在模仿薄荷健康应用中的体重选择功能的自定义UI组件。这个控件通常用于帮助用户轻松、准确地输入自己的体重信息,提供一种视觉上更直观、更友好的交互方式。 薄荷健康是一款...

    Android仿UC浏览器,商业级的代码

    【标题】"Android仿UC浏览器,商业级的代码"揭示了这个项目的目标是创建一个类似于UC浏览器的应用,但具有商业级别的质量和性能。在Android平台上,这样的应用需要考虑到用户体验、性能优化、安全性和可扩展性等多个...

    Android仿美团界面APP源码.zip

    在本项目中,"Android仿美团界面APP源码" 是一个专门为Android开发者提供的学习资源,旨在帮助他们理解和实现类似美团应用的用户界面。这个源码是实现一个完整的Android应用程序,包含多个功能模块,如首页、分类、...

    android高仿微信(服务器+客户端)

    【Android高仿微信项目概述】 本项目旨在创建一个与微信类似的应用程序,涵盖了基础的文字、图片和语音通讯功能。作为一个企业内部的通讯工具,它具备了即时通讯的基本要素,为团队协作提供了便捷的沟通渠道。在...

    Android-计数功能仿照小米运动的动画

    在Android应用开发中,实现类似小米运动的计数动画效果是一项常见的需求,它可以为用户提供更加生动、直观的交互体验。小米运动的计步动画通常包括数字动态增长、过渡平滑以及与用户操作实时同步等特点。要创建这样...

    仿京东分类 android

    在Android开发领域,"仿京东分类 android"是一个常见的实践项目,旨在模拟京东应用程序中的商品分类功能。这个项目主要涉及到了ListView和Fragment两个核心组件,它们是Android应用开发中不可或缺的部分。 **...

Global site tag (gtag.js) - Google Analytics