|
|
@@ -24,6 +24,7 @@ import android.view.MotionEvent;
|
|
|
import android.view.View;
|
|
|
|
|
|
import androidx.annotation.Nullable;
|
|
|
+import androidx.core.content.res.ResourcesCompat;
|
|
|
|
|
|
public class UnlockView extends View {
|
|
|
|
|
|
@@ -34,17 +35,20 @@ public class UnlockView extends View {
|
|
|
private Path borderPath;
|
|
|
private float borderWidth;
|
|
|
private Bitmap bmUnlock;
|
|
|
+ private Bitmap bmUnlocked;
|
|
|
private float distance = 0;
|
|
|
private float padding = 0;
|
|
|
private boolean down;
|
|
|
private float lastX;
|
|
|
- private Rect src;
|
|
|
- private RectF dst;
|
|
|
+ private Rect src, src1;
|
|
|
+ private RectF dst, dst1;
|
|
|
private DrawFilter drawFilter;
|
|
|
private Typeface source_han_sans_sc_normal;
|
|
|
private Typeface source_han_sans_sc_light;
|
|
|
private int colorLight = 0xFFFFFFFF;
|
|
|
private int colorDark = 0xFF222639;
|
|
|
+ private int colorUnlockedLight = 0x11C1C3CD;
|
|
|
+ private int colorUnlockedDark = 0x1E222639;
|
|
|
private boolean dark;
|
|
|
|
|
|
private String hint;
|
|
|
@@ -84,8 +88,8 @@ public class UnlockView extends View {
|
|
|
dark = t.getBoolean(R.styleable.UnlockView_darkMode, false);
|
|
|
t.recycle();
|
|
|
|
|
|
- source_han_sans_sc_normal = Typeface.createFromAsset(this.getContext().getAssets(), "font/source_han_sans_sc_normal.otf");
|
|
|
- source_han_sans_sc_light = Typeface.createFromAsset(this.getContext().getAssets(), "font/source_han_sans_sc_light.otf");
|
|
|
+ source_han_sans_sc_normal = ResourcesCompat.getFont(getContext(), R.font.source_han_sans_sc_normal);
|
|
|
+ source_han_sans_sc_light = ResourcesCompat.getFont(getContext(), R.font.source_han_sans_sc_light);
|
|
|
borderPaint = new Paint();
|
|
|
borderPaint.setAntiAlias(true);
|
|
|
borderWidth = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 1, getContext().getResources().getDisplayMetrics());
|
|
|
@@ -95,8 +99,11 @@ public class UnlockView extends View {
|
|
|
borderPath = new Path();
|
|
|
padding = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 7, getContext().getResources().getDisplayMetrics());
|
|
|
bmUnlock = BitmapFactory.decodeResource(getResources(), dark ? R.mipmap.icon_unlock_dark : R.mipmap.icon_unlock);
|
|
|
+ bmUnlocked = BitmapFactory.decodeResource(getResources(), dark ? R.mipmap.icon_unlocked_dark : R.mipmap.icon_unlocked);
|
|
|
src = new Rect(0, 0, bmUnlock.getWidth(), bmUnlock.getHeight());
|
|
|
dst = new RectF();
|
|
|
+ src1 = new Rect(0, 0, bmUnlocked.getWidth(), bmUnlocked.getHeight());
|
|
|
+ dst1 = new RectF();
|
|
|
|
|
|
drawFilter = new PaintFlagsDrawFilter(0, Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG);
|
|
|
|
|
|
@@ -111,6 +118,11 @@ public class UnlockView extends View {
|
|
|
textPaint.setTypeface(source_han_sans_sc_light);
|
|
|
}
|
|
|
|
|
|
+ public void setUnlocked(boolean unlocked) {
|
|
|
+ this.unlocked = unlocked;
|
|
|
+ postInvalidate();
|
|
|
+ }
|
|
|
+
|
|
|
@Override
|
|
|
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
|
|
|
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
|
|
|
@@ -124,15 +136,36 @@ public class UnlockView extends View {
|
|
|
float height = getHeight();
|
|
|
float d = (height - borderWidth);
|
|
|
float r = d / 2;
|
|
|
- if (dark) {
|
|
|
- borderPaint.setColor(colorDark);
|
|
|
- textPaint.setColor(colorDark);
|
|
|
+
|
|
|
+ if (unlocked) {
|
|
|
+ borderPaint.setColor(dark ? colorUnlockedDark : colorUnlockedLight);
|
|
|
+ borderPaint.setStyle(Paint.Style.FILL);
|
|
|
+ textPaint.setColor(dark ? colorDark : colorLight);
|
|
|
+ canvas.drawRoundRect(0, 0, width, height, height / 2, height / 2, borderPaint);
|
|
|
+ float textWidth = textPaint.measureText("已解锁");
|
|
|
+ float baseLineY = Math.abs(textPaint.ascent() + textPaint.descent()) / 2 + getHeight() / 2f;
|
|
|
+ canvas.drawText("已解锁", (getWidth() + dp2px(32)) / 2f - textWidth / 2, baseLineY, textPaint);
|
|
|
+
|
|
|
+ dst1.left = (width - textPaint.measureText("已解锁") - dp2px(32)) / 2;
|
|
|
+ dst1.top = (height - dp2px(26)) / 2;
|
|
|
+ dst1.right = dst1.left + dp2px(21);
|
|
|
+ dst1.bottom = dst1.top + dp2px(26);
|
|
|
+ canvas.drawBitmap(bmUnlocked, src1, dst1, null);
|
|
|
+ return;
|
|
|
} else {
|
|
|
- borderPaint.setColor(colorLight);
|
|
|
- textPaint.setColor(colorLight);
|
|
|
+ borderPaint.setStyle(Paint.Style.STROKE);
|
|
|
+ if (dark) {
|
|
|
+ borderPaint.setColor(colorDark);
|
|
|
+ textPaint.setColor(colorDark);
|
|
|
+ } else {
|
|
|
+ borderPaint.setColor(colorLight);
|
|
|
+ textPaint.setColor(colorLight);
|
|
|
+ }
|
|
|
}
|
|
|
+
|
|
|
borderPath.reset();
|
|
|
borderPath.moveTo(r + borderWidth / 2, borderWidth / 2);
|
|
|
+ borderPath.lineTo(r + borderWidth / 2, borderWidth / 2);
|
|
|
borderPath.lineTo(width - r, borderWidth / 2);
|
|
|
borderPath.arcTo(width - d - borderWidth / 2, borderWidth / 2, width - borderWidth / 2, height - borderWidth / 2, -90, 180, true);
|
|
|
borderPath.lineTo(r + borderWidth / 2, height - borderWidth / 2);
|
|
|
@@ -187,13 +220,13 @@ public class UnlockView extends View {
|
|
|
if (down) {
|
|
|
distance += event.getX() - lastX;
|
|
|
lastX = event.getX();
|
|
|
- postInvalidate();
|
|
|
if (distance >= getWidth() - padding * 2 - (getHeight() - padding * 2) && !unlocked) {
|
|
|
unlocked = true;
|
|
|
if (unlockListener != null) {
|
|
|
unlockListener.onUnlock();
|
|
|
}
|
|
|
}
|
|
|
+ postInvalidate();
|
|
|
}
|
|
|
break;
|
|
|
}
|