Learn to customize toast (change toast default animation, pop-up position)

Source: Internet
Author: User

The project used a drawertoast, checked the brother's things: http://blog.csdn.net/pmavio/article/details/38817885

The main implementation of the queue without waiting for toast directly up, the animation of toast, such as custom effects.

There are several main changes to be made according to the requirements:

1.toast position, just below the titlebar.

2. When the second toast appears, do not stack up, stop the animation of the previous toast, and display the second toast directly in the same location.

3. Each system has a default toast animation effect, which is superimposed on a custom animation and does not achieve the desired effect.

Because I am lazy · So directly in the source code above the change.

The location of the toast

1.DrawerToast principle of thinking is in the Toast.setview when the introduction of a number of layouts, and finally the need to display the view is placed in a linearlayout to do vertical linear display, read the source, the initviews in the

Mview.setgravity (gravity.center_horizontal| Gravity.bottom); Change to mview.setgravity (Gravity.center_horizontal | Gravity.top); To meet the features I want to top up.

A running program found that the toast was also indented a little bit above the screen, checking the code for discovery

New layoutparams (ScreenWidth, screenheight); Mtopview2.setlayoutparams (params);
View Code

This is the height of the screen at the time of initialization, which also includes the status bar of the Android system, so the height is more than the application itself. Then do not know because of what causes the view to come out of the part is not extended to the bottom, is indented to the top of the screen. So at the time of initialization, the height of the status bar off the height, and then set the margin of titlebar height, solve the first problem

//Status Bar Height Public Static intGetstatusbarheight (Context context) {Class<?> C =NULL; Object obj=NULL; Field field=NULL; intx = 0, statusbarheight = 0; Try{C= Class.forName ("Com.android.internal.r$dimen"); Obj=c.newinstance (); Field= C.getfield ("Status_bar_height"); X=integer.parseint (Field.get (obj). toString ()); Statusbarheight=context.getresources (). getdimensionpixelsize (x); } Catch(Exception E1) {e1.printstacktrace (); }        returnStatusbarheight; }intStatusbarheight =getstatusbarheight (Mcontext); Layoutparams params=Newlayoutparams (screenwidth, ScreenHeight-statusbarheight); Mtopview2.setlayoutparams (params);
View Code

2. The second problem is good to do, as long as each toast to show the time before the Viewremove has been added to drop it.

Mview.addview (V, 0) in the Show method, preceded by Mview.removeallviews ();

3. This problem is very annoying, originally thought set the animation itself the default animation should be gone, looked up only to find that each system's default toast animation effect with the back set is not conflicting, can exist at the same time, so also have to remove the system default animation.

Since drawertoast itself has been useful for reflection to get the show and hide methods under MTN, just make a little change in the original code.

    /*** Show and Hide methods under MTN are obtained by reflection*/    Private voidInittn () {mtoast=NewToast (Mcontext);        Mtoast.setview (Mtopview); Class<Toast> clazz = Toast.class; Try{MTN= Clazz.getdeclaredfield ("MTN"); Mtn.setaccessible (true); Mobj=Mtn.get (mtoast); //Cancel default toast popup animations for each system modify by YANGSQ 2014-01-04Field field = Mobj.getclass (). Getdeclaredfield ("Mparams"); if(Field! =NULL) {field.setaccessible (true); Object Mparams=Field.get (mobj); if(Mparams! =NULL&& Mparamsinstanceofwindowmanager.layoutparams) {windowmanager.layoutparams params=(windowmanager.layoutparams) mparams; Params.windowanimations=-1; }} Showmethod= Mobj.getclass (). Getdeclaredmethod ("Show",NULL); Hidemethod= Mobj.getclass (). Getdeclaredmethod ("Hide",NULL); Hasreflectexception=false; } Catch(nosuchfieldexception e) {hasreflectexception=true;        System.out.println (E.getmessage ()); } Catch(illegalaccessexception e) {hasreflectexception=true;        System.out.println (E.getmessage ()); } Catch(IllegalArgumentException e) {hasreflectexception=true;        System.out.println (E.getmessage ()); } Catch(nosuchmethodexception e) {hasreflectexception=true;        System.out.println (E.getmessage ()); }    }
View Code

The last is to call show at the time of the beginning and end of the animation, according to their own needs to preach, not much to say.

Learn to customize toast (change toast default animation, pop-up position)

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.