XYLibrary笔记三:ToastUtils

2023-03-24 13:21:40 浏览数 (2)

用于替换原生Toast,防止高版本出现的显示问题,高版本加显示权限

代码语言:javascript复制
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>

示例:

代码语言:javascript复制
ToastUtils.make()
        .Text("AAAAAA")
        .TextColor(R.color.colorPrimary) //字体颜色
        .TextSize(50) //字体大小
        .BackGroundColor(R.color.colorAccent) //背景颜色
        .LayoutParamsX(0) //离X左边距离
        .LayoutParamsY(150) //离Y坐标距离
        .Gravity(Gravity.BOTTOM) //Toast显示位置的重心设置
        .RoundedCorners(3) //背景圆角大小
        .ShowTime(ToastUtils.LENGTH_SHORTSHORT) //显示时长
        .show();

代码

代码语言:javascript复制
package cn.xy.library.util.toast;

import android.content.Context;
import android.graphics.PixelFormat;
import android.os.Build;
import android.os.Handler;
import android.view.Gravity;
import android.view.WindowManager;
import android.view.WindowManager.LayoutParams;
import android.widget.TextView;
import cn.xy.library.App;
import cn.xy.library.util.convert.ConvertUtils;
import cn.xy.library.util.image.ImageUtils;

/**
 * xiangy add by 12:48 2021/1/5
 */
public class ToastUtils {
  private WindowManager mWindowManager;
  private LayoutParams mLayoutParams;
  private static TextView mView;
  private static final int HIDE = 1;
  private static ToastUtils mToastView;
  private static String Text = "";
  private static int TextColor = 0xFF000000;
  private static int BackGroundColor = 0xDCDCDCDC;
  private static float TextSize = 20.0F;
  private static int Radius = 15;
  private static int LayoutParamsX = 0;
  private static int LayoutParamsY = 35;
  private static int LayoutParamsGravity = Gravity.BOTTOM;
  int DelayMillis_sh = 2000;
  public static final int LENGTH_SHORTSHORT = 0;
  public static final int LENGTH_SHORT = 1;
  public static final int LENGTH_LONG = 2;
  public static final int LENGTH_LONGLONG = 3;

  private ToastUtils() {
    throw new UnsupportedOperationException("u can't instantiate me...");
  }

  public static ToastUtils make() {
    if (mToastView == null) {
      mToastView = new ToastUtils(App.getApp());
      return mToastView;
    } else {
      return mToastView;
    }
  }

  public ToastUtils Text(String toastString) {
    Text = toastString;
    return mToastView;
  }

  public ToastUtils TextSize(int size){
    TextSize = size;
    return mToastView;
  }
  public ToastUtils TextColor(int color){
    TextColor = color;
    return mToastView;
  }
  public ToastUtils TextColor(String color){
    TextColor = ConvertUtils.string2color(color);
    return mToastView;
  }

  public ToastUtils RoundedCorners(int radius){
    Radius = radius;
    return mToastView;
  }
  public ToastUtils BackGroundColor(int backgroundcolor){
    BackGroundColor = backgroundcolor;
    return mToastView;
  }
  public ToastUtils BackGroundColor(String backgroundcolor){
    BackGroundColor = ConvertUtils.string2color(backgroundcolor);
    return mToastView;
  }

  public ToastUtils LayoutParamsX(int x){
    LayoutParamsX = x;
    return mToastView;
  }

  public ToastUtils LayoutParamsY(int y){
    LayoutParamsY = y;
    return mToastView;
  }

  public ToastUtils Gravity(int gravity){
    LayoutParamsGravity = gravity;
    return mToastView;
  }

  public ToastUtils ShowTime(int i){
    switch (i){
      case LENGTH_SHORTSHORT:
        DelayMillis_sh = 1500;
        break;
      case LENGTH_SHORT:
        DelayMillis_sh = 2500;
        break;
      case LENGTH_LONG:
        DelayMillis_sh = 3500;
        break;
      case LENGTH_LONGLONG:
        DelayMillis_sh = 4500;
        break;
    }
    return mToastView;
  }

  private Handler mHandler = new Handler() {
    public void handleMessage(android.os.Message msg) {
      if (msg.what == HIDE) {
        hideView();
      }
    }
  };

  private ToastUtils(Context context) {
    mView = new TextView(context);
    // 设置窗体显示类型
    mWindowManager = (WindowManager) context.getSystemService(context.WINDOW_SERVICE);
    mLayoutParams = new LayoutParams(
        LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 0, 0,
        LayoutParams.TYPE_PHONE,
        LayoutParams.FLAG_NOT_TOUCH_MODAL | LayoutParams.FLAG_NOT_FOCUSABLE | LayoutParams.FLAG_NOT_TOUCHABLE,
        PixelFormat.RGBA_8888);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
      mLayoutParams.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
    } else {
      mLayoutParams.type = WindowManager.LayoutParams.TYPE_PHONE;
    }
  }

  public void show(){
    if (mView != null) {
      mView.setText(Text);
      mView.setTextColor(TextColor);
      mView.setPadding(10, 10, 10, 10);
      mView.setBackgroundColor(BackGroundColor);
      ImageUtils.setRoundedCorners(mView,Radius);
      mView.setTextSize(TextSize);
    }
    mLayoutParams.gravity = LayoutParamsGravity;
    mLayoutParams.x = LayoutParamsX;
    mLayoutParams.y = LayoutParamsY;
    showView();
  }
  private void showView() {
    if (mView.getParent() == null) {
      mWindowManager.addView(mView, mLayoutParams);
    }
    mHandler.removeMessages(HIDE);
    mHandler.sendEmptyMessageDelayed(HIDE, DelayMillis_sh);
  }

  private void hideView() {
    if (mView.getParent() != null) {
      mWindowManager.removeView(mView);
    }
  }
}

0 人点赞