Android Toast进阶——自定义Toast
进阶目标
- 如何自定义Toast的显示时间。
- 如何修改Toast的出现动画。
控制Toast显示时间
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); }之前Toast源码里也讲过,为什么Toast的桌面显示时间只能是2s和3.5s,关键就是在于long delay变量的延迟时间只能是2s和3.5s。因此,如果你是Android操作系统的开发人员,你可以直接修改Android Framework层的NotificationManagerService类代码,将LONG_DELAY和SHORT_DELAY改成你想要的时间间隔。但是,这种做法的弊端很明显。首先,你可能只是一个小小的应用层开发工程师,只能改动应用层代码。其次,就算修改NotificationManagerService,也只能改动LONG_DELAY和SHORT_DELAY两个变量,无法做到随意修改显示时间。
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; private 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(); params = new WindowManager.LayoutParams(); params.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"); params.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(context, 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; } } } }
修改Toast呈现动画
<style name="custom_toast_anim_view"> <item name="@android:windowEnterAnimation">@anim/enter_anim</item> <item name="@android:windowExitAnimation">@anim/exit_anim</item> </style>然后在anim文件夹下面增加两个动画效果文件,分别为enter_anim.xml和exit_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" android:toXDelta="0" android:toYDelta="80" /> <translate android:fromXDelta="0" android:fromYDelta="0" android:toXDelta="0" android:toYDelta="-100" android:duration="300" android:fillAfter="true" android:interpolator="@android:anim/decelerate_interpolator"/> <alpha android:duration="100" android:fromAlpha="0" android:toAlpha="1" /> <translate android:duration="80" android:fillAfter="true" android:fromXDelta="0" android:fromYDelta="0" android:startOffset="300" android:toXDelta="0" android:toYDelta="20" /> </set>exit_anim.xml:
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" > <alpha android:duration="800" android:fromAlpha="1" android:toAlpha="0" /> </set>然后,修改一下自定义Toast代码中的params.windowAnimations变量即可:
params.windowAnimations = com.example.photocrop.R.style.custom_toast_anim_view;
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。