Android uses custom attributes to automatically play and scroll pictures

Hello, everyone. I remember that last time I took you to realize an effect similar to the picture scrolling player in Taobao client, but after I finished it, I found that I forgot to add the function of automatic picture playback (maybe I forgot to add it on purpose.....). As a result, the picture can only be played by sliding my fingers. So today I will lead you again to add the missing functions to make our picture player more perfect.

This program development will be completely based on the last code. If a friend has not read the previous article, please read the implementation code for Android to realize the functions of picture scrolling and tab controls.

Since we want to add the function of automatic playback, there is a very important problem to consider. What should I do if I have scrolled to the last picture? Because our current implementation scheme is that all pictures are arranged horizontally according to the order defined in the layout file, and then decide which picture to display by offsetting the leftmargin of the first picture. Therefore, when the picture scrolls to the last one, we can let the program quickly roll back to the first picture, and then scroll from the beginning. This effect is different from Taobao client (Taobao does not have a rollback mechanism, but naturally scrolls from the last picture to the first picture). I have also studied the implementation method of Taobao picture scroller, which is not difficult to implement. However, since we are developing based on the last code, we can't achieve the same effect as Taobao client in the scheme, so we don't pursue full consistency with it here. Each style is also very good.

OK, now start to implement the function. First, open slidingswitcherview and add a new asynctask to roll back to the first image:

Then add a new method in slidingswitcherview:

This method is very simple. We start the new scrolltofirstitemtask and set the scrolling speed to 20 * itemcount. In this way, the more pictures we need to scroll, the faster the rollback speed will be. After defining this method, you can immediately roll back from the current image to the first image as long as you call the scrolltofirstitem method anywhere.

OK, then we will define a method to start the autoplay function. The following code is still added in slidingswitcherview:

We can see that a timer is enabled in this method and will be executed every three seconds. Then judge whether the current picture is the last one in the execution logic of the timer. If it is not the last one, scroll to the next picture. If it is the last one, roll back to the first picture. It should be noted that the code in the timer runs in the sub thread, and the operations of rolling pictures and updating tabs are UI operations, so they need to be executed in the handler.

After that, just call the startautoplay method of slidingswitcherview when the activity is created, and the autoplay function will be realized!!

It's over? Naive!! If it's over, how can it live up to everyone's expectations? Such a simple function is weak in terms of an article.

The next is the focus of today. We need to use custom attributes to enable the autoplay function, so as to make you closer to experts and play Android more.

Let's continue. Create a new attrs.xml file in the RES / values directory and add the code:

Where, auto_ Play is the attribute name we will use, and the format is Boolean. The value of slidingswitcherview is optional, and the corresponding ID needs to be referenced in the code.

Then rewrite the constructor of slidingswitcherview and add the code to get custom attributes from the layout file:

As you can see, we get auto in the constructor_ If the value of play is true, the startautoplay method is called to enable the function of automatic playback.

Next is the moment to witness miracles! Let's open activity_ Main.xml, add two lines of key code. Add myattr: Auto in the outermost LinearLayout and in our custom com.example.viewswitcher.slidingswitcherview_ play="true"。 The complete XML code is as follows:

That is, we just need to set myattr: Auto_ If play is equal to true or false, you can decide whether to enable the automatic playback function, which is very simple and convenient.

Well, that's the end of today's explanation. If you have any questions, please leave a message below.

Please click here to download the source code

The above is the whole content of this article. I hope the content of this article has a certain reference value for your study or work. If you have any questions, you can leave a message. Thank you for your support for 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
分享
二维码
< <上一篇
下一篇>>