Android gesture password view learning notes (I)

When I first came into contact with Android, I saw the gesture password view written by others, and then I thought, when can I write such a high-end thing?? It doesn't matter. Don't be afraid. It's not that you don't have good technology, but that you don't want to spend time studying it. In fact, it's not so difficult (nothing in the world is difficult, just afraid of people with a heart!). Let's implement a gesture password step by step.

We must have seen the gesture password view, but let's take a look at the effect we want to achieve today:

The above is a gesture view prompt view, and the following is a gesture view.

Usage:

App starts with some custom attributes,

attrs.xml:

MainActivity.java:

It doesn't matter if we don't understand it. Let's clarify our goals first, and then achieve them step by step:

First implement our indicator view, because the implementation of indicator view is equivalent to half of the gesture password view:

Implementation idea:

1. We need to know how many rows and columns the indicator has, what is displayed by default, and what is displayed when selected? 2. Then, the corresponding points are displayed as selected according to the incoming password, and the unselected points are in the default state.

Well, you know our idea, first customize a view called indicatorview, inherit the view, and then rewrite the three construction methods:

Define custom attributes (create attrs.xml file under RES / values):

1. We need the default display picture passed in:

2. We need to get the incoming selected picture:

Other properties:

After defining the attribute, we can reference the custom view in the XML:

be careful:

The middle drawable file can be found in the GitHub project. I will give the link at the end of the article.

With custom attributes, we can obtain the custom attributes we passed in the layout file in the construction method with three parameters:

Well, now we have got what we want. Next, we need to know how big my view is. Compared with our little partners, do we know what to do next? Yes ~! We need to rewrite the onmeasure method and specify the size of our view:

So what rules should we use to specify the size of our view?

1. When the user specifies the size of the view, we use the size passed in by the user, and then calculate the size of our point according to the width and height.

2. If the user does not specify the size of the view, the width and height are set to wrap_ Content, we need to calculate the size of the view according to the size of the selected image and the unselected image passed in by the user:

Well, now that we know how to measure our view, we'll implement it next:

The width calculation method is the same, except that the number of rows is replaced by the number of columns:

OK, now we know the height and width of the point, and then the width and height of the control will naturally be known. But what if the size of the selected image is different from that of the unselected image? It doesn't matter. Next, let's revise the size of the picture:

OK, I got the picture. The width and height of the control and the width and height of the point are known, so it's time for us to enter our core code (rewrite the OnDraw method and draw our point):

Uh huh!! Then we expose a method to let the outside world pass in a set of passwords that need reality:

OK ~ ~ we're finished ~ ~ ~ isn't it very simple? After the indicator view is finished, think about the gesture password view. Is it just that it changes according to the gesture, and then draw our line?

The GitHub link of the project is attached: https://github.com/913453448/GestureContentView/

In the next section, we will implement the gesture password view together.

Android gesture password view notes (II)

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>