当前位置:主页 > 移动开发 > Android代码 >

Android自定义Toast之WindowManager

时间:2021-08-31 10:00:46 | 栏目:Android代码 | 点击:

本文为大家分享了Android自定义Toast之WindowManager,供大家参考,具体内容如下

Toast:WindowManager

三个重要的API:

相当于布局文件中的属性

一定要销毁,否则无法退出程序。一定要移除,否则报异常

Toast:WindowManager

1) 获取WindowManager对象
2)产生WindowManager.LayoutParams(并且设置参数)
3)产生一个View
4)显示:addView(view,mParams);
5) 隐藏:removeView(view);
6) 更新位置一:view:设置一个OnTouchListener:ACTION_DONW,ACTION_MOVE,ACTION_UP
7) 更新位置二:updateViewLayout(view,mParams)
8)写成AddressToast


布局文件

 <TextView 
    android:id="@+id/tv_custom_toast"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="人间仙境"
    android:drawableLeft="@drawable/address"
    android:gravity="center_vertical"
    android:drawablePadding="5dp"
    android:padding="10dp"
    />

封装的工具类

public class AddressToast {
  private WindowManager mWm;
  private WindowManager.LayoutParams mParams;
  private Context mContext;
  private View mV;

  public AddressToast(Context context){
    this.mContext = context;
    // 1 获取WindowManger 对象
    mWm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    // 2 产生LayoutParams
    mParams = new WindowManager.LayoutParams();
    // 3 设置相关参数
    mParams.width=WindowManager.LayoutParams.WRAP_CONTENT;
    mParams.height=WindowManager.LayoutParams.WRAP_CONTENT;
    mParams.type = WindowManager.LayoutParams.TYPE_PRIORITY_PHONE;
    // params.flags = WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
    // | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
    // | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE;
    mParams.flags = WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON 
        | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;  
  }

  //显示文本
  public void show(String msg){
    // 4 产生一个View
    mV = View.inflate(mContext, R.layout.view_toast, null);
    // 5 WindowManager:addView(view,params);
    mWm.addView(mV, mParams);
  }
  public void hide() {
    if (mV != null) {
      if (mV.getParent() != null) {
        mWm.removeView(mV);
      }
      mV = null;
    }
  }
}

调用工具类的逻辑:

public void clickMe2(View v) {
    mToast = new AddressToast(this);
    mToast.show("好迪真好大家好才是....");
  }

  public void clickMe3(View v) {
    mToast.hide();
  }
  //onPause()一定要执行,否则报错
  @Override
  protected void onPause() {
    super.onPause();
    //remove
    if(mV!=null){
      if(mV.getParent()!=null){
        mWm.removeView(mV);
      }
      mV=null;
    }
  }

对产生的view设置接触监听事件

刚刚的弹出的window还不能移动,可设置接触添加mv.setOnTouchListener(this)并实现接口OnTouchListener中的onTouch方法:

@Override
  public boolean onTouch(View v, MotionEvent event) {
    // getX是获取以widget左上角为坐标原点计算的X轴坐标值
    // getRawX 获取的是以屏幕左上角为坐标原点计算的X轴坐标值
    int action = event.getAction();
    switch (action) {
    case MotionEvent.ACTION_DOWN:
      // Log.d("example", "ACTION_DOWN");
      mDownX = event.getRawX();
      mDownY = event.getRawY();
      break;
    case MotionEvent.ACTION_MOVE:
      // Log.d("example", "ACTION_MOVE");
      float moveX = event.getRawX();
      float moveY = event.getRawY();
      // x
      int dx = (int) (moveX - mDownX + 0.5f); // 0.5f 四舍五入 (int)4.1 4
      int dy = (int) (moveY - mDownY + 0.5f);
      Log.d("example", "dx=" + dx);
      mParams.x += dx;
      mParams.y += dy;
      //7.更新位置二
      mWm.updateViewLayout(mV, mParams);
      // 重新给按下去点赋值
      mDownX = moveX;
      mDownY = moveY;
      break;
    case MotionEvent.ACTION_UP:
      // Log.d("example", "ACTION_UP");
      break;

    default:
      break;
    }
    return false;
  }
}

您可能感兴趣的文章:

相关文章