欢迎来到代码驿站!

Android代码

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

Android自定义APP全局悬浮按钮

时间:2020-12-16 09:50:10|栏目:Android代码|点击:

原本想通过framelayout实现一个悬浮在其他控件上的按钮,但是觉得很麻烦,需要各个界面都要动态填充.于是想到了悬浮窗,就自定一个ImageView用于显示全局按钮.

一、首先因为悬浮窗式的所以要添加权限,对于SDK>=23的需要动态获取权限,我这边用的是22的

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

二、通过application获取到全局性的WindowManager的params数据

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

三、自定义ImageView,并实现点击具有状态选择.其中写了一个回调接口用于对点击事件的处理

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);
  }

  /**
   * 状态栏的高度
   *
   */
  public static int getStatusHeight(Context context) {
    int statusHeight = -1;
    try {
      Class clazz = Class.forName("com.android.internal.R$dimen");  //使用反射获取实例
      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) {
    //获取相对屏幕的坐标,即以屏幕左上角为原点
    x = event.getRawX();
    y = event.getRawY() - statusHeight;  //statusHeight是系统状态栏的高度
    switch (event.getAction()) {
      case MotionEvent.ACTION_DOWN:  //按下
        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:  //手指移动
        updateViewPosition();
        isMove = true;
        break;
      case MotionEvent.ACTION_UP:  //手抬起
        setImageResource(R.drawable.btn_voice_rest);
        mLastX = event.getRawX();
        mLastY = event.getRawY();
        mUpTime = System.currentTimeMillis();
        //按下到抬起的时间大于500毫秒,并且抬手到抬手绝对值大于20像素处理点击事件
        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); //刷新显示
  }

  /**
   * 设置点击回调接口
   */
  public interface OnSpeakListener{
    void onSpeakListener();
  }
  public void setOnSpeakListener(OnSpeakListener listener){
    this.listener=listener;
  }
}

四、Activity中使用,其中有设置图片的参数和位置参数

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;//设置背景图片
    wmParams.flags= WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE ;//
    wmParams.gravity = Gravity.LEFT|Gravity.TOP;//
    wmParams.x = widthPixels-150;  //设置位置像素
    wmParams.y = heightPixels-110;
    wmParams.width=200; //设置图片大小
    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, "点击事件", Toast.LENGTH_SHORT).show();
      }
    });
  }

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

上一篇:Android 利用三阶贝塞尔曲线绘制运动轨迹的示例

栏    目:Android代码

下一篇:android判断动画已结束示例代码

本文标题:Android自定义APP全局悬浮按钮

本文地址:http://www.codeinn.net/misctech/33376.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有