Android customizes the clickable imagespan and builds it in textview
Sometimes you may want to add some pictures to the textview, such as the following figure. When you send a text message to enter a contact, you need to change the contact number into a picture, but this picture cannot use a fixed picture, but is customized according to the content, which is more like a view.
Of course, if you are not a view, but a fixed picture, such as sending messages with emoticons instead of special symbols, it will be easier to implement. Or maybe you want the picture to be clickable. Here, the author wants to introduce how to use a custom imagespan to insert clickable pictures or views into the text.
Before that, if you don't know spannablestring. Setspan(), and what linkmovementmethod is, it's recommended to see the writer's resolution, URL in textview, etc. to specify special strings and click events
First, because imagespan does not inherit clickablespan, there is no onclick () method. So I wrote a clickableimagespan.
At the same time, we found that the linkmovementmethod provided by Google only executes the onclick () method of clickablespan. The following is the source code of ontouchevent () of linkmovementmethod. This method responds when we click spanned.
It is found that this method is actually to find the corresponding span through coordinates. Then, when the link array is not empty, it will get span and execute its onclick () method. Here we notice this code
This shows that this method only obtains clickablespan, because if we directly use the linkmovementmethod class of the system, we cannot make imagespan respond to click events.. Because we know that imagespan does not inherit clickablespan. Therefore, the author wrote a clickablemovementmethod
Only minor changes have been made, so that this class can support both clickablespan and clickableimagespan written by ourselves.
So far, a clickable imagespan is completed. The remaining steps are the same as the way to implement the text style. First, new a spannablestring to pass in the text, then find the location where you need to place the imagespan (usually using regular expressions), then new a clickableimagespan to pass in the image, and pass in the clickableimagespan object through the setspan () method of spannablestring. Finally, don't forget that when textview calls setmovementmethod, our clickablemovementmethod. GetInstance () method is passed in. The specific code is implemented by referring to the text style, which can be modified slightly. The specific author will no longer post this part of the code.
So, what should we do if we need to display a customized view instead of passing a simple picture. In fact, just convert the view into drawable. The following is the main implementation code:
The createdrawable () method converts a view into a bitmap through the getdrawingcache () method of the view, and then after obtaining bitmapdrawable, don't forget to call setbounds (). This method determines the size of the picture. If it is not set, the length and width of the picture will be 0! Of course, if you don't think the display effect is too big or too small, you can also adjust the picture size by this method. In other steps, I believe you have seen the author's analysis of URLs in textview, specifying special strings and click events, and there should be no difficulty in implementation. Therefore, the author will not repeat.
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.