Android滑动解锁控件.docx
《Android滑动解锁控件.docx》由会员分享,可在线阅读,更多相关《Android滑动解锁控件.docx(11页珍藏版)》请在冰豆网上搜索。
![Android滑动解锁控件.docx](https://file1.bdocx.com/fileroot1/2023-2/4/876b480f-45e6-4560-97ed-811f6e467629/876b480f-45e6-4560-97ed-811f6e4676291.gif)
Android滑动解锁控件
Android滑动解锁控件
importandroid.content.Context;
importandroid.graphics.Bitmap;
importandroid.graphics.BitmapFactory;
importandroid.graphics.Canvas;
importandroid.graphics.Rect;
importandroid.os.Handler;
importandroid.os.Message;
importandroid.os.Vibrator;
importandroid.util.AttributeSet;
importandroid.util.Log;
importandroid.view.MotionEvent;
importandroid.view.View;
importandroid.widget.ImageView;
importandroid.widget.RelativeLayout;
importandroid.widget.TextView;
importcom.mosjoy.ad.R;
publicclassSliderRelativeLayoutextendsRelativeLayout{
privatestaticStringTAG="SliderRelativeLayout";
privateTextViewtv_slider_icon=null;//初始控件,用来判断是否为拖动?
privateBitmapdragBitmap=null;//拖拽图片
privateContextmContext=null;//初始化图片拖拽时的Bitmap对象
privateHandlermainHandler=null;//与主Activity通信的Handler对象
privateImageViewmPhoneImageView;
privateImageViewmUnlockImageView;
privateImageViewmMessageImageView;
privateImageViewmCameraImageView;
privatebooleanmStopBoolean=false;
privateCanvasmCanvas;
publicSliderRelativeLayout(Contextcontext){
super(context);
mContext=context;
initDragBitmap();
}
publicSliderRelativeLayout(Contextcontext,AttributeSetattrs){
super(context,attrs,0);
mContext=context;
initDragBitmap();
}
publicSliderRelativeLayout(Contextcontext,AttributeSetattrs,
intdefStyle){
super(context,attrs,defStyle);
mContext=context;
initDragBitmap();
}
//初始化图片拖拽时的Bitmap对象
privatevoidinitDragBitmap(){
if(dragBitmap==null)
dragBitmap=BitmapFactory.decodeResource(mContext.getResources(),
R.drawable.getup_slider_ico_normal);
}
@Override
protectedvoidonFinishInflate(){
super.onFinishInflate();
//该控件主要判断是否处于滑动点击区域。
滑动时处于INVISIBLE(不可见)状态,滑动时处于VISIBLE(可见)状态
tv_slider_icon=(TextView)findViewById(R.id.slider_icon);
mPhoneImageView=(ImageView)findViewById(R.id.iv_phone);
mUnlockImageView=(ImageView)findViewById(R.id.iv_unlock);
mCameraImageView=(ImageView)findViewById(R.id.iv_camera);
mMessageImageView=(ImageView)findViewById(R.id.iv_message);
}
privateintmLastMoveX=1000;//当前bitmap应该绘制的地方,初始值为足够大,可以认为看不见
privateintmLastMoveY=1000;//当前bitmap应该绘制的地方,初始值为足够大,可以认为看不见
publicbooleanonTouchEvent(MotionEventevent){
intx=(int)event.getX();
inty=(int)event.getY();
switch(event.getAction()){
caseMotionEvent.ACTION_DOWN:
mLastMoveX=(int)event.getX();
mLastMoveY=(int)event.getY();
//处理Action_Down事件:
判断是否点击了滑动区域
returnhandleActionDownEvenet(event);
caseMotionEvent.ACTION_MOVE:
mLastMoveX=x;//保存了X轴方向
mLastMoveY=y;
invalidate();//重新绘制
returntrue;
caseMotionEvent.ACTION_UP:
//处理Action_Up事件:
判断是否解锁成功,成功则结束我们的Activity;否则,缓慢回退该图片。
handleActionUpEvent(event);
returntrue;
}
returnsuper.onTouchEvent(event);
}
//绘制拖动时的图片
@Override
protectedvoidonDraw(Canvascanvas){
super.onDraw(canvas);
//图片更随手势移动
if(!
mStopBoolean){
invalidateDragImg(canvas);
}
mCanvas=canvas;
}
//图片更随手势移动
privatevoidinvalidateDragImg(Canvascanvas){
//Log.e(TAG,"handleActionUpEvenet:
invalidateDragImg");
//以合适的坐标值绘制该图片
intdrawXCor=mLastMoveX-dragBitmap.getWidth()/2;
intdrawYCor=mLastMoveY-dragBitmap.getHeight()/2;
canvas.drawBitmap(dragBitmap,drawXCor<0?
5:
drawXCor,drawYCor,null);
//isHitUnlock(canvas);
}
//手势落下是,是否点中了图片,即是否需要开始移动
privatebooleanhandleActionDownEvenet(MotionEventevent){
Rectrect=newRect();
tv_slider_icon.getHitRect(rect);
booleanisHit=rect.contains((int)event.getX(),(int)event.getY());
//开始拖拽,隐藏该图片
if(isHit&&!
mStopBoolean){
tv_slider_icon.setVisibility(View.INVISIBLE);
mPhoneImageView.setVisibility(View.VISIBLE);
mUnlockImageView.setVisibility(View.VISIBLE);
mMessageImageView.setVisibility(View.VISIBLE);
mCameraImageView.setVisibility(View.VISIBLE);
}
returnisHit;
}
//判断是否到达解锁点
privatebooleanisHitUnlock(){
RectphoneRect=newRect();
mPhoneImageView.getHitRect(phoneRect);
RectunlockRect=newRect();
mUnlockImageView.getHitRect(unlockRect);
RectmessageRect=newRect();
mMessageImageView.getHitRect(messageRect);
RectcameraRect=newRect();
mCameraImageView.getHitRect(cameraRect);
//解锁到电话界面
if(phoneRect.contains(mLastMoveX,mLastMoveY)){
mStopBoolean=true;
adStopTwoSecond();
//结束我们的主Activity界面
Messagemsg=newMessage();
msg.what=MainLockActivity.MSG_PHONE_LOCK_SUCESS;
mainHandler.sendMessageDelayed(msg,2*1000);
//mainHandler.obtainMessage(MainLockActivity.MSG_PHONE_LOCK_SUCESS).sendToTarget();
returntrue;
}elseif(unlockRect.contains(mLastMoveX,mLastMoveY)){
mStopBoolean=true;
adStopTwoSecond();
//结束我们的主Activity界面
Messagemsg=newMessage();
msg.what=MainLockActivity.MSG_LOCK_SUCESS;
mainHandler.sendMessageDelayed(msg,21000);
returntrue;
}elseif(messageRect.contains(mLastMoveX,mLastMoveY)){
mStopBoolean=true;
adStopTwoSecond();
//结束我们的主Activity界面
Messagemsg=newMessage();
msg.what=MainLockActivity.MSG_MESSAGE_LOCK_SUCESS;
mainHandler.sendMessageDelayed(msg,21000);
//mainHandler.obtainMessage(MainLockActivity.MSG_MESSAGE_LOCK_SUCESS).sendToTarget();
returntrue;
}elseif(cameraRect.contains(mLastMoveX,mLastMoveY)){
mStopBoolean=true;
adStopTwoSecond();
//结束我们的主Activity界面
Messagemsg=newMessage();
msg.what=MainLockActivity.MSG_CAMERA_LOOK_SUCESS;
mainHandler.sendMessageDelayed(msg,2*1000);
//mainHandler.obtainMessage(MainLockActivity.MSG_CAMERA_LOOK_SUCESS).sendToTarget();
returntrue;
}
returnfalse;
}
//回退动画时间间隔值
privatestaticintBACK_DURATION=20;//20ms
//水平方向前进速率
privatestaticfloatVE_HORIZONTAL=0.7f;//0.1dip/ms
//判断松开手指时,是否达到末尾即可以开锁了,是,则开锁,否则,通过一定的算法使其回退。
privatevoidhandleActionUpEvent(MotionEventevent){
intx=(int)event.getX();
inty=(int)event.getY();
//解锁到电话界面
if(isHitUnlock()){
}else{
mStopBoolean=false;
//没有成功解锁,以一定的算法使其回退
//每隔20ms,速率为0.6dip/ms,使当前的图片往后回退一段距离,直到到达最左端
mLastMoveX=x;//记录手势松开时,当前的坐标位置。
intdistance=x-tv_slider_icon.getRight();
//只有移动了足够距离才回退
Log.e(TAG,"handleActionUpEvent:
mLastMoveX-->"+mLastMoveX+"distance-->"+distance);
if(distance>=0)
mHandler.postDelayed(BackDragImgTask,BACK_DURATION);
else{//复原初始场景
resetViewState();
}
}
}
//暂停两秒
privatevoidadStopTwoSecond(){
mPhoneImageView.setVisibility(View.INVISIBLE);
mUnlockImageView.setVisibility(View.INVISIBLE);
mCameraImageView.setVisibility(View.INVISIBLE);
mMessageImageView.setVisibility(View.INVISIBLE);
//mCanvas.drawBitmap(dragBitmap,2000,2000,null);
invalidate();
//try{
//Thread.sleep(2*1000);
//}catch(InterruptedExceptione){
//e.printStackTrace();
//}
}
//重置初始的状态,显示tv_slider_icon图像,使bitmap不可见
privatevoidresetViewState(){
mLastMoveX=1000;
mLastMoveY=1000;
tv_slider_icon.setVisibility(View.VISIBLE);
mPhoneImageView.setVisibility(View.INVISIBLE);
mUnlockImageView.setVisibility(View.INVISIBLE);
mCameraImageView.setVisibility(View.INVISIBLE);
mMessageImageView.setVisibility(View.INVISIBLE);
invalidate();//重绘最后一次
}
//通过延时控制当前绘制bitmap的位置坐标
privateRunnableBackDragImgTask=newRunnable(){
publicvoidrun(){
//一下次Bitmap应该到达的坐标值
mLastMoveX=mLastMoveX-(int)(BACK_DURATION*VE_HORIZONTAL);
invalidate();//重绘
//是否需要下一次动画?
到达了初始位置,不在需要绘制
booleanshouldEnd=Math.abs(mLastMoveX-tv_slider_icon.getRight())<=8;
if(!
shouldEnd)
mHandler.postDelayed(BackDragImgTask,BACK_DURATION);
else{//复原初始场景
resetViewState();
}
}
};
privateHandlermHandler=newHandler(){
publicvoidhandleMessage(Messagemsg){
}
};
//震动一下下咯
privatevoidvirbate(){
Vibratorvibrator=(Vibrator)mContext.getSystemService(Context.VIBRATOR_SERVICE);
vibrator.vibrate(200);
}
publicvoidsetMainHandler(Handlerhandler){
mainHandler=handler;//activity所在的Handler对象
}
}