Create a cool Android studio plug-in

After learning the basics of Android studio plug-ins in the previous articles, this article intends to develop a cool plug-in. Because the previous foundation will be used, if you don't read the previous series of articles, please return first. Of course, if there is a foundation, it can be ignored. Let's take a look at the final effect of this article as follows (well, many people say it's dazzling):

Although it has no practical use, it feels very interesting to develop as a learning plug-in.

1. Basic ideas

The basic idea can be summarized as follows:

1) Through the editor object, you can get the JComponent object that encapsulates the code edit box, that is, call the following function: JComponent component = editor. Getcontentcomponent();

2) , gets the character (or string) entered or deleted. If you delete or paste by selecting multiple characters, it is a string. You can listen for text changes by adding a documentlistener. Override the beforedocumentchange function and get the new and old characters through the documentevent object. Through the functions: documentevent. Getnewfragment(), documentevent. Getoldfragment(). They represent the input string and the deleted string.

3) , display the input or deleted string in the edit box. Just encapsulate each string into jlabel and add jlabel to JComponent to display the input or deleted string (or character).

4) . obtain the coordinate position of the jlabel object used to display each string in the JComponent. Add caretlistener to listen for cursor position. Each time the cursor position changes, it is refreshed to the temporary variable. When you want to add a jlabel, get the location saved in the current temporary variable, that is, the location where jlabel should be stored.

5) , animation effect. Start a thread. For the input string, you only need to constantly modify the font size. For deleted strings, constantly modify the location and font size of jlabel.

6) . save the plug-in status locally. The user clicks to open or close the plug-in and other switch options. It needs to be saved and can be restored the next time Android studio is opened. Just implement the persistentstatecomponent interface.

7) . when the user does not click action, the documentlistener can be registered automatically. This is mainly because the user has opened the plug-in. The next time he opens Android studio, he doesn't need to click aciton. When he directly enters, he can automatically register and listen to document changes. Because the editor object is required to register documentlistener, there are only two ways to obtain the editor object: through the GetData function of anactionevent object; The other is to use the platformdatakeys.editor.getdata (DataContext) method through the DataContext object. Obviously, the first method can only be obtained in the actionperformed and update methods of the anaction class. Therefore, only the second method can be considered. As described in the previous article, when listening for keyboard character input, you can obtain the DataContext object. That is, override the execute function of the typedactionhandler interface. The DataContext object is passed in the execute parameter.

It can be seen that the knowledge used above is the content introduced in the previous three articles and is not complex. Only Article 6 is not introduced. This article will learn about local persistent data.

2. Local persistence of plug-in state

Let's first look at how to implement local persistence. First, define a global shared variable class globalvar to implement the persistentstatecomponent interface. Let's have a visual understanding and look at the code directly.

Use the @ state annotation to specify the local storage location, ID, and so on. The specific implementation can basically be written with reference to this template, that is, rewriting the loadstate () and getstate () functions. In addition, you need to pay attention to the writing of the getInstance () function. That's the basic template. There's nothing special. Just follow the gourd and draw the gourd.

It is also important to remember to register this persistence class in plugin. XML. Find the < extensions > tag and add the < Applicationservice > sub tag as follows:

After writing this, when obtaining data, it is directly as follows:

3. Write action

It mainly includes two actions: enableaction and randomcoloraction. Enableaction is used to set the plug-in on or off, and randomcoloraction is used to set whether to use random colors. Since the two functions are similar, we only look at the implementation of enableaction:

The code is relatively simple, which is very similar to that written in the previous articles. Just notice that the actionPerformed function calls two functions:

The clearallstr() function in the charpanel object will be described later. Just know that it clears all animation objects in the cache. The registerdocumentlistener() function in the globalvar object adds a documentlistener listener. The center of realizing the effect of this paper is the documentlistener listener, which obtains the data to realize the effect of character animation by monitoring the changes of text content. Therefore, the documentlistener listener should be added as early as possible, and the moments when the documentlistener listener is added include: the user clicks action and the user types in characters. That is, it is possible to add a documentlistener listener in multiple places. Therefore, this function is extracted and added to globalvar. The specific implementation is as follows:

It can be seen that once the documentlistener listener is added, a thread will be started. This thread will always execute to achieve the animation effect. The documentlistener listener only needs to be joined once.

4. Realize animation

The charpanel object has been used many times before. The charpanel object is used to achieve animation effects. First source code:

Explain the two key functions run () and draw (). The run () function is a function that starts a new thread to execute. Its implementation is a loop. When the plug-in is started, it will run in a loop all the time. Charpanel uses two sets to keep the strings deleted or added by the user. Charset will be displayed directly, and bufferlist saves the input or deleted strings monitored by the documentlistener listener. Strings entered or deleted are encapsulated in the charobj class. Before each cycle in the run function, transfer all the data in the bufferlist to the charset. Why use two sets? This is mainly because when looping through the charset, if the change data monitored by the documentlistener is directly added to the charset, an error will occur. Because it is not allowed to add or delete elements in Java collections during traversal.

The run function calls the draw () function every time it loops. The draw () function resets jlabel's position attribute and font attribute according to the data encapsulated by charobj, which makes jlabel have animation effect, because the font size and position data will be gradually modified at the end of each loop of the run function.

5. Source code

Other codes are relatively simple, and it's not interesting to explain the code. Directly present the source code. If you have any doubts, please leave a message and I'll try to find time to reply one by one.

GitHub address: https://github.com/huachao1001/Amazing-Mode

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
分享
二维码
< <上一篇
下一篇>>