Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to draw Nine Palace Lattice unlock Control by Android

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Most people don't understand the knowledge points of this "Android how to draw Jiugong grid unlock control" article, so Xiaobian summarizes the following contents for everyone. The contents are detailed, the steps are clear, and they have certain reference value. I hope everyone can gain something after reading this article. Let's take a look at this "Android how to draw Jiugong grid unlock control" article.

The code for custom controls is as follows:

public class LockedView extends View { private boolean isFirst = true;//Set to true for the first load, and redraw later will not be executed private int width, height;//Gets the width and height of the control private int offsetX, offsetY;//Get the offset of the X and Y axes for point coordinates private Point[][] pointList;//array of coordinates for each point private int r;//radius of each circle private Bitmap map1, map2, map3;//Bitmap of 3 states private float eventX, eventY;//X and Y coordinates of the control when touched private boolean isPressed;//Determines whether the control is touched private boolean moveOnPoint;//Determine whether to move to a point private boolean isFinish;//Determine whether the gesture ends private List = new ArrayList();//Store the collection of points passed private Point checkedPoint; private Paint paint;//Brush public static final int LOCKED_FIRST=0;//Return value of password set when acitivity is first loaded public static final int LOCKED_TRUE=1;//Return value if unlock succeeded public static final int LOCKED_FALSE=2;//unlock failed return value private OnLockedChangedListener onLocked;//interface callback public LockedView(Context context, AttributeSet attrs) { super(context, attrs); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); //determine whether the first load if (isFirst) { //Initialize point coordinates initPoints(); //initialize brush initPaint(); isFirst = false; } //Draw a bitmap based on the state of each point drawPoints(canvas); //Draw the line during the gesture slide drawLines(canvas); } //initialize brush private void initPaint() { paint = new Paint(Paint.ANTI_ALIAS_FLAG); //Set brush color to blue paint.setColor(getResources().getColor(R.color.LockedColor)); //Set brush width to 8 paint.setStrokeWidth(8); } //Draw a line private void drawLines(Canvas canvas) { //if the collection has a value if (list.size() > 0) { //Get an instance of the starting point Point startPoint = list.get(0); for (int i = 1; i

< list.size(); i++) { //获得停止点的实例 Point stopPoint = list.get(i); //根据起始点坐标跟停止点坐标来画线 canvas.drawLine(startPoint.getX(), startPoint.getY(), stopPoint.getX(), stopPoint.getY(), paint); //把停止点赋值给起始点,这样每次遍历的时候起始点都是上一个点 startPoint = stopPoint; } //如果没有移动到一个点上 if (moveOnPoint == false) { //则根据最后个点的坐标跟当前手势移动的坐标画线 canvas.drawLine(startPoint.getX(), startPoint.getY(), eventX, eventY, paint); } } } //设置触摸事件 @Override public boolean onTouchEvent(MotionEvent event) { //获得触摸的X坐标 eventX = event.getX(); //获得触摸的Y坐标 eventY = event.getY(); //每次触摸到离开屏幕之前都默认为没有完成 isFinish = false; //默认移动到点上了 moveOnPoint = true; //选中的点 checkedPoint = null; switch (event.getAction()) { case MotionEvent.ACTION_DOWN: //如果按下,重置下点 reset(); //根据触摸的X,Y坐标以及圆的半径来判断是否触摸到了一个圆上,如果有则返回实例,没有则返回空 checkedPoint = checkPoint(eventX, eventY, r); if (checkedPoint != null) { //如果实例不为空,则设置选中点的状态为选中 checkedPoint.setState(Point.POINT_XUANZHONG); //第一次按下按到点上时设置为true isPressed = true; } break; case MotionEvent.ACTION_MOVE: //如果按下在一个点上,就会执行移动动作的逻辑 if (isPressed) { //同上 checkedPoint = checkPoint(eventX, eventY, r); if (checkedPoint != null) { checkedPoint.setState(Point.POINT_XUANZHONG); //如果实例不为空,则设置移动到了点上 moveOnPoint = true; } else { //否则设置没有移动到点上 moveOnPoint = false; } } break; case MotionEvent.ACTION_UP: //抬起时,设置第一次按在点上的参数为false以及完成了触摸过程 isPressed = false; isFinish = true; break; case MotionEvent.ACTION_CANCEL: isPressed = false; isFinish = true; break; } //如果第一下按在了点上并且没有完成触摸并且选中的点的实例不为空 if (isPressed && !isFinish && checkedPoint != null) { //判断这个实例是否在list集合中 if (isInList(checkedPoint)) { //如果在,则设置没有移动在点上 moveOnPoint = false; } else { //否则就添加到集合里面 list.add(checkedPoint); } //如果完成了触摸 } else if (isFinish) { if (list.size() >

0) { //If the set length is 1, it means that only one point has been touched and reset directly. if(list.size()==1){ reset(); //If the collection length is less than 5, it means that the password is too short to meet the requirements, set the selected point to the error state, and return the data through the interface callback }else if(list.size()=5){ StringBuffer buffer=new StringBuffer(); for(int i=0;i

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report