package ymm.learns.gesturepwd.view;
import static android.content.Context.MODE_PRIVATE;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.graphics.Paint.Cap;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import ymm.learns.gesturepwd.MainActivity;
import ymm.learns.gesturepwd.R;
import ymm.learns.gesturepwd.SetPwdResActivity;
public class NinePointLineView extends View {
Paint linePaint = new Paint();
Paint whiteLinePaint = new Paint();
Paint textPaint = new Paint();
Bitmap defaultBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.lock);
int defaultBitmapRadius = defaultBitmap.getWidth() / 2;
Bitmap selectedBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.indicator_lock_area);
int selectedBitmapDiameter = selectedBitmap.getWidth();
int selectedBitmapRadius = selectedBitmapDiameter / 2;
PointInfo[] points = new PointInfo[9];
PointInfo startPoint = null;
int width, height;
int moveX, moveY;
boolean isUp = false;
Context cxt;
StringBuffer lockString = new StringBuffer();
public NinePointLineView(Context context) {
super(context);
cxt = context;
// this.setBackgroundColor(Color.WHITE);//设置整个背景
initPaint();
}
public NinePointLineView(Context context, AttributeSet attrs) {
super(context, attrs);
// this.setBackgroundColor(Color.WHITE);//设置整个九宫格的背景
initPaint();
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// 解析测量规格中的模式和大小
int widthMode = MeasureSpec.getMode(widthMeasureSpec);
int widthSize = MeasureSpec.getSize(widthMeasureSpec);
int heightMode = MeasureSpec.getMode(heightMeasureSpec);
int heightSize = MeasureSpec.getSize(heightMeasureSpec);
// 根据需要计算视图的宽度和高度
if (widthMode == MeasureSpec.EXACTLY) {
// 如果宽度是精确值,则使用它
width = widthSize;
} else {
// 否则,根据内容计算宽度
width = 400;
}
Log.e("NinePointLine", " heightMode="+heightMode );
width = widthSize > 600 ? widthSize : 600;
height = heightSize > 600 ? heightSize : 600;//限制最小区域防止出错
Log.e("NinePointLine", "onMeasure: Line 53 and width="+width+" and height="+height );
if (width != 0 && height != 0) {
initPoints(points);
}
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
}
private int startX = 0, startY = 0;
@Override
protected void onDraw(Canvas canvas) {
//canvas.drawText("passwd:" + lockString, 0, 40, textPaint);
if (moveX != 0 && moveY != 0 && startX != 0 && startY != 0) {
drawLine(canvas, startX, startY, moveX, moveY);
}
drawNinePoint(canvas);
super.onDraw(canvas);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
boolean flag = true;
if (isUp) {
finishDraw();
flag = false;
} else {
handlingEvent(event);
flag = true;
}
return flag;
}
private void handlingEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_MOVE:
moveX = (int) event.getX();
moveY = (int) event.getY();
for (PointInfo temp : points) {
if (temp.isInMyPlace(moveX, moveY) && temp.isSelected() == false) {
temp.setSelected(true);
startX = temp.getCenterX();
startY = temp.getCenterY();
int len = lockString.length();
if (len != 0) {
int preId = lockString.charAt(len - 1) - 48;
points[preId].setNextId(temp.getId());
}
lockString.append(temp.getId());
break;
}
}
invalidate(0, height - width, width, height);
break;
case MotionEvent.ACTION_DOWN:
int downX = (int) event.getX();
int downY = (int) event.getY();
for (PointInfo temp : points) {
if (temp.isInMyPlace(downX, downY)) {
temp.setSelected(true);
startPoint = temp;
startX = temp.getCenterX();
startY = temp.getCenterY();
lockString.append(temp.getId());
break;
}
}
invalidate(0, height - width, width, height);
break;
case MotionEvent.ACTION_UP:
startX = startY = moveX = moveY = 0;
isUp = true;
invalidate();
savePwd();
break;
default:
break;
}
}
private void finishDraw() {
for (PointInfo temp : points) {
temp.setSelected(false);
temp.setNextId(temp.getId());
}
lockString.delete(0, lockString.length());
isUp = false;
invalidate();
}
private void initPoints(PointInfo[] points) {
int len = points.length;
int seletedSpacing = (width - selectedBitmapDiameter * 3) / 4;
int seletedX = seletedSpacing;
int seletedY = height - width + seletedSpacing;
int defaultX = seletedX + selectedBitmapRadius - defaultBitmapRadius;
int defaultY = seletedY + selectedBitmapRadius - defaultBitmapRadius;
for (int i = 0; i < len; i++) {
if (i == 3 || i == 6) {
seletedX = seletedSpacing;
seletedY += selectedBitmapDiameter + seletedSpacing;
defaultX = seletedX + selectedBitmapRadius
- defaultBitmapRadius;
defaultY += selectedBitmapDiameter + seletedSpacing;
}
points[i] = new PointInfo(i, defaultX, defaultY, seletedX, seletedY);
seletedX += selectedBitmapDiameter + seletedSpacing;
defaultX += selectedBitmapDiameter + seletedSpacing;
}
}
private void initPaint() {
Log.e("NinePointLine", "initPaint: 161" );
initLinePaint(linePaint);
initTextPaint(textPaint);
initWhiteLinePaint(whiteLinePaint);
}
/**
* @param paint
*/
private void initTextPaint(Paint paint) {
textPaint.setTextSize(30);
textPaint.setAntiAlias(true);
textPaint.setTypeface(Typeface.MONOSPACE);
}
/**
* @param paint
*/
private void initLinePaint(Paint paint) {
Log.e("NinePointLine", "initLinePaint: 178" );
paint.setColor(Color.GRAY);
paint.setStrokeWidth(30);//设置两个点之间的线的北京的宽度
paint.setAntiAlias(true);
paint.setStrokeCap(Cap.ROUND);
}
/**
* @param paint
*/
private void initWhiteLinePaint(Paint paint) {
pa
没有合适的资源?快使用搜索试试~ 我知道了~
android九宫格锁屏模块
共66个文件
xml:17个
webp:10个
properties:7个
需积分: 3 0 下载量 141 浏览量
2024-02-18
12:08:11
上传
评论
收藏 528KB ZIP 举报
温馨提示
android九宫格锁屏模块,使用原生的activity创建一个生成9宫格的基类,并用这种基类存储密码,并在最后显示设置的密码,代码开发在android studio2022.3.1,小米8,android api 26上测试通过
资源推荐
资源详情
资源评论
收起资源包目录
GesturePwd.zip (66个子文件)
.gradle
8.0
fileChanges
last-build.bin 1B
checksums
checksums.lock 17B
executionHistory
executionHistory.bin 983KB
executionHistory.lock 17B
dependencies-accessors
gc.properties 0B
dependencies-accessors.lock 17B
fileHashes
fileHashes.lock 17B
resourceHashesCache.bin 19KB
fileHashes.bin 69KB
gc.properties 0B
vcsMetadata
buildOutputCleanup
cache.properties 51B
buildOutputCleanup.lock 17B
outputFiles.bin 21KB
vcs-1
gc.properties 0B
file-system.probe 8B
gradle.properties 1KB
gradle
wrapper
gradle-wrapper.jar 58KB
gradle-wrapper.properties 236B
app
src
androidTest
java
ymm
learns
gesturepwd
ExampleInstrumentedTest.java 756B
test
java
ymm
learns
gesturepwd
ExampleUnitTest.java 382B
main
java
ymm
learns
gesturepwd
SetPwdResActivity.java 843B
view
NinePointLineView.java 13KB
MainActivity.java 2KB
res
mipmap-xxhdpi
ic_launcher_round.webp 6KB
ic_launcher.webp 3KB
mipmap-hdpi
ic_launcher_round.webp 3KB
ic_launcher.webp 1KB
values-night
themes.xml 812B
mipmap-mdpi
ic_launcher_round.webp 2KB
ic_launcher.webp 982B
mipmap-xxxhdpi
ic_launcher_round.webp 8KB
ic_launcher.webp 4KB
mipmap-anydpi
ic_launcher.xml 343B
ic_launcher_round.xml 343B
mipmap-xhdpi
ic_launcher_round.webp 4KB
ic_launcher.webp 2KB
xml
data_extraction_rules.xml 551B
backup_rules.xml 478B
values
colors.xml 378B
strings.xml 72B
themes.xml 812B
layout
activity_main.xml 1KB
activity_set_pwd_res.xml 463B
drawable
lock.png 2KB
ic_launcher_background.xml 5KB
indicator_lock_area.png 6KB
logo.png 42KB
ic_action_search.png 3KB
ic_launcher_foreground.xml 2KB
bg.png 172KB
ic_launcher.png 7KB
AndroidManifest.xml 1KB
proguard-rules.pro 750B
libs
build.gradle 1KB
.gitignore 6B
gradlew.bat 3KB
build.gradle 168B
.idea
workspace.xml 7KB
misc.xml 457B
compiler.xml 174B
gradle.xml 681B
.gitignore 50B
settings.gradle 328B
local.properties 417B
gradlew 6KB
.gitignore 225B
共 66 条
- 1
资源评论
sunbingzibo
- 粉丝: 93
- 资源: 23
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功