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:
- 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" >
- Field Scroller = Viewpager. Class.getdeclaredfield ("Mscroller");
- Scroller.setaccessible (true);
- Interpolator interpolator = new Linearinterpolator (); Set up accelerators
- Viewpagerscroller scroller = new Viewpagerscroller (context,interpolator); Reset Viewpager's scroller
- 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
- @Override
- Public void Startscroll (int startX, int starty, int dx, int dy) {
- //Ignore received duration, use fixed one instead
- Super.startscroll (StartX, starty, dx, DY, mduration);
- }
- @Override
- Public void Startscroll (int startX, int starty, int dx, int dy, int duration) {
- //Ignore received duration, use fixed one instead
- Super.startscroll (StartX, starty, dx, DY, mduration);
- }
- 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
- Class Launcherpagetransformer implements Viewpager.pagetransformer {
- private static float Default_scale = 1.0f;
- private static float scale_factor = 0.30f; Scaling factor 0.50f
- private static float rotation_factor = 20f; Rotation factor
- private static float alpha_factor = 0.8f;
- @Override
- public void Transformpage (view view, float position) {
- if (position <= 1) { //[ -1,1]
- //Modify The default slide transition to shrink the page as well
- if (position < 0) {
- //View.setrotationy (position * rotation_factor);
- View.setscalex (Scale_factor * position + Default_scale);
- View.setscaley (Scale_factor * position + Default_scale);
- //View.setalpha (Alpha_factor * position + 1.0f);
- } Else {
- //View.setrotationy (position * rotation_factor);
- View.setscalex (Scale_factor *-position + Default_scale);
- View.setscaley (Scale_factor *-position + Default_scale);
- //View.setalpha (Alpha_factor *-position + 1.0f);
- }
- }
- }
- }
Specific effects can be achieved by modifying the rotation and scaling.
- 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
- Public class Launcherviewpager extends Viewpager {
- public static final int page_limit = 3;
- Public Launcherviewpager (context context) {
- This (context, null);
- }
- Public Launcherviewpager (context context, AttributeSet attrs) {
- Super (context, attrs);
- Init (context);
- }
- private void init (context context) {
- This.setpagemargin (-getresources (). Getinteger (R.integer.portal_viewpager_margin));
- This.setoffscreenpagelimit (Page_limit);
- This.setpagetransformer (True, new Launcherpagetransformer ());
- try {
- Field Scroller = Viewpager. Class.getdeclaredfield ("Mscroller");
- Scroller.setaccessible (true);
- Interpolator interpolator = new Linearinterpolator ();
- Viewpagerscroller scroller = new Viewpagerscroller (context,
- Interpolator);
- Scroller.set (this, Scroller);
- } catch (Nosuchfieldexception e) {
- } catch (IllegalArgumentException e) {
- } catch (Illegalaccessexception e) {
- }
- }
- }
- 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