Android 11 WindowManager实现自定义Toast功能

复制代码
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);
}

}
复制代码
posted @   炸憨啪  阅读(639)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 没有源码,如何修改代码逻辑?
· NetPad:一个.NET开源、跨平台的C#编辑器
· PowerShell开发游戏 · 打蜜蜂
· 凌晨三点救火实录:Java内存泄漏的七个神坑,你至少踩过三个!
点击右上角即可分享
微信分享提示