您好,登錄后才能下訂單哦!
這篇文章將為大家詳細講解有關怎么在android中通過自定義View實現手勢解鎖功能,文章內容質量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關知識有一定的了解。
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 List<Integer> linedCycles = new ArrayList<Integer>(); 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是一種基于Linux內核的自由及開放源代碼的操作系統,主要使用于移動設備,如智能手機和平板電腦,由美國Google公司和開放手機聯盟領導及開發。
關于怎么在android中通過自定義View實現手勢解鎖功能就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。