Advanced Target on a blog we learned the toast of the source, understand the toast from the display to disappear the whole process, learning link: Android toast source analysis. As the saying goes, it's good. We learn toast source code is not used to dazzle, but to understand the toast principle, so as to really solve our problems. I'll mention some of the two real-world toast-related issues that you might encounter in your business, and see how you can solve these problems after learning the toast source. Two questions are:
- How to customize the display time of a toast.
- How to modify the appearance of a toast animation.
Next, we explain how to solve the real problems in the two businesses after reading the toast source.
Control toast display time by learning from the toast source, we know that the display and disappearance of toast is implemented by Notificationmanagerservice calling the show and hide methods of the TN class, The length of time the toast is displayed is related to the delay time that handler sends the message. The specific source code is as follows:
private void scheduletimeoutlocked (Toastrecord r) { mhandler.removecallbacksandmessages (r); Message m = Message.obtain (Mhandler, Message_timeout, R); Long delay = R.duration = = Toast.length_long? Long_delay:short_delay; Mhandler.sendmessagedelayed (m, delay); }
Before the toast source also said, why Toast desktop display time can only be 2s and 3.5s, the key is that long delay variable delay time can only be 2s and 3.5s. So if you are a developer of the Android operating system, you can directly modify the Android framework layer's Notificationmanagerservice class code, which will long_delay and Short_ Delay changes to the time interval you want. However, the drawbacks of this approach are obvious. First of all, you may only be a small application layer development engineer, can only change the application layer code. Second, even if modified notificationmanagerservice, can only change the Long_delay and Short_delay two variables, can not be arbitrarily modified display time. There are so many drawbacks, what should we do with the application layer development engineer? The answer is also very simple, modeled after the toast source, we build a wheel, customize a toast, so that we can certainly control the toast display time. Through the source we know that toast is based on WindowManager to display, then we can completely masturbate a custom toast out, the source code is as follows:
Import Android.content.context;import Android.graphics.pixelformat;import Android.os.handler;import Android.os.message;import Android.view.gravity;import Android.view.view;import Android.view.WindowManager;import Android.widget.toast;public class Toastcustom {private static final int message_timeout = 2;private WindowManager wdm;pri Vate double time;private View mview;private windowmanager.layoutparams params;private workerhandler mHandler;private Toastcustom (Context context, String text, double time) {WDM = (WindowManager) context.getsystemservice (context.window_ SERVICE); Mhandler = new Workerhandler (); Toast Toast = Toast.maketext (context, text, toast.length_long); MView = Toast.getview ();p arams = new Windowmanager.layoutparams ();p arams.height = Windowmanager.layoutparams.wrap_content;params.width = Windowmanager.layoutparams.wrap_content;params.format = Pixelformat.translucent;params.windowanimations = Toast.getview (). Getanimation (). Infinite;params.type = WindowManager.LayoutParams.TYPE_toast;params.settitle ("TOAST");p arams.gravity = Gravity.center_horizontal | Gravity.center_vertical;params.flags = windowmanager.layoutparams.flag_keep_screen_on| windowmanager.layoutparams.flag_not_focusable| Windowmanager.layoutparams.flag_not_touchable;this.time = time;} public static Toastcustom Maketext (context context, String text, double time) {Toastcustom toastcustom = new Toastcustom (c Ontext, text, time); return toastcustom;} public void Show () {Wdm.addview (MView, params); Mhandler.sendemptymessagedelayed (Message_timeout, (long) (TIME * 1000)) ;} public void Cancel () {Wdm.removeview (MView);} Private class Workerhandler extends Handler {@Override public void Handlemessage (Message msg) { Switch (msg.what) {case message_timeout:cancel (); Break } }}}
The principle is simple, use WindowManager to display toasts, and then use the handler mechanism to send deferred message control WindowManager and then delete the toast. One thing to note: The handler of the custom toast here is also the looper of the main thread, and the child threads calling the custom toast need to add looper.prepare () and Looper.loop () code.
Modify Toast rendering animation Android native Toast class does not provide an interface for animating our effects, Each Android native Toast animation effect is defined in the TN class com.android.internal.r.style.animation_toast, so if you want to modify the animation effect of an Android toast, Or you need to make a toast, modify the contents of the params.windowanimations variable. Next, let's customize an animation effect first. Define a new style,xml content in the Style.xml file as follows:
<style name= "Custom_toast_anim_view" > <item name= "@android: Windowenteranimation" > @anim/enter_ anim</item> <item name= "@android: Windowexitanimation" > @anim/exit_anim</item> </ Style>
Then add two animation effects files under the Anim folder, Enter_anim.xml and Exit_anim.xml, respectively. Enter_anim.xml:
<?xml version= "1.0" encoding= "Utf-8"? ><set xmlns:android= "Http://schemas.android.com/apk/res/android" > <translate android:duration= "1" android:fromxdelta= "0" android:fromydelta= "0" Andro Id:toxdelta= "0" android:toydelta= "/>" <translate android:fromxdelta= "0" android:fromy Delta= "0" android:toxdelta= "0" android:toydelta= " -100" android:duration= "Android:fillafte" R= "true" android:interpolator= "@android: Anim/decelerate_interpolator"/> <alpha android:duration= "10 0 "android:fromalpha=" 0 "android:toalpha=" 1 "/> <translate android:duration=" Android " Oid:fillafter= "true" android:fromxdelta= "0" android:fromydelta= "0" android:startoffset= "a" Ndroid:toxdelta= "0" android:toydelta= "/></set>"
Exit_anim.xml:
<?xml version= "1.0" encoding= "Utf-8"? ><set xmlns:android= "Http://schemas.android.com/apk/res/android" > <alpha android:duration= "android:fromalpha=" 1 " android:toalpha=" 0 "/></set >
Then, modify the params.windowanimations variable in your custom toast code:
Params.windowanimations = Com.example.photocrop.r.style.custom_toast_anim_view;
Android Toast Advanced--Custom toast