Android High Imitation QQ5.0 slide menu effect custom controls to attack

Source: Internet
Author: User

Reprint please indicate source: http://blog.csdn.net/lmj623565791/article/details/39257409, this article from "Zhang Hongyang's Blog"

The previous blog took everyone to achieve: Android custom control to create the history of the simplest side-by-side menu, a brother looked after said, you this sliding menu is outdated AH ~qq5.0 effect is also good ~ ~ Well, indeed, the last one also promised, slightly modify the previous code, to achieve QQ5.0 slide menu ~ ~ OK, The following will begin to show you how easy it is to write a class of QQ slide!

1. Principle Analysis

First of all, compared to the implementation of our last article from the QQ effect is still how far:

The gap is still pretty big.

Difference 1, QQ content area will accompany the appearance of the menu and reduce

Difference 2, QQ side-by-side menu to the person's feeling is hidden behind the content, rather than dragged out of the feeling

Difference 3, QQ side-slide menu has a zoom and transparency effect ~


So how can we do it:

For the difference 1: This is good, we can change the size of the content area while sliding, how to change it? Throughout the course of the menu, we constantly record the ratio of the width of the menu display to its total width, a process from 0 to 1, then convert the 0~1 to 1~0.7 (assuming the content area shrinks to 0.7);

For the difference 3: Also better to do, the above can be obtained from 0 to 1 of this value, then the zoom and transparency of the animation is a cinch;

For the Difference 2: We used the horizontalscrollview, and then horizontally placed the menu and content, how to let the menu can be hidden behind the content? In fact, it is relatively simple, in the course of the menu appears, constantly set the X-direction of the menu offset, 0 when completely hidden, 0.3, the hidden X-direction offset is 0.7 width, analogy ~ ~ ~


Well, after the analysis, what is the best way to achieve these animations?

No need to think, property animation ha, if you do not understand the property animation, you can see: Android Properties Animation (property Animation) Full Parse (top) and Android property animation (properties Animation) Full Parse (bottom)

2. Realize
1, the preliminary code

Layout file God horse, and the previous article is exactly the same, here does not repeat the code, do not know, first look at the next article;

Let's take a look at the complete code we've implemented:

Package Com.example.zhy_slidingmenu;import Android.content.context;import Android.content.res.typedarray;import Android.util.attributeset;import Android.util.typedvalue;import Android.view.motionevent;import Android.view.viewgroup;import Android.widget.horizontalscrollview;import Android.widget.linearlayout;import Com.zhy.utils.screenutils;public class Slidingmenu extends horizontalscrollview{/** * screen width */private int mScreenWidth;/ * * DP */private int mmenurightpadding;/** * menu width */private int mmenuwidth;private int Mhalfmenuwidth;private Boolean is Open;private Boolean Once;public Slidingmenu (context context, AttributeSet Attrs) {This (context, attrs, 0);} Public Slidingmenu (context context, AttributeSet attrs, int defstyle) {Super (context, attrs, defstyle); mscreenwidth = Screenutils.getscreenwidth (context); TypedArray a = Context.gettheme (). Obtainstyledattributes (Attrs,r.styleable.slidingmenu, Defstyle, 0); int n = A.getindexcount (); for (int i = 0; i < n; i++) {int attr = A.getindex (i);Switch (attr) {case r.styleable.slidingmenu_rightpadding://default 50mMenuRightPadding = A.getdimensionpixelsize (attr, ( int) typedvalue.applydimension (Typedvalue.complex_unit_dip, 50f,getresources (). Getdisplaymetrics ()));// Default is 10DPbreak;}} A.recycle ();} Public Slidingmenu (Context context) {This (context, NULL, 0);} @Overrideprotected void onmeasure (int widthmeasurespec, int heightmeasurespec) {/** * shows the setting of a width */if (!once) { LinearLayout wrapper = (linearlayout) getchildat (0); ViewGroup menu = (viewgroup) wrapper.getchildat (0); ViewGroup content = (viewgroup) wrapper.getchildat (1); mmenuwidth = Mscreenwidth-mmenurightpadding;mhalfmenuwidth = Mmenuwidth/2;menu.getlayoutparams (). Width = Mmenuwidth;content.getlayoutparams (). width = mscreenwidth;} Super.onmeasure (Widthmeasurespec, heightmeasurespec);} @Overrideprotected void OnLayout (Boolean changed, int l, int t, int r, int b) {super.onlayout (changed, L, T, R, b); if (chan GED) {//The menu is hidden This.scrollto (mmenuwidth, 0); once = True;}} @Overridepublic Boolean OntoucHevent (motionevent ev) {int action = ev.getaction (); switch (action) {//up) is judged if the display area is greater than half the width of the menu and is completely displayed, otherwise hidden case MotionEvent.ACTION_UP:int scrollx = Getscrollx (), if (Scrollx > Mhalfmenuwidth) {this.smoothscrollto (mmenuwidth, 0); IsOpen = false;} Else{this.smoothscrollto (0, 0); IsOpen = true;} return true;} return super.ontouchevent (EV);} /** * Open menu */public void Openmenu () {if (isOpen) return;this.smoothscrollto (0, 0); IsOpen = true;} /** * Close menu */public void Closemenu () {if (IsOpen) {this.smoothscrollto (mmenuwidth, 0); isOpen = false;}} /** * Toggle Menu State */public void Toggle () {if (IsOpen) {Closemenu ();} else{openmenu ();}}

Using Horizontalscrollview, listen to the Action_up event, when the user lifts his finger, according to the current menu display width value, determine whether to retract or fully expand; gives the user a Rightpadding property for setting the distance from the right screen of the menu ; and provide a few methods of opening, closing and switching; the specific explanation looks at the next blog;

2, the realization of the idea

Now that we've settled on those 3 differences, we've chosen to animate with attributes and now decide where to add the animation effect.

Needless to say, I think about it in the Action_move, yes, action_move can really, constantly get the current getscrollx/mmenuwidth, changing the menu transparency, scaling, X-direction offset Changing the width and height of the content area;

To speak, I did this in my move at first, but there are two questions:

1, the animation effect is not very smooth, especially the menu, has the effect of jitter;

2, the user lifted up, also need in the up inside, continue unfinished animation, that is, your transparency, scaling God horse, when the user will need to automatically change after lifting;

So, I began to change a direction, since it is srollview, there must be a scrollchanged method, Kung Fu is a conscientious, really this method:

@Overrideprotected void onscrollchanged (int l, int t, int oldl, int Oldt) {}
This method as long as the scrollchanged will trigger, L is the scrollx we need, too great ~ ~ ~

3, the animation proportion of the calculation

We in the onscrollchanged inside, get L is a getscrollx, that is, the menu has shown the width value;

FLOAT scale = l * 1.0f/mmenuwidth;

With the width of the menu to do division, in the menu hidden to show the whole process, you will get 1.0~0.0 such a change in the interval;

With this interval, the animation can be set according to the interval;

1, the first is the content area of the scaling scale calculation:

We're going to let the content area change from 1.0~0.8 in the course of menu appearance ~ ~

So how to convert 1.0~0.0 into 1.0~0.8, actually very simple:

Float RightScale = 0.8f + scale * 0.2F; (scale from 1 to 0), is not oh ~

Then there are 3 more animations:

2. Scale calculation of menu

Carefully observe the next QQ, the menu probably zoom change is 0.7~1.0

float Leftscale = 1-0.3f * scale;

3, the transparency ratio of the menu:

We set it to 0.6~1.0:0.6f + 0.4f * (1-scale)

4. The X-Direction offset of the menu:

Look at QQ, not completely from being covered by the content area, or there is a little drag out of the feeling, so our offset so set:

Tranlatex = mmenuwidth * scale * 0.6f; just start or let it hide a little bit ~ ~

4, The complete realization

Said so much, in fact, in the history of the simplest slide, to the QQ5.0 effect of the change, only a few lines of code ~ ~

@Overrideprotected void onscrollchanged (int l, int t, int oldl, int Oldt) {super.onscrollchanged (L, T, Oldl, Oldt), float SC Ale = L * 1.0f/mmenuwidth;float leftscale = 1-0.3f * Scale;float RightScale = 0.8f + scale * 0.2F; Viewhelper.setscalex (Mmenu, Leftscale); Viewhelper.setscaley (Mmenu, Leftscale); Viewhelper.setalpha (Mmenu, 0.6f + 0.4f * (1-scale)); Viewhelper.settranslationx (Mmenu, mmenuwidth * scale * 0.6f); Viewhelper.setpivotx (mcontent, 0); Viewhelper.setpivoty (Mcontent, Mcontent.getheight ()/2); Viewhelper.setscalex (Mcontent, RightScale); Viewhelper.setscaley (Mcontent, RightScale);}

Just a few lines. Here the property animation with the Nineoldandroids in order to maintain a downward compatibility, the main is set up a variety of animation, the above are detailed said ~ ~ ~
Then, remember to put our menu and content layout, separate declaration for our mmenu, Mcontent ~ No, changed so many lines ~

3.

is a mule is a horse that pulls out to sneak


The menu bar needs to drag the ListView will not conflict, the previous article has been tested;

About the range of animation properties: The above is particularly clear, such as the content we are the smallest display 0.8, if you like 0.6, you go to modify, including offset, transparency and other areas;

Because the previous article has written about how to extract attributes into custom properties, so there is no extraction, or it always feels like repeating ~


Well, recently there is a slide to write apps, that is, the menu bar is completely hidden under the content area, if needed:


In fact, I like this effect.

Implementation, note a few lines of code is implemented:

@Overrideprotected void onscrollchanged (int l, int t, int oldl, int Oldt) {super.onscrollchanged (L, T, Oldl, Oldt), float SC Ale = L * 1.0f/mmenuwidth;//float leftscale = 1-0.3f * Scale;//float RightScale = 0.8f + scale * 0.2F;////VIEWHELPER.S Etscalex (Mmenu, Leftscale);//viewhelper.setscaley (Mmenu, Leftscale);//viewhelper.setalpha (MMenu, 0.6f + 0.4f * (1- scale)); Viewhelper.settranslationx (Mmenu, mmenuwidth * scale);//viewhelper.setpivotx (mcontent, 0);//viewhelper.setpivoty ( Mcontent, Mcontent.getheight ()/2);//viewhelper.setscalex (Mcontent, RightScale);//viewhelper.setscaley (MContent, RightScale);}


Well, although the final implementation seems to be very simple, it seems, ah ~ ~ But the process is not easy from scratch ~ ~ All kinds of attempts, I can say I even squat in the sliding QQ menu to observe it ~ ha, laughed at, the blog also wrote the process of the failure of the attempt, Hopefully it will be better for everyone to learn something useful in there ~~yeah!! Here, it's OK to leave a message ~ ~ ~ No messages, I will come to a source please leave the mailbox, Hey, open a joke ~




SOURCE Click to download


Finally, I built a QQ group, convenient for everyone to communicate. Group number:55032675




Android High Imitation QQ5.0 slide menu effect custom controls to attack

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.