網(wǎng)站首頁 編程語言 正文
本文實(shí)例為大家分享了android九宮格鎖屏控件的具體代碼,供大家參考,具體內(nèi)容如下
代碼:
public class LockView extends View {
? ? //半徑
? ? private int radius;
? ? //中心小圓半徑
? ? private int smallRadius;
? ? //一行個(gè)數(shù)
? ? private int column;
? ? //選中顏色
? ? private int selectColor;
? ? //未選中顏色
? ? private int normalColor;
? ? //陰影顏色
? ? private int shaderColor;
? ? //連線的顏色
? ? private int lineColor;
? ? //圓線寬
? ? private int circleStrokeWidth;
? ? //連線的線寬
? ? private int lineStrokeWidth;
? ? private Paint normalPaint;
? ? private Paint selectPaint;
? ? private Paint linePaint;
? ? private Paint centerPaint;
? ? private int width;
? ? //每個(gè)圓寬度
? ? private int everyWidth;
? ? //是否是選中繪制
? ? private boolean isSelect;
? ? //所有圓信息
? ? private List<Point> allCircleList = new ArrayList<>();
? ? //選中圓的標(biāo)志
? ? private List<Integer> selectList = new ArrayList<>();
? ? //是否是重置
? ? private boolean isReSet;
? ? private LockViewFinishListener lockViewFinishListener;
? ? public LockView(Context context, @Nullable AttributeSet attrs) {
? ? ? ? super(context, attrs);
? ? ? ? init(context, attrs);
? ? }
? ? public LockViewFinishListener getLockViewFinishListener() {
? ? ? ? return lockViewFinishListener;
? ? }
? ? public void setLockViewFinishListener(LockViewFinishListener lockViewFinishListener) {
? ? ? ? this.lockViewFinishListener = lockViewFinishListener;
? ? }
? ? private void init(Context context, AttributeSet attrs) {
? ? ? ? TypedArray typedArray = context.obtainStyledAttributes(attrs,R.styleable.LockView);
? ? ? ? radius = typedArray.getInteger(R.styleable.LockView_lock_radius,100);
? ? ? ? smallRadius = typedArray.getInteger(R.styleable.LockView_smallRadius,30);
? ? ? ? column = typedArray.getInteger(R.styleable.LockView_column,3);
? ? ? ? selectColor =typedArray.getColor(R.styleable.LockView_selectColor,Color.RED);
? ? ? ? normalColor = typedArray.getColor(R.styleable.LockView_lock_normalColor,Color.GRAY);
? ? ? ? shaderColor = typedArray.getColor(R.styleable.LockView_shaderColor,Color.argb(80, 0xff, 0x00, 0x00));
? ? ? ? lineColor = typedArray.getColor(R.styleable.LockView_lineColor,Color.RED);
? ? ? ? circleStrokeWidth = typedArray.getInteger(R.styleable.LockView_circleStrokeWidth,5);
? ? ? ? lineStrokeWidth = typedArray.getInteger(R.styleable.LockView_lineStrokeWidth,15);
? ? ? ? normalPaint = new Paint();
? ? ? ? normalPaint.setColor(normalColor);
? ? ? ? normalPaint.setAntiAlias(false);//設(shè)置為無鋸齒
? ? ? ? normalPaint.setStrokeWidth(circleStrokeWidth);//線寬
? ? ? ? normalPaint.setStyle(Paint.Style.STROKE);
? ? ? ? selectPaint = new Paint();
? ? ? ? selectPaint.setColor(selectColor);
? ? ? ? selectPaint.setAntiAlias(false);
? ? ? ? selectPaint.setStrokeWidth(circleStrokeWidth);
? ? ? ? selectPaint.setStyle(Paint.Style.STROKE);
? ? ? ? centerPaint = new Paint();
? ? ? ? centerPaint.setColor(selectColor);
? ? ? ? centerPaint.setAntiAlias(false);
? ? ? ? centerPaint.setStrokeWidth(radius - smallRadius);
? ? ? ? centerPaint.setStyle(Paint.Style.FILL_AND_STROKE);
? ? ? ? linePaint = new Paint();
? ? ? ? linePaint.setColor(lineColor);
? ? ? ? linePaint.setAntiAlias(false);//設(shè)置為無鋸齒
? ? ? ? linePaint.setStrokeWidth(lineStrokeWidth);//線寬
? ? ? ? linePaint.setAlpha(150);
? ? ? ? linePaint.setStyle(Paint.Style.STROKE);
? ? }
? ? @Override
? ? protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
? ? ? ? width = measureWidth(widthMeasureSpec);
? ? ? ? setMeasuredDimension(width, width);
? ? ? ? everyWidth = (width - getPaddingLeft() - getPaddingRight()) / column;
? ? ? ? allCircleList.clear();
? ? ? ? for (int i = 0; i < column; i++) {
? ? ? ? ? ? for (int j = 0; j < column; j++) {
? ? ? ? ? ? ? ? float cx = getPaddingLeft() + everyWidth / 2 * (2 * j + 1);
? ? ? ? ? ? ? ? float cy = getPaddingTop() + everyWidth / 2 * (2 * i + 1);
? ? ? ? ? ? ? ? Point point = new Point();
? ? ? ? ? ? ? ? point.cx = cx;
? ? ? ? ? ? ? ? point.cy = cy;
? ? ? ? ? ? ? ? allCircleList.add(point);
? ? ? ? ? ? }
? ? ? ? }
? ? }
? ? @Override
? ? protected void onDraw(Canvas canvas) {
? ? ? ? super.onDraw(canvas);
? ? ? ? for (int i = 0; i < allCircleList.size(); i++) {
? ? ? ? ? ? Point point = allCircleList.get(i);
? ? ? ? ? ? canvas.drawCircle(point.cx, point.cy, radius, normalPaint);
? ? ? ? }
? ? ? ? if (isReSet) {//重置
? ? ? ? ? ? isReSet = false;
? ? ? ? ? ? postInvalidate();
? ? ? ? } else {
? ? ? ? ? ? if (isSelect) {
? ? ? ? ? ? ? ? for (int i = 0; i < selectList.size(); i++) {
? ? ? ? ? ? ? ? ? ? int index = selectList.get(i);
? ? ? ? ? ? ? ? ? ? Point point = allCircleList.get(index);
? ? ? ? ? ? ? ? ? ? canvas.drawCircle(point.cx, point.cy, radius, selectPaint);
? ? ? ? ? ? ? ? ? ? Shader mShader = new RadialGradient(point.cx, point.cy, smallRadius, new int[]{selectColor, shaderColor},
? ? ? ? ? ? ? ? ? ? ? ? ? ? new float[]{0.9f, 1f}, Shader.TileMode.CLAMP);
? ? ? ? ? ? ? ? ? ? centerPaint.setShader(mShader);
? ? ? ? ? ? ? ? ? ? canvas.drawCircle(point.cx, point.cy, smallRadius, centerPaint);
? ? ? ? ? ? ? ? ? ? if (i >= 1) {
? ? ? ? ? ? ? ? ? ? ? ? int lastIndex = selectList.get(i - 1);
? ? ? ? ? ? ? ? ? ? ? ? Point lastPoint = allCircleList.get(lastIndex);
? ? ? ? ? ? ? ? ? ? ? ? canvas.drawLine(lastPoint.cx, lastPoint.cy, point.cx, point.cy, linePaint);
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? }
? ? }
? ? @Override
? ? public boolean onTouchEvent(MotionEvent event) {
? ? ? ? switch (event.getAction()) {
? ? ? ? ? ? case MotionEvent.ACTION_DOWN:
? ? ? ? ? ? ? ? isReSet = true;
? ? ? ? ? ? ? ? selectList.clear();
? ? ? ? ? ? ? ? int index = calculateWhich(event.getX(), event.getY());
? ? ? ? ? ? ? ? if (index != -1) {
? ? ? ? ? ? ? ? ? ? selectList.add(index);
? ? ? ? ? ? ? ? ? ? isSelect = true;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? case MotionEvent.ACTION_MOVE:
? ? ? ? ? ? ? ? index = calculateWhich(event.getX(), event.getY());
? ? ? ? ? ? ? ? if (index != -1) {
? ? ? ? ? ? ? ? ? ? if (!selectList.contains(index)) {
? ? ? ? ? ? ? ? ? ? ? ? selectList.add(index);
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? case MotionEvent.ACTION_UP:
? ? ? ? ? ? ? ? if (lockViewFinishListener != null) {
? ? ? ? ? ? ? ? ? ? StringBuffer result = new StringBuffer();
? ? ? ? ? ? ? ? ? ? for (int i = 0; i < selectList.size(); i++) {
? ? ? ? ? ? ? ? ? ? ? ? result.append(selectList.get(i));
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? lockViewFinishListener.onSuccess(result + "");
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? break;
? ? ? ? }
? ? ? ? postInvalidate();
? ? ? ? return true;
? ? }
? ? /**
? ? ?* 計(jì)算控件寬高
? ? ?*
? ? ?* @param widthMeasureSpec
? ? ?* @return
? ? ?*/
? ? private int measureWidth(int widthMeasureSpec) {
? ? ? ? int result;
? ? ? ? int specSize = MeasureSpec.getSize(widthMeasureSpec);
? ? ? ? int specMode = MeasureSpec.getMode(widthMeasureSpec);
? ? ? ? if (specMode == MeasureSpec.EXACTLY) {
? ? ? ? ? ? result = specSize;
? ? ? ? } else {
? ? ? ? ? ? result = getPaddingLeft() + getPaddingRight() + radius * 2 * column ;
? ? ? ? ? ? if (specMode == MeasureSpec.AT_MOST) {
? ? ? ? ? ? ? ? result = Math.min(result, specSize);
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? return result;
? ? }
? ? /**
? ? ?* 計(jì)算是在哪個(gè)圓中
? ? ?*
? ? ?* @return
? ? ?*/
? ? private int calculateWhich(float lx, float ly) {
? ? ? ? for (int i = 0; i < allCircleList.size(); i++) {
? ? ? ? ? ? Point point = allCircleList.get(i);
? ? ? ? ? ? if (lx > point.cx - radius && lx < point.cx + radius) {
? ? ? ? ? ? ? ? if (ly > point.cy - radius && ly < point.cy + radius) {
? ? ? ? ? ? ? ? ? ? return i;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? return -1;
? ? }
? ? public interface LockViewFinishListener {
? ? ? ? void onSuccess(String result);
? ? }
? ? private class Point {
? ? ? ? private float cx;
? ? ? ? private float cy;
? ? }
}
<!--九宮格鎖屏控件--> ? ? <declare-styleable name="LockView"> ? ? ? ? <!--大圓半徑--> ? ? ? ? <attr name="lock_radius" format="integer"/> ? ? ? ? <!--小圓半徑--> ? ? ? ? <attr name="smallRadius" format="integer"/> ? ? ? ? <!--一行個(gè)數(shù)--> ? ? ? ? <attr name="column" format="integer"/> ? ? ? ? <!--選中顏色--> ? ? ? ? <attr name="selectColor" format="color"/> ? ? ? ? <!--未選中顏色--> ? ? ? ? <attr name="lock_normalColor" format="color"/> ? ? ? ? <!--陰影顏色--> ? ? ? ? <attr name="shaderColor" format="color"/> ? ? ? ? <!--連線的顏色--> ? ? ? ? <attr name="lineColor" format="color"/> ? ? ? ? <!--圓線寬--> ? ? ? ? <attr name="circleStrokeWidth" format="integer"/> ? ? ? ? <!--連線的線寬--> ? ? ? ? <attr name="lineStrokeWidth" format="integer"/> </declare-styleable>
原文鏈接:https://blog.csdn.net/qq_31433525/article/details/84287048
相關(guān)推薦
- 2022-11-27 Git基礎(chǔ)學(xué)習(xí)之文件刪除操作命令詳解_相關(guān)技巧
- 2022-10-19 Android項(xiàng)目中引入aar包的正確方法介紹_Android
- 2022-12-25 go?slice不同初始化方式性能及數(shù)組比較詳解_Golang
- 2022-11-25 Python隨機(jī)值生成的常用方法總結(jié)_python
- 2022-06-17 Ruby序列化和持久化存儲(chǔ)(Marshal、Pstore)操作方法詳解_Golang
- 2022-06-12 詳解Flutter如何讀寫文本文件_Android
- 2023-06-20 k8s應(yīng)用監(jiān)控探針詳解_云其它
- 2022-08-10 Go?modules?replace解決Go依賴引用問題_Golang
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支