Viewpager Toggle Animation Pagetransformer Use

Source: Internet
Author: User

Android started from 3.0, added a lot of animations, viewpager of course, compared to the very mediocre default switch animation, Google showed us two animation examples: Depthpagetransformer and Zoomoutpagetransformer, in fact, I You can also make a completely different transition animation by implementing Viewpager.pagetransformer. The key is to understand the parameters of the Transformpage (view view, float position). View is naturally the slide in the view,position here is the float type, not the usual understanding of the int position, but the current sliding state of a representation, such as when sliding to the full screen, the position is 0, and left to slide, so that the right side of a screen is entered , position is 1, if the previous page and the next page in the basic screen in half, the previous page of position is-0.5, the next page of Posiotn is 0.5, so according to the value of position we can set the required alpha,x/y information.

Now, let's look at the code of two animated effects provided by Google, which is very simple, and I believe that after everyone has seen it, they can achieve an animated effect on their own.

Depthpagetransformer:

[Java]View Plaincopy
  1. Public class Depthpagetransformer implements Pagetransformer {
  2. private static float Min_scale = 0.75f;
  3. @SuppressLint ("Newapi")
  4. @Override
  5. public void Transformpage (view view, float position) {
  6. int pagewidth = View.getwidth ();
  7. if (Position <-1) { //[-infinity,-1]
  8. //This page was the off-screen to the left .
  9. View.setalpha (0);
  10. } Else if (position <= 0) { //[ -1,0]
  11. //Use of the default slide transition when
  12. //Moving to the left page
  13. View.setalpha (1);
  14. View.settranslationx (0);
  15. View.setscalex (1);
  16. View.setscaley (1);
  17. } Else if (position <= 1) { //(0,1]
  18. //Fade the page out.
  19. View.setalpha (1-position);
  20. //CounterAct the default slide transition
  21. View.settranslationx (PageWidth *-position);
  22. //Scale the PAGE down (between Min_scale and 1)
  23. float scalefactor = Min_scale + (1-min_scale)
  24. * (1-math.abs (position));
  25. View.setscalex (Scalefactor);
  26. View.setscaley (Scalefactor);
  27. } else { //(1,+infinity]
  28. //This page is a-off-screen to the right.
  29. View.setalpha (0);
  30. }
  31. }
  32. }


Zoomoutpagetransformer:

[Java]View Plaincopy
  1. Public class Zoomoutpagetransformer implements Pagetransformer {
  2. private static float Min_scale = 0.85f;
  3. private static float min_alpha = 0.5f;
  4. @Override
  5. public void Transformpage (view view, float position) {
  6. int pagewidth = View.getwidth ();
  7. int pageheight = View.getheight ();
  8. if (Position <-1) { //[-infinity,-1]
  9. //This page was the off-screen to the left .
  10. View.setalpha (0);
  11. } Else if (position <= 1) { //[ -1,1]
  12. //Modify The default slide transition to
  13. //Shrink the page as well
  14. float scalefactor = Math.max (Min_scale, 1-math.abs (position));
  15. float Vertmargin = pageheight * (1-scalefactor)/ 2;
  16. float Horzmargin = pagewidth * (1-scalefactor)/ 2;
  17. if (position < 0) {
  18. View.settranslationx (Horzmargin-vertmargin/ 2);
  19. } Else {
  20. View.settranslationx (-horzmargin + vertmargin/ 2);
  21. }
  22. //Scale the PAGE down (between Min_scale and 1)
  23. View.setscalex (Scalefactor);
  24. View.setscaley (Scalefactor);
  25. //Fade the page relative to its size.
  26. View.setalpha (Min_alpha + (Scalefactor-min_scale)
  27. /(1-min_scale) * (1-min_alpha));
  28. } else { //(1,+infinity]
  29. //This page is a-off-screen to the right.
  30. View.setalpha (0);
  31. }
  32. }
  33. }

With these two effects, we set it in the code to take effect: Mpager.setpagetransformer (True, New Depthpagetransformer ());

Here are: The first one is the Depthpagetransformer effect, the second is the Zoomoutpagetransformer effect.



OK, finally on the source code: http://download.csdn.net/detail/weidi1989/6443601

Viewpager Toggle Animation Pagetransformer Use

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.