"Reprint" Android Metro Style Launcher Development Series second article

Source: Internet
Author: User
Tags home screen

Preface:dear friends, please forgive me for so long to start writing this series of the second blog, no way to busy new product release, good nonsense not to say, first review: In my previous blog Android Metro style Launcher Development Series first Wrote how to configure the Android development environment, just with text and pictures to show the development of Metro style Launcher preliminary design and product requirements, this article will be on the code to explain how to achieve the corresponding UI effect, OK, storytelling! Launcher Main Framework implementation: Launcher Main Frame I choose the familiar Viewpager control, because Viewpager can easily do pager between the switch animation, animation can be customized, so easy to get rid of the various effects slide out of the screen, You can also control the switching speed, which makes it easy to achieve the following effects:
  1. sliding speed control:

    This is done by getting Viewpager's scroller, setting some parameters on Scroller, and implementing the Code as follows:

    [Java]View Plaincopy < param name= "allowfullscreen" value= "false" >< param name= "wmode" value= "Transparent" >
      1. Field Scroller = Viewpager.  Class.getdeclaredfield ("Mscroller");
      2. Scroller.setaccessible (true);
      3. Interpolator interpolator = new Linearinterpolator (); Set up accelerators
      4. Viewpagerscroller scroller = new Viewpagerscroller (context,interpolator); Reset Viewpager's scroller
      5. Scroller.set (this, Scroller);

    In the above code is used in the Viewpagerscroller.java, in the Viewpagerscroller is set as follows, where the mduration variable is a custom animation time, this you can set the length of time according to their own animation effect, I define this as 500 milliseconds.

    [Java]View Plaincopy
    1. @Override
    2. Public void Startscroll (int startX, int starty, int dx, int dy) {
    3. //Ignore received duration, use fixed one instead
    4. Super.startscroll (StartX, starty, dx, DY, mduration);
    5. }
    6. @Override
    7. Public void Startscroll (int startX, int starty, int dx, int dy, int duration) {
    8. //Ignore received duration, use fixed one instead
    9. Super.startscroll (StartX, starty, dx, DY, mduration);
    10. }

  2. Page switching effect implementation: each screen page between the switch is implemented by implementing the Viewpager.pagetransformer interface, the specific explanation of this interface I do not introduce here, you can refer to Google official documents: Using Viewpager for Screen Slides. My implementation is as follows:
    [Java]View Plaincopy
    1. Class Launcherpagetransformer implements Viewpager.pagetransformer {
    2. private static float Default_scale = 1.0f;
    3. private static float scale_factor = 0.30f; Scaling factor 0.50f
    4. private static float rotation_factor = 20f; Rotation factor
    5. private static float alpha_factor = 0.8f;
    6. @Override
    7. public void Transformpage (view view, float position) {
    8. if (position <= 1) { //[ -1,1]
    9. //Modify The default slide transition to shrink the page as well
    10. if (position < 0) {
    11. //View.setrotationy (position * rotation_factor);
    12. View.setscalex (Scale_factor * position + Default_scale);
    13. View.setscaley (Scale_factor * position + Default_scale);
    14. //View.setalpha (Alpha_factor * position + 1.0f);
    15. } Else {
    16. //View.setrotationy (position * rotation_factor);
    17. View.setscalex (Scale_factor *-position + Default_scale);
    18. View.setscaley (Scale_factor *-position + Default_scale);
    19. //View.setalpha (Alpha_factor *-position + 1.0f);
    20. }
    21. }
    22. }
    23. }
    Specific effects can be achieved by modifying the rotation and scaling.
  3. left and right side page hover implementation:

    the page section on both sides of the Home screen page can be implemented by setting the Viewpager setpagemargin (int margin) method, with the following code:

    [Java]View Plaincopy
    1. Public class Launcherviewpager extends Viewpager {
    2. public static final int page_limit = 3;
    3. Public Launcherviewpager (context context) {
    4. This (context, null);
    5. }
    6. Public Launcherviewpager (context context, AttributeSet attrs) {
    7. Super (context, attrs);
    8. Init (context);
    9. }
    10. private void init (context context) {
    11. This.setpagemargin (-getresources (). Getinteger (R.integer.portal_viewpager_margin));
    12. This.setoffscreenpagelimit (Page_limit);
    13. This.setpagetransformer (True, new Launcherpagetransformer ());
    14. try {
    15. Field Scroller = Viewpager.  Class.getdeclaredfield ("Mscroller");
    16. Scroller.setaccessible (true);
    17. Interpolator interpolator = new Linearinterpolator ();
    18. Viewpagerscroller scroller = new Viewpagerscroller (context,
    19. Interpolator);
    20. Scroller.set (this, Scroller);
    21. } catch (Nosuchfieldexception e) {
    22. } catch (IllegalArgumentException e) {
    23. } catch (Illegalaccessexception e) {
    24. }
    25. }
    26. }
  4. Summarize:

    The above is the launcher main frame using Viewpager to achieve left and right slide and zoom effect explanation, write bad place also please point out and criticize, "three people will have my teacher", any criticism and suggestions I will have to reply and exchange, can add my number, faster communication. Next blog I will explain each of the screen Cellview implementation, with focus amplification effect, to achieve each focus of the cellview suspension effect and in the XML file flexible configuration links and so on.

The first time to get blog update reminders, as well as more technical information sharing, welcome to the personal public platform: Programmer Interaction Alliance (coder_online), sweep the QR code below or search number Coder_online can pay attention to, we can communicate online.

"Reprint" Android Metro Style Launcher Development Series second article

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.