Click "+" for Renren in Android to bring up the pop-up effect.

Source: Internet
Author: User

Recently, I worked overtime day and night, and even forgot my last name. What's more terrible is that I had to pay for a bug! Alas, let's not talk about work first. Back to the technology, we can satisfy ourselves with such spiritual food every day. Recently, we have forgotten to share many things with you. I am back to continue to share with you another new cainiao discovery, we hope to help more people implement some of their project needs. No matter whether you have such requirements, I only hope to help you, and share your own things to help more people and let cainiao grow together!

Continue with the previous blog style, first go to the Code, there is a picture of the truth!

Effect:


Implementation ideas:

Are you familiar with this effect? haha, it is an effect in Renren's network. I also found that many applications use this effect now, like the recent upto app for calendar sharing, you can check it out if you have time. There is still a cool effect that hasn't been well studied yet.

Some people may have seen this effect, such as the quickbar in the address book, but it is not flexible. To achieve this effect, we actually use the popupwindow. I have a blog about this. I just want to add a function on the basis of this blog. Suddenly found this thing is still very gray and easy to use.

We need to rewrite the popupwindow. Then, use setcontentview () to load our layout file, and then add an animation to achieve the same effect of Renren.

The code for rewriting popupwindow is provided. If you do not know anything, read the code or add QQ.

Package COM. jiahui. view; import Java. util. arraylist; import android. content. context; import android. graphics. rect; import android. graphics. drawable. bitmapdrawable; import android. graphics. drawable. drawable; import android. view. gravity; import android. view. layoutinflater; import android. view. motionevent; import android. view. view; import android. view. view. onclicklistener; import android. view. view. ontouchlistene R; import android. view. viewgroup; import android. view. viewgroup. layoutparams; import android. view. windowmanager; import android. view. animation. animation; import android. view. animation. animationutils; import android. view. animation. interpolator; import android. widget. imageview; import android. widget. linearlayout; import android. widget. popupwindow; import android. widget. textview; import COM. jiahui. quickbar. acti Onitem; import COM. jiahui. quickbar. r;/*** rewrite popupwindow * @ author administrator **/public class quickactionbar extends popupwindow {private view root; private imageview marrowup; private imageview marrowdown; private animation mtrackanim; private layoutinflater Inflater; private context; private view anchor; private popupwindow window; private drawable background = NULL; private windowmanager wind Owmanager; public static final int placement = 1; public static final int anim_grow_from_right = 2; public static final int anim_grow_from_center = 3; public static final int anim_auto = 4; private int animstyle; private Boolean animatetrack; private viewgroup mtrack; private arraylist <actionitem> actionitems; Public quickactionbar (view anchor) {super (Anchor); this. anchor = anchor; this. window = ne W popupwindow (anchor. getcontext ();/*** close the window when you click it outside the popwindow */window. settouchinterceptor (New ontouchlistener () {@ overridepublic Boolean ontouch (view, motionevent event) {If (event. getaction () = motionevent. action_outside) {// make it disappear quickactionbar. this. window. dismiss (); Return true;} return false ;}); Context = anchor. getcontext (); windowmanager = (windowmanager) context. getsystemservice (Conte XT. window_service); actionitems = new arraylist <actionitem> (); Inflater = (layoutinflater) context. getsystemservice (context. layout_inflater_service); root = (viewgroup) Inflater. inflate (R. layout. quickbar, null); // the upper and lower arrows marrowdown = (imageview) root. findviewbyid (R. id. arrow_down); marrowup = (imageview) root. findviewbyid (R. id. arrow_up); setcontentview (Root); mtrackanim = animationutils. loadanimation (cont EXT, R. anim. rail);/*** set the acceleration effect */mtrackanim. setinterpolator (New interpolator () {@ overridepublic float getinterpolation (float t) {final float inner = (T * 1.55f)-1.1f; return 1.2f-inner * inner ;}}); // This is the horizontal layout of mtrack = (viewgroup) root in the pop-up window. findviewbyid (R. id. tracks); animstyle = anim_auto; // set the animation style animatetrack = true;}/*** sets a flag to mark the animation display ** @ Param animatetrack */Public void animatetrack (boolea N animatetrack) {This. animatetrack = animatetrack;}/*** set the animation style ** @ Param animstyle */Public void setanimstyle (INT animstyle) {This. animstyle = animstyle;}/*** add an action ** @ Param actionitem */Public void addactionitem (actionitem) {actionitems. add (actionitem) ;}/ *** pop-up form */Public void show () {preshow (); int [] Location = new int [2]; // obtain the anchor location. getlocationonscreen (location );// Construct a rectangular rect anchorrect = new rect (location [0], location [1], location [0] + anchor at the position of anchor. getwidth (), location [1] + anchor. getheight (); root. setlayoutparams (New layoutparams (layoutparams. wrap_content, layoutparams. wrap_content); root. measure (layoutparams. wrap_content, layoutparams. wrap_content); int rootwidth = root. getmeasuredwidth (); int rootheight = root. getmeasuredheight (); // get the screen width int screenwidt H = windowmanager. getdefadisplay display (). getwidth (); // set X yint xpos = (screenwidth-rootwidth)/2 in the pop-up position; int ypos = anchorrect. top-rootheight; Boolean ontop = true; // If (rootheight> anchorrect is displayed at the bottom. top) {ypos = anchorrect. bottom; ontop = false;} // set different direction arrow Pictures Based on the pop-up position. // showarrow (ontop )? R. id. arrow_down: R. id. arrow_up), // anchorrect. centerx (); // set the animation style setanimationstyle (screenwidth, anchorrect. centerx (), ontop); // create action listcreateactionlist (); // the pop-up window appears at the specified position. showatlocation (this. anchor, gravity. no_gravity, xpos, ypos); // sets the horizontal layout of the animation if (animatetrack) {mtrack. startanimation (mtrackanim);}/*** pre-processing window */protected void preshow () {If (root = NULL) {Throw new illegalstateexce Ption ("layout needs to be set for the pop-up window");} If (background = NULL) {window. setbackgrounddrawable (New bitmapdrawable ();} else {window. setbackgrounddrawable (background);} // set the window width. setwidth (windowmanager. layoutparams. wrap_content); // set the height of the window. setheight (windowmanager. layoutparams. wrap_content); window. settouchable (true); window. setfocusable (true); window. setoutsidetouchable (true); // specify the layout window. setcontentview (Root );}/** * Set the animation style ** @ Param screenwidth * @ Param requestedx * @ Param ontop */private void setanimationstyle (INT screenwidth, int requestedx, Boolean ontop) {int arrowpos = requestedx-marrowup. getmeasuredwidth ()/2; Switch (animstyle) {Case anim_grow_from_left: window. setanimationstyle (ontop )? R. style. animations_popdownmenu_left: R. style. animations_popdownmenu_left); break; Case anim_grow_from_right: window. setanimationstyle (ontop )? R. style. animations_popdownmenu_right: R. style. animations_popdownmenu_right); break; Case anim_grow_from_center: window. setanimationstyle (ontop )? R. style. animations_popdownmenu_center: R. style. animations_popdownmenu_center); break; Case anim_auto: If (arrowpos <screenwidth/4) {window. setanimationstyle (ontop )? R. style. animations_popdownmenu_left: R. style. animations_popdownmenu_left);} else if (arrowpos> screenwidth/4 & arrowpos <3 * (screenwidth/4) {window. setanimationstyle (ontop )? R. style. animations_popdownmenu_center: R. style. animations_popdownmenu_center);} else {window. setanimationstyle (ontop )? R. style. animations_popdownmenu_right: R. style. animations_popdownmenu_right);} break;}/*** create action list */private void createactionlist () {view; String title; drawable icon; onclicklistener clicklistener; int Index = 1; for (INT I = 0; I <actionitems. size (); I ++) {Title = actionitems. get (I ). gettitle (); icon = actionitems. get (I ). getIcon (); clicklistener = actionitems. get (I ). getclicklistener (); // get Action itemview = getactionitem (title, icon, clicklistener); view. setfocusable (true); view. setclickable (true); mtrack. addview (view, index); index ++ ;}} /*** get action item ** @ Param title * @ Param icon * @ Param listener * @ return */private view getactionitem (String title, drawable icon, onclicklistener listener) {// load the action layout linearlayout = (linearlayout) Inflater. inflate (R. layout. action_it Em, null); imageview img_icon = (imageview) linearlayout. findviewbyid (R. id. icon); textview TV _title = (textview) linearlayout. findviewbyid (R. id. title); If (img_icon! = NULL) {img_icon.setimagedrawable (icon);} else {img_icon.setvisibility (view. Gone);} If (TV _title! = NULL) {TV _title.settext (title);} else {TV _title.setonclicklistener (listener);} return linearlayout ;} /// ** // * display the arrow // * @ Param whicharrow resource ID // * @ Param requestedx // * distance from the left of the screen //*/ // Private void showarrow (INT whicharrow, int requestedx) {// final view showarrow = (whicharrow = R. id. arrow_up )? Marrowup //: marrowdown; // final view hidearrow = (whicharrow = R. Id. arrow_up )? Marrowdown //: marrowup; // final int arrowwidth = marrowup. getmeasuredwidth (); // showarrow. setvisibility (view. visible); // viewgroup. marginlayoutparams Param = (viewgroup. marginlayoutparams) // showarrow //. getlayoutparams (); // you can specify the distance to the left. // Param. leftmargin = requestedx-arrowwidth/2; // hidearrow. setvisibility (view. invisible );////}}

Here, we only paste the core code. If you use other code, you can download the source code and study it. Continue with my style and release your source code to share with you. I hope this will help you a little bit.

If you need to reprint reference please indicate the source: http://blog.csdn.net/jiahui524

You are welcome to have more exchanges. Sharing is the foundation of happiness! Let's grow together with cainiao!

Source code download: http://download.csdn.net/detail/jiahui524/4158447

 

PS:Some netizens recently asked questions, saying that the actionitem click event could not be completed. This is also a mistake I made. I am sorry for it and I would like to thank the netizen for his question. If you want the actionitem event to be valid, find the getactionitem method in the quickactionbar in the original source code and modify the code as follows:

/*** Get action item ** @ Param title * @ Param icon * @ Param listener * @ return */private view getactionitem (String title, drawable icon, onclicklistener listener) {// load the action layout linearlayout = (linearlayout) Inflater. inflate (R. layout. action_item, null); imageview img_icon = (imageview) linearlayout. findviewbyid (R. id. icon); textview TV _title = (textview) linearlayout. findviewbyid (R. id. title ); If (img_icon! = NULL) {img_icon.setimagedrawable (icon);} else {img_icon.setvisibility (view. Gone);} If (TV _title! = NULL) {TV _title.settext (title);} else {TV _title.setonclicklistener (listener);} If (listener! = NULL) {linearlayout. setonclicklistener (listener);} return linearlayout ;}

 

 

Related 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.