怎么在android中通过自定义View实现手势解锁功能
这篇文章将为大家详细讲解有关怎么在android中通过自定义View实现手势解锁功能,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。
创新互联主要从事成都网站建设、网站制作、网页设计、企业做网站、公司建网站等业务。立足成都服务陆丰,10年网站建设经验,价格优惠、服务专业,欢迎来电咨询建站服务:13518219792
package com.example.gesturelock; import java.util.ArrayList; import java.util.List; import java.util.Timer; import java.util.TimerTask; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Path; import android.util.AttributeSet; import android.view.MotionEvent; import android.view.View; import com.example.gesturelock.GestureLockView.OnGestureFinishListener; public class MyGestureLockView extends View { /** * 不同状态的画笔 */ private Paint paintNormal; private Paint paintOnTouch; private Paint paintInnerCycle; private Paint paintLines; private Paint paintKeyError; private MyCycle[] cycles; private Path linePath = new Path(); private ListlinedCycles = new ArrayList (); private OnGestureFinishListener onGestureFinishListener; private String key; private int eventX, eventY; private boolean canContinue = true; private boolean result; private Timer timer; /** * 不同状态下的色值 */ private int OUT_CYCLE_NORMAL = Color.rgb(108, 119, 138); // ������Բ��ɫ private int OUT_CYCLE_ONTOUCH = Color.rgb(025, 066, 103); // ѡ����Բ��ɫ private int INNER_CYCLE_ONTOUCH = Color.rgb(002, 210, 255); // ѡ����Բ��ɫ private int LINE_COLOR = Color.argb(127, 002, 210, 255); // ��������ɫ private int ERROR_COLOR = Color.argb(127, 255, 000, 000); public void setOnGestureFinishListener( OnGestureFinishListener onGestureFinishListener) { this.onGestureFinishListener = onGestureFinishListener; } public void setKey(String key) { this.key = key; } public MyGestureLockView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); init(); } public MyGestureLockView(Context context, AttributeSet attrs) { super(context, attrs); init(); } public MyGestureLockView(Context context) { super(context); init(); } private void init() { paintNormal = new Paint(); paintNormal.setAntiAlias(true); paintNormal.setStrokeWidth(3); paintNormal.setStyle(Paint.Style.STROKE); paintOnTouch = new Paint(); paintOnTouch.setAntiAlias(true); paintOnTouch.setStrokeWidth(3); paintOnTouch.setStyle(Paint.Style.STROKE); paintInnerCycle = new Paint(); paintInnerCycle.setAntiAlias(true); paintInnerCycle.setStyle(Paint.Style.FILL); paintLines = new Paint(); paintLines.setAntiAlias(true); paintLines.setStyle(Paint.Style.STROKE); paintLines.setStrokeWidth(6); paintKeyError = new Paint(); paintKeyError.setAntiAlias(true); paintKeyError.setStyle(Paint.Style.STROKE); paintKeyError.setStrokeWidth(3); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { // TODO Auto-generated method stub super.onMeasure(widthMeasureSpec, heightMeasureSpec); } @Override protected void onLayout(boolean changed, int left, int top, int right, int bottom) { // TODO Auto-generated method stub super.onLayout(changed, left, top, right, bottom); int perSize = 0; if (cycles == null && (perSize = getWidth() / 6) > 0) { cycles = new MyCycle[9]; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { MyCycle cycle = new MyCycle(); cycle.setNum(i * 3 + j); cycle.setOx(perSize * (j * 2 + 1)); cycle.setOy(perSize * (i * 2 + 1)); cycle.setR(perSize * 0.5f); cycles[i * 3 + j] = cycle; } } } } /** * 绘制所需要绘制的内容 */ @Override protected void onDraw(Canvas canvas) { // TODO Auto-generated method stub super.onDraw(canvas); for (int i = 0; i < cycles.length; i++) { if (!canContinue && !result) { paintOnTouch.setColor(ERROR_COLOR); paintInnerCycle.setColor(ERROR_COLOR); paintLines.setColor(ERROR_COLOR); } else if (cycles[i].isOnTouch()) { paintOnTouch.setColor(OUT_CYCLE_ONTOUCH); paintInnerCycle.setColor(INNER_CYCLE_ONTOUCH); paintLines.setColor(LINE_COLOR); } else { paintNormal.setColor(OUT_CYCLE_NORMAL); paintInnerCycle.setColor(INNER_CYCLE_ONTOUCH); paintLines.setColor(LINE_COLOR); } if (cycles[i].isOnTouch()) { canvas.drawCircle(cycles[i].getOx(), cycles[i].getOy(), cycles[i].getR(), paintOnTouch); drawInnerBuleCycle(cycles[i], canvas); } else { canvas.drawCircle(cycles[i].getOx(), cycles[i].getOy(), cycles[i].getR(), paintNormal); } } drawLine(canvas); } /** * 绘制大圆里的小圆 * * @param canvas */ private void drawInnerBuleCycle(MyCycle cycle, Canvas canvas) { canvas.drawCircle(cycle.getOx(), cycle.getOy(), cycle.getR() / 3, paintInnerCycle); } private void drawLine(Canvas canvas) { linePath.reset(); if (linedCycles.size() > 0) { for (int i = 0; i < linedCycles.size(); i++) { int index = linedCycles.get(i); if (i == 0) { // 设置为整条路径的起点 linePath.moveTo(cycles[index].getOx(), cycles[i].getOy()); } else { linePath.lineTo(cycles[i].getOx(), cycles[i].getOy()); } } linePath.lineTo(eventX, eventY); canvas.drawPath(linePath, paintLines); } } /** * 根据手择时触摸点的不同,修改对应的状态值 */ @Override public boolean onTouchEvent(MotionEvent event) { if (canContinue) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: case MotionEvent.ACTION_MOVE: eventX = (int) event.getX(); eventY = (int) event.getY(); for (int i = 0; i < cycles.length; i++) { if (cycles[i].isPointIn(eventX, eventY)) { cycles[i].setOnTouch(true); if (!linedCycles.contains(cycles[i].getNum())) { linedCycles.add(cycles[i].getNum()); } } } break; case MotionEvent.ACTION_UP: canContinue = false; StringBuffer sb = new StringBuffer(); for (int i = 0; i < linedCycles.size(); i++) { sb.append(linedCycles.get(i)); } result = key.equals(sb.toString()); if (onGestureFinishListener != null) { onGestureFinishListener.OnGestureFinish(result); } timer = new Timer(); timer.schedule(new TimerTask() { @Override public void run() { // 回到初始状态 eventX = eventY = 0; for (int i = 0; i < cycles.length; i++) { cycles[i].setOnTouch(false); } linedCycles.clear(); linePath.reset(); canContinue = true; postInvalidate(); } }, 1000); break; } } invalidate(); return true; } }
自定义圆类:
package com.example.gesturelock; public class MyCycle { private int ox; // Բ�ĺ����� private int oy; // Բ�������� private float r; // �뾶���� private Integer num; // ������ֵ private boolean onTouch; // false=δѡ�� public int getOx() { return ox; } public void setOx(int ox) { this.ox = ox; } public int getOy() { return oy; } public void setOy(int oy) { this.oy = oy; } public float getR() { return r; } public void setR(float r) { this.r = r; } public Integer getNum() { return num; } public void setNum(Integer num) { this.num = num; } public boolean isOnTouch() { return onTouch; } public void setOnTouch(boolean onTouch) { this.onTouch = onTouch; } public boolean isPointIn(int x, int y) { double distance = Math.sqrt((x - ox) * (x - ox) + (y - oy) * (y - oy)); return distance < r; } }
Android是什么
Android是一种基于Linux内核的自由及开放源代码的操作系统,主要使用于移动设备,如智能手机和平板电脑,由美国Google公司和开放手机联盟领导及开发。
关于怎么在android中通过自定义View实现手势解锁功能就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。
当前题目:怎么在android中通过自定义View实现手势解锁功能
文章路径:http://azwzsj.com/article/ggesph.html