
public class ToastUtil {
private static WindowManager windowManager;
private static WindowManager.LayoutParams layoutParams;
private static volatile ToastUtil instance;
private static Handler handler=new Handler();
//单例
public static synchronized ToastUtil getInstance(){
if(instance==null){
synchronized (ToastUtil.class){
if(instance==null){
instance=new ToastUtil();
}
}
}
return instance;
}
private ToastUtil() {
windowManager= (WindowManager) LauncherApplication.CONTEXT.getSystemService(Context.WINDOW_SERVICE);
layoutParams=new WindowManager.LayoutParams();
if (Build.VERSION.SDK_INT >= 26) {
layoutParams.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
} else {
layoutParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
}
//相对位置在中间,所以后面的X、Y轴位置根据Gravity.CENTER 来定义
layoutParams.gravity=Gravity.CENTER;
layoutParams.format = PixelFormat.TRANSLUCENT;
layoutParams.windowAnimations = R.style.Animation_Design_BottomSheetDialog;
layoutParams.flags = WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH
| WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
| WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
| WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED;
}
/**
* 弹出TextView
* @param context 上下文
* @param content 文本
* @param y TextView在屏幕居中 ,y轴的位置
* @param width TextView的宽
* @param height TextView的高
*/
public void createTextView(Context context,String content,int y,int width,int height){
TextView textView =new TextView(context);
textView.setText(content);
textView.setTextColor(Color.WHITE);
textView.setTextSize(TypedValue.COMPLEX_UNIT_PX,48);
textView.setGravity(Gravity.CENTER);
textView.setBackground(context.getResources().getDrawable(R.drawable.shape_button_ok));
layoutParams.width = width;
layoutParams.height =height;
layoutParams.y=y;
windowManager.addView(textView, layoutParams);
handler.postDelayed(new Runnable() {
@Override
public void run() {
windowManager.removeView(textView);
}
},2000);
}
/**
* 适配多国语言翻译所以需要资源ID
* @param context 上下文
* @param contentId 字符串ID
* @param backId 背景ID
* @param y y轴的位置
* @param width TextView的宽
* @param height TextView的高
*/
public void createTextView(Context context,int contentId,int backId,int y,int width,int height){
TextView textView =new TextView(context);
textView.setText(context.getResources().getText(contentId));
textView.setTextColor(Color.WHITE);
textView.setTextSize(TypedValue.COMPLEX_UNIT_PX,48);
textView.setGravity(Gravity.CENTER);
textView.setBackground(context.getResources().getDrawable(backId));
layoutParams.width = width;
layoutParams.height =height;
layoutParams.y=y;
windowManager.addView(textView, layoutParams);
handler.postDelayed(new Runnable() {
@Override
public void run() {
windowManager.removeView(textView);
}
},1000);
}
/**
* 也可以弹出自定义的View ,2秒之后消失
* @param view
* @param x Gravity.CENTER
* @param y Gravity.CENTER
*/
public void showView(View view,int x,int y){
layoutParams.x=x;
layoutParams.y=y;
layoutParams.height=view.getHeight();
layoutParams.width=view.getWidth();
windowManager.addView(view, layoutParams);
handler.postDelayed(new Runnable() {
@Override
public void run() {
windowManager.removeView(view);
}
},2000);
}
}

【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 没有源码,如何修改代码逻辑?
· NetPad:一个.NET开源、跨平台的C#编辑器
· PowerShell开发游戏 · 打蜜蜂
· 凌晨三点救火实录:Java内存泄漏的七个神坑,你至少踩过三个!