android九宫格锁屏控件使用详解
本文实例为大家分享了android九宫格锁屏控件的具体代码,供大家参考,具体内容如下
文章图片
代码:
public class LockView extends View {//半径private int radius; //中心小圆半径private int smallRadius; //一行个数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; //每个圆宽度private int everyWidth; //是否是选中绘制private boolean isSelect; //所有圆信息private List allCircleList = new ArrayList<>(); //选中圆的标志private ListselectList = 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); //设置为无锯齿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); //设置为无锯齿linePaint.setStrokeWidth(lineStrokeWidth); //线宽linePaint.setAlpha(150); linePaint.setStyle(Paint.Style.STROKE); }@Overrideprotected 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); }}}@Overrideprotected 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); }}}}}@Overridepublic 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; }/*** 计算控件宽高** @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; }/*** 计算是在哪个圆中** @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; }}
【android九宫格锁屏控件使用详解】以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
推荐阅读
- Android——Generics|Android——Generics 泛型
- Android混淆后日志查看方法
- 产品说明丨Android端使用MobPush快速集成方法
- Android中的Drawable(二)
- Android|Android 的进程间通信 Binder——AIDL的入门使用(三)
- 如何在|如何在 Android 手机上同时登陆两个微信号
- Android实现实时视频聊天功能|源码 Demo 分享
- 一步步探索学习Android|一步步探索学习Android Touch事件分发传递机制(三)
- Android系统优化Ninja加快编译
- 大数据|抖音 Android 性能优化系列(Java 锁优化)