Android Custom APP Global Floating Button

  • 2021-08-12 03:29:11
  • OfStack

Originally, I wanted to realize a button suspended on other controls through framelayout, but I felt very troublesome and needed to dynamically fill every interface. So I thought of the floating window and customized an ImageView for displaying global buttons.

1. First of all, you need to add permissions because of the floating window. For SDK > = 23 needs to get permission dynamically, but I use 22 here


 <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
  <uses-permission android:name="android.permission.WRITE_SETTINGS"/>

2. Acquiring params data of global WindowManager through application


 private WindowManager.LayoutParams wmParams=new WindowManager.LayoutParams();
  public WindowManager.LayoutParams getMywmParams(){
    return wmParams;
  }

3. Custom ImageView, and realize click with state selection. One callback interface is written to deal with click events


public class CustomeMovebutton extends ImageView {
  private final int statusHeight;
  int sW;
  int sH;
  private float mTouchStartX;
  private float mTouchStartY;
  private float x;
  private float y;
  private boolean isMove=false;
  private Context context;
  private WindowManager wm = (WindowManager) getContext().getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
  private WindowManager.LayoutParams wmParams = ((MyApplication) getContext().getApplicationContext()).getMywmParams();
  private float mLastX;
  private float mLastY;
  private float mStartX;
  private float mStartY;
  private long mDownTime;
  private long mUpTime;
  private OnSpeakListener listener;

  public CustomeMovebutton(Context context) {
    this(context,null);
    this.context = context;
  }
  public CustomeMovebutton(Context context, AttributeSet attrs) {
    this(context, attrs,-1);
  }
  public CustomeMovebutton(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs,defStyleAttr);
    sW = wm.getDefaultDisplay().getWidth();
    sH = wm.getDefaultDisplay().getHeight();
    statusHeight = getStatusHeight(context);
  }

  /**
   *  Height of status bar 
   *
   */
  public static int getStatusHeight(Context context) {
    int statusHeight = -1;
    try {
      Class clazz = Class.forName("com.android.internal.R$dimen");  // Getting instances using reflection 
      Object object = clazz.newInstance();
      int height = Integer.parseInt(clazz.getField("status_bar_height")
          .get(object).toString());
      statusHeight = context.getResources().getDimensionPixelSize(height);
    } catch (Exception e) {
      e.printStackTrace();
    }
    return statusHeight;
  }
  @Override
  public boolean onTouchEvent(MotionEvent event) {
    // Gets the coordinates relative to the screen, that is, starting from the upper left corner of the screen 
    x = event.getRawX();
    y = event.getRawY() - statusHeight;  //statusHeight Is the height of the system status bar 
    switch (event.getAction()) {
      case MotionEvent.ACTION_DOWN:  // Press 
        setImageResource(R.drawable.btn_voice_pressed);
        mTouchStartX = event.getX();
        mTouchStartY = event.getY();
        mStartX = event.getRawX();
        mStartY = event.getRawY();
        mDownTime = System.currentTimeMillis();
        isMove = false;
        break;
      case MotionEvent.ACTION_MOVE:  // Finger movement 
        updateViewPosition();
        isMove = true;
        break;
      case MotionEvent.ACTION_UP:  // Hands up 
        setImageResource(R.drawable.btn_voice_rest);
        mLastX = event.getRawX();
        mLastY = event.getRawY();
        mUpTime = System.currentTimeMillis();
        // The time from pressing to lifting is greater than 500 Milliseconds , And the absolute value from raising your hand to raising your hand is greater than 20 Pixel processing click events 
        if(mUpTime - mDownTime < 500){
          if(Math.abs(mStartX- mLastX )< 20.0 && Math.abs(mStartY - mLastY) < 20.0){
              if (listener!=null){
                listener.onSpeakListener();
              }
          }
        }

        break;
    }
    return true;
  }

  private void updateViewPosition() {
    wmParams.x = (int) (x - mTouchStartX);
    wmParams.y = (int) (y- mTouchStartY);
    wm.updateViewLayout(this, wmParams); // Refresh display 
  }

  /**
   *  Setting the click callback interface 
   */
  public interface OnSpeakListener{
    void onSpeakListener();
  }
  public void setOnSpeakListener(OnSpeakListener listener){
    this.listener=listener;
  }
}

4. Used in Activity, which has parameters for setting pictures and position parameters


public class MainActivity extends AppCompatActivity{

  private WindowManager wm;
  private WindowManager.LayoutParams wmParams;
  private CustomeMovebutton CustomeMovebutton;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    wm = (WindowManager) getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
    DisplayMetrics dm = getResources().getDisplayMetrics();
    int widthPixels = dm.widthPixels;
    int heightPixels = dm.heightPixels;
    wmParams = ((MyApplication) getApplication()).getMywmParams();
    wmParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
    wmParams.format= PixelFormat.RGBA_8888;// Set the background picture 
    wmParams.flags= WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE ;//
    wmParams.gravity = Gravity.LEFT|Gravity.TOP;//
    wmParams.x = widthPixels-150;  // Set position pixels 
    wmParams.y = heightPixels-110;
    wmParams.width=200; // Set the picture size 
    wmParams.height=200;
    CustomeMovebutton = new CustomeMovebutton(getApplicationContext());
    CustomeMovebutton.setImageResource(R.drawable.btn_voice_rest);
    wm.addView(CustomeMovebutton, wmParams);
    CustomeMovebutton.setOnSpeakListener(new CustomeMovebutton.OnSpeakListener() {
      @Override
      public void onSpeakListener() {
        Toast.makeText(MainActivity.this, " Click event ", Toast.LENGTH_SHORT).show();
      }
    });
  }

  @Override
  protected void onDestroy() {
    super.onDestroy();
    if(CustomeMovebutton != null){
      wm.removeView(CustomeMovebutton);
    }
  }
}

Related articles: