Hello everyone, remember last time I brought you together to achieve a similar with Taobao client with a picture scrolling player effect, but after finishing, I found that I forgot to add the function of the picture auto play (perhaps I intentionally forgot to add ...), the result picture can only slide through the finger to play. So today I will lead you again, add the function of the omission before, let our picture player more perfect.
The program development will be based entirely on the previous code, if a friend has not read the previous article, please read the Android implementation of the picture scrolling control, including the page signing function, so that your application like Taobao as Hyun-dazzle up.
Now that you're joining AutoPlay, there's a very important issue to consider. What should I do if I'm currently scrolling to the last picture? Because our current implementation is that all of the pictures are arranged horizontally according to the order defined in the layout file, and then you decide which one to display by offsetting the leftmargin of the first picture. So when the picture scrolls to the last one, we can get the program to roll back to the first picture quickly and then scroll from the beginning. This effect and Taobao client is a certain difference (Taobao and no rollback mechanism, but very naturally from the last picture to scroll to the first picture), I have also studied Taobao picture of the implementation of the scroll, it is not difficult to achieve. But because we are based on the last code development, the scheme can not achieve and Taobao client the same effect, so here is not the pursuit and it is completely consistent, each has a good style.
OK, now it's time to implement the function, first of all, to open the Slidingswitcherview and add a new asynctask in it, specifically for rolling back to the first picture:
class Scrolltofirstitemtask extends Asynctask<integer, Integer, integer> {@Override Protec
Ted integer doinbackground (integer ... speed) {int leftMargin = Firstitemparams.leftmargin;
while (true) {LeftMargin = LeftMargin + speed[0];
When the LeftMargin is greater than 0 o'clock, the description has scrolled to the first element, jumping out of the loop if (LeftMargin > 0) {leftMargin = 0;
Break
} publishprogress (LeftMargin);
Sleep (20);
return leftMargin; } @Override protected void Onprogressupdate (Integer ... leftMargin) {Firstitemparams.leftmargin
= Leftmargin[0];
Firstitem.setlayoutparams (Firstitemparams); } @Override protected void OnPostExecute (Integer leftMargin) {firstitemparams.leftmargin = Lef
Tmargin;
Firstitem.setlayoutparams (Firstitemparams); }
}
Then add a new method to the Slidingswitcherview:
/**
* Scrolls to the first element.
*/public
void Scrolltofirstitem () {
new Scrolltofirstitemtask (). Execute (* itemscount);
This method is very simple, is to start our new scrolltofirstitemtask, scrolling speed set to Itemscount, so that when we need to scroll the number of pictures, the rollback speed will be faster. By defining this method, you can immediately roll back from the current picture to the first picture as long as you call the Scrolltofirstitem method anywhere.
See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/OS/extra/