上接:
系列文章链接如下:
代码文件地址:
https://github.com/wukong1688/Android-BaseLockScreen
3、按键的屏蔽
当自定义锁屏页最终出现在手机上时,我们希望它像系统锁屏页那样屹立不倒,所有的按键都不能触动它,只有通过划屏或者指纹才能解锁,因此有必要对按键进行一定程度上的屏蔽。针对只有虚拟按键的手机,我们可以通过隐藏虚拟按键的方式部分解决这个问题。但是当用户在锁屏页底部滑动,隐藏后的虚拟按键还是会滑出,而且如果用户是物理按键的话就必须进行屏蔽了。需要重写Activity的onBackPressed()方法即可。
public boolean onKeyDown(int keyCode, KeyEvent event) {
int key = event.getKeyCode();
switch (key) {
case KeyEvent.KEYCODE_BACK: {
return true;
}
case KeyEvent.KEYCODE_MENU: {
return true;
}
}
return super.onKeyDown(keyCode, event);
}
4、滑屏解锁
package com.jack.applockscreen.activity; import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Align;
import android.graphics.Paint.Style;
import android.graphics.Point;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.support.v4.view.ViewConfigurationCompat;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.view.Display;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.WindowManager; import com.jack.applockscreen.R; /**
* 解锁按钮控件
*
* @author chenglei
*/
public class LockScreenView extends View { private int mWidth;
private int mHeight; private int mScreenWidth;
private int mScreenHeight;
private WindowManager mWindowManager;
private Display mDisplay;
private float mTouchPadRadius;
private float mTouchPadBorderWidthNormal;
private float mTouchPadBorderWidthPressed;
private Paint mPaintNormal;
private TextPaint mTextPaint;
private Paint mBackgroundPaint; private Point mTouchPadCenter = new Point(); private int mTouchSlop;
private int mStartX;
private int mCurrX;
private int mOffectX;
private boolean mCanMove; private Drawable mDrawableLeft;
private Drawable mDrawableRight;
private Drawable mDrawableCenter;
private String mTextLeft;
private String mTextRight; private Point mTargetDrawableLeftPosition = new Point();
private Point mTargetDrawableRightPosition = new Point();
private Point mTargetTextLeftPosition = new Point();
private Point mTargetTextRightPosition = new Point();
private float mDensity;
private static final float TOUCH_PAD_BORDER_WIDTH_NORMAL = ;
private static final float TOUCH_PAD_BORDER_WIDTH_PRESSED = ;
private static final float TARGET_DRAWABLE_PADDING = ;
private static final float TARGET_TEXT_SIZE = ; private static final int STATE_TRIGGER_NONE = ;
private static final int STATE_TRIGGER_LEFT = ;
private static final int STATE_TRIGGER_RIGHT = ;
private int mTouchPadTriggerState = STATE_TRIGGER_NONE; private OnTriggerListener mOnTriggerListener; private Drawable mDotsLeft;
private Drawable mDotsRight; public LockScreenView(Context context) {
this(context, null, );
} public LockScreenView(Context context, AttributeSet attrs) {
this(context, attrs, );
} public LockScreenView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
} private void init() {
mWindowManager = ((Activity) getContext()).getWindowManager();
mDisplay = mWindowManager.getDefaultDisplay();
mScreenWidth = mDisplay.getWidth();
mScreenHeight = mDisplay.getHeight(); // mTouchPadBorderWidthNormal = DensityUtil.dip2px(getContext(), 8);
// mTouchPadBorderWidthPressed = DensityUtil.dip2px(getContext(), 2); mDensity = getResources().getDisplayMetrics().density;
mTouchPadBorderWidthNormal = TOUCH_PAD_BORDER_WIDTH_NORMAL * mDensity;
mTouchPadBorderWidthPressed = TOUCH_PAD_BORDER_WIDTH_PRESSED * mDensity; final ViewConfiguration configuration = ViewConfiguration.get(getContext());
mTouchSlop = ViewConfigurationCompat.getScaledPagingTouchSlop(configuration); mDrawableCenter = getResources().getDrawable(R.mipmap.ic_launcher);
mDotsLeft = getResources().getDrawable(R.mipmap.keyguaid_dots_left);
mDotsRight = getResources().getDrawable(R.mipmap.keyguaid_dots_right); initNormalPaint();
initTextPaint();
} private void initNormalPaint() {
mPaintNormal = new Paint();
mPaintNormal.setFlags(Paint.ANTI_ALIAS_FLAG);
mPaintNormal.setColor(Color.WHITE);
mPaintNormal.setStyle(Style.STROKE);
mPaintNormal.setStrokeWidth(mTouchPadBorderWidthNormal);
mBackgroundPaint = new Paint();
mBackgroundPaint.setColor(Color.parseColor("#66000000"));
} private void initTextPaint() {
mTextPaint = new TextPaint(TextPaint.ANTI_ALIAS_FLAG);
mTextPaint.setColor(Color.WHITE);
mTextPaint.setTextAlign(Align.CENTER);
mTextPaint.setTextSize(TARGET_TEXT_SIZE * mDensity);
} @Override
protected void onDraw(Canvas canvas) {
drawBackground(canvas);
drawDots(canvas);
drawTouchPad(canvas);
drawTargetDrawablesAndTexts(canvas);
} private void drawBackground(Canvas canvas) {
canvas.drawRect(, mHeight / , mWidth, mHeight * / , mBackgroundPaint);
} private void drawDots(Canvas canvas) {
if (!mTouchDown) {
BitmapDrawable dotsLeft = (BitmapDrawable) mDotsLeft;
BitmapDrawable dotsRight = (BitmapDrawable) mDotsRight;
canvas.drawBitmap(dotsLeft.getBitmap(),
mWidth / - mDotsLeft.getIntrinsicWidth() / ,
mHeight / - mDotsLeft.getIntrinsicHeight() / ,
mPaintNormal);
canvas.drawBitmap(dotsRight.getBitmap(),
mWidth * / - mDotsRight.getIntrinsicWidth() / ,
mHeight / - mDotsRight.getIntrinsicHeight() / ,
mPaintNormal);
}
} private void drawTouchPad(Canvas canvas) {
BitmapDrawable bd = (BitmapDrawable) mDrawableCenter;
if (mTouchDown) {
canvas.drawCircle(mTouchPadCenter.x, mTouchPadCenter.y, mDrawableCenter.getIntrinsicWidth() - , mPaintNormal);
} else {
canvas.drawBitmap(bd.getBitmap(),
mTouchPadCenter.x - mDrawableCenter.getIntrinsicWidth() / ,
mTouchPadCenter.y - mDrawableCenter.getIntrinsicHeight() / ,
mPaintNormal);
canvas.drawCircle(mTouchPadCenter.x, mTouchPadCenter.y, mDrawableCenter.getIntrinsicWidth() - , mPaintNormal);
}
} private void drawTargetDrawablesAndTexts(Canvas canvas) {
if (mDrawableLeft != null) {
BitmapDrawable bd = (BitmapDrawable) mDrawableLeft;
canvas.drawBitmap(
bd.getBitmap(),
mTargetDrawableLeftPosition.x - mDrawableLeft.getIntrinsicWidth() / ,
mTargetDrawableLeftPosition.y - mDrawableLeft.getIntrinsicHeight() / ,
mPaintNormal);
} if (mDrawableRight != null) {
BitmapDrawable bd = (BitmapDrawable) mDrawableRight;
canvas.drawBitmap(
bd.getBitmap(),
mTargetDrawableRightPosition.x - mDrawableRight.getIntrinsicWidth() / ,
mTargetDrawableRightPosition.y - mDrawableRight.getIntrinsicHeight() / ,
mPaintNormal);
} if (mTextLeft != null) {
canvas.drawText(mTextLeft, mTargetTextLeftPosition.x, mTargetTextLeftPosition.y, mTextPaint);
} if (mTextRight != null) {
canvas.drawText(mTextRight, mTargetTextRightPosition.x, mTargetTextRightPosition.y, mTextPaint);
}
} @Override
public boolean performClick() {
return super.performClick();
} private boolean mTouchDown = false; @Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
mStartX = (int) event.getX();
if (mStartX > mTouchPadCenter.x - mTouchPadRadius && mStartX < mTouchPadCenter.x + mTouchPadRadius) {
mPaintNormal.setStrokeWidth(mTouchPadBorderWidthPressed);
mTouchDown = true;
invalidate();
} else {
return false;
}
break;
case MotionEvent.ACTION_MOVE:
mCurrX = (int) event.getX();
// mOffectX = mCurrX - mStartX;
//
// if (!mCanMove && Math.abs(mOffectX) < mTouchSlop) {
// return false;
// } else {
// mCanMove = true;
// if (mOffectX > mTouchSlop) {
// mOffectX += mTouchSlop;
// } else if (mOffectX < -mTouchSlop) {
// mOffectX -= mTouchSlop;
// }
// } mStartX = mCurrX;
if (mDrawableRight != null && mCurrX < mWidth * / /*mTargetDrawableLeftPosition.x + mDrawableRight.getIntrinsicWidth() * 2.5*/) {
mTouchPadCenter.set(mTargetDrawableLeftPosition.x, mTargetDrawableLeftPosition.y);
mTouchPadTriggerState = STATE_TRIGGER_LEFT;
} else if (mDrawableRight != null && mCurrX > mWidth * / /*mTargetDrawableRightPosition.x - mDrawableRight.getIntrinsicWidth() * 2.5*/) {
mTouchPadCenter.set(mTargetDrawableRightPosition.x, mTargetDrawableRightPosition.y);
mTouchPadTriggerState = STATE_TRIGGER_RIGHT;
} else {
mTouchPadCenter.x = mCurrX;
mTouchPadCenter.set(mTouchPadCenter.x + mOffectX, mTouchPadCenter.y);
mTouchPadTriggerState = STATE_TRIGGER_NONE;
} invalidate();
break;
case MotionEvent.ACTION_UP:
mCanMove = false;
if (STATE_TRIGGER_NONE == mTouchPadTriggerState) {
mTouchPadCenter.set(mWidth / , mHeight / );
//mPaintNormal.setStrokeWidth(mTouchPadBorderWidthNormal);
mTouchDown = false;
invalidate();
} else if (STATE_TRIGGER_LEFT == mTouchPadTriggerState) {
if (mOnTriggerListener != null) {
mOnTriggerListener.onTriggerLeft();
}
} else if (STATE_TRIGGER_RIGHT == mTouchPadTriggerState) {
if (mOnTriggerListener != null) {
mOnTriggerListener.onTriggerRight();
}
}
break;
}
return true;
} @Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int widthMode = MeasureSpec.getMode(widthMeasureSpec);
int heightMode = MeasureSpec.getMode(heightMeasureSpec);
int widthSize = MeasureSpec.getSize(widthMeasureSpec);
int heightSize = MeasureSpec.getSize(heightMeasureSpec); if (widthMode == MeasureSpec.EXACTLY) {
// Parent has told us how big to be. So be it.
mWidth = widthSize;
} else {
mWidth = mScreenWidth; // Check against our minimum width
mWidth = Math.max(mWidth, getSuggestedMinimumWidth()); if (widthMode == MeasureSpec.AT_MOST) {
mWidth = Math.min(widthSize, mWidth);
}
} if (heightMode == MeasureSpec.EXACTLY) {
mHeight = heightSize;
} else {
mHeight = mScreenHeight / ; // Check against our minimum width
mHeight = Math.max(mHeight, getSuggestedMinimumHeight()); if (heightMode == MeasureSpec.AT_MOST) {
mHeight = Math.min(heightSize, mHeight);
}
} setMeasuredDimension(mWidth, mHeight);
mTouchPadRadius = mDrawableCenter.getIntrinsicWidth(); mTouchPadCenter.set(mWidth / , mHeight / ); mTargetDrawableLeftPosition.set((int) (TARGET_DRAWABLE_PADDING * mDensity), mHeight / );
mTargetDrawableRightPosition.set((int) (mWidth - TARGET_DRAWABLE_PADDING * mDensity), mHeight / );
mTargetTextLeftPosition.set((int) (TARGET_DRAWABLE_PADDING * mDensity), (int) (mHeight / - * mDensity));
mTargetTextRightPosition.set((int) (mWidth - TARGET_DRAWABLE_PADDING * mDensity), (int) (mHeight / - * mDensity));
} public void setTargetDrawablesAndTexts(Drawable drawableLeft, Drawable drawableRight, String textLeft, String textRight) {
mDrawableLeft = drawableLeft;
mDrawableRight = drawableRight;
mTextLeft = textLeft;
mTextRight = textRight;
invalidate();
} public void setTargetDrawablesAndTexts(int drawableLeftId, int drawableRightId, String textLeft, String textRight) {
mDrawableLeft = drawableLeftId > ? getResources().getDrawable(drawableLeftId) : null;
mDrawableRight = drawableRightId > ? getResources().getDrawable(drawableRightId) : null;
mTextLeft = textLeft;
mTextRight = textRight;
invalidate();
} public interface OnTriggerListener {
public void onTriggerLeft(); public void onTriggerRight();
} public void setOnTriggerListener(OnTriggerListener l) {
mOnTriggerListener = l;
} }
很多朋友私下问我代码文件的位置,这里也贴出来分享给大家!
Github地址: