溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Android開發(fā)模仿qq視頻通話懸浮按鈕(實(shí)例代碼)

發(fā)布時(shí)間:2020-09-01 12:09:05 來源:腳本之家 閱讀:260 作者:我真的是小熊 欄目:移動(dòng)開發(fā)

模仿qq視頻通話的懸浮按鈕的實(shí)例代碼,如下所示;

public class FloatingWindowService extends Service{
  private static final String TAG="OnTouchListener";
  private static View mView = null;
  private static WindowManager mWindowManager = null;
  private static Context mContext = null;
  public static Boolean isShown = false;
  public WindowManager.LayoutParams params = null;
  private int pixel;
  private int TheOffset;
  @Override
  public void onCreate() {
    super.onCreate();
  }
  @Override
  public int onStartCommand(Intent intent, int flags, int startId) {
    pixel = intent.getIntExtra("pixel",1);
    showPopupWindow(this);
    return super.onStartCommand(intent, flags, startId);
  }
  /**
   * 顯示彈出框
   *
   * @param context
   *
   */
  private void showPopupWindow(final Context context) {
    if (isShown) {
      return;
    }
    isShown = true;
    // 獲取應(yīng)用的Context
    mContext = context.getApplicationContext();
    // 獲取WindowManager
    mWindowManager = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
    params = new WindowManager.LayoutParams();
    mView = setUpView(context);
    // 類型
    params.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
    int flags=WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
    params.flags = flags;
    params.format = PixelFormat.TRANSLUCENT;
    params.width = WindowManager.LayoutParams.WRAP_CONTENT;
    params.height = WindowManager.LayoutParams.WRAP_CONTENT;
    params.gravity = Gravity.CENTER;
    mWindowManager.addView(mView, params);
  }
  /**
   * 隱藏彈出框
   */
  private static void hidePopupWindow() {
    if (isShown && null != mView) {
      mWindowManager.removeView(mView);
      isShown = false;
    }
  }
  private  int x=0;
  private  int y=0;
  private  int startX=0;
  private  int startY=0;
  private View setUpView(final Context context) {
    View view = LayoutInflater.from(context).inflate(R.layout.popupwindow,
        null);
     TextView tv= (TextView) view.findViewById(R.id.title);
    int w = View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED);
    int h = View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED);
    tv.measure(w, h);
    TheOffset=(pixel-tv.getMeasuredWidth())/2-50;
    tv.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        Intent intent =new Intent(context,MainActivity.class);
        context.startActivity(intent);
        // Toast.makeText(context,"點(diǎn)擊事件",Toast.LENGTH_LONG).show();
      }
    });
    tv.setOnTouchListener(new View.OnTouchListener() {
      @Override
      public boolean onTouch(View v, MotionEvent event) {
        switch (event.getAction()){
          case MotionEvent.ACTION_MOVE:
            int newX= (int) (event.getRawX()-x);
            int newY= (int) (event.getRawY()-y);
            params.x=newX+startX;
            params.y=newY+startY;
              mWindowManager.updateViewLayout(mView,params);
            break;
          case MotionEvent.ACTION_DOWN:
            x= (int) event.getRawX();
            y= (int) event.getRawY();
            break;
          case MotionEvent.ACTION_UP:
            if(params.x>=0){
              params.x=TheOffset;
              mWindowManager.updateViewLayout(mView,params);
            }
            if(params.x<=-0){
              params.x=-TheOffset;
              mWindowManager.updateViewLayout(mView,params);
            }
            Log.i(TAG,params.x+"");
            Log.i(TAG,params.y+"");
            //判斷 從按住到抬起時(shí)候的移動(dòng)距離, 如果如果移動(dòng)距離大于20 那么就攔截事件,否則就不攔截事件,主要是處理點(diǎn)擊事件的沖突
            if(Math.abs(startX-params.x)>20 ||Math.abs(startY-params.y)>20 ){
              //記錄上一次的偏移量
              startX=params.x;
              startY=params.y;
              return true;
            }else {
              startX=params.x;
              startY=params.y;
              return false;
            }
        }
        return false;
      }
    });
    return view;
  }
  @Nullable
  @Override
  public IBinder onBind(Intent intent) {
    return null;
  }
  @Override
  public void onDestroy() {
    super.onDestroy();
    if (mView != null) {
      isShown=false;
      mWindowManager.removeView(mView);
    }
  }
  }

Main  

@Override
    protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      findViewById(R.id.open).setOnClickListener(this);
      findViewById(R.id.close).setOnClickListener(this);
    }

-點(diǎn)擊開啟 關(guān)閉懸浮按鈕 

@Override
    public void onClick(View v) {
      switch (v.getId()){
        case R.id.open:
          //判斷是否擁有懸浮權(quán)限
          //op 的值是 0 ~ 47,其中0代表粗略定位權(quán)限,1代表精確定位權(quán)限,24代表懸浮窗權(quán)限。(具體可以看看Android源碼在android.app下就有個(gè)AppOpsManager類)
          if(utils.checkOp(this,24)==0) {
            Intent intent=new Intent(MainActivity.this, FloatingWindowService.class);
            intent.putExtra("pixel",utils.pixel(this)[0]);
            startService(intent);
          }else {
            //引導(dǎo)用戶進(jìn)入懸浮權(quán)限設(shè)置界面
            Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
                Uri.parse("package:" + getPackageName()));
            startActivityForResult(intent, 200);
          }
          break;
        case R.id.close:
          stopService(new Intent(MainActivity.this,FloatingWindowService.class));
          break;
      }
    }

判斷權(quán)限 -獲取屏幕的寬高 

public class utils {
      public static int checkOp(Context context, int op){
        final int version = Build.VERSION.SDK_INT;
        if (version >= 19){
          Object object = context.getSystemService("appops");
          Class c = object.getClass();
          try {
            Class[] cArg = new Class[3];
            cArg[0] = int.class;
            cArg[1] = int.class;
            cArg[2] = String.class;
            Method lMethod = c.getDeclaredMethod("checkOp", cArg);
            return (Integer) lMethod.invoke(object, op, Binder.getCallingUid(), context.getPackageName());
          } catch(NoSuchMethodException e) {
            e.printStackTrace();
          } catch (IllegalAccessException e) {
            e.printStackTrace();
          } catch (IllegalArgumentException e) {
            e.printStackTrace();
          } catch (InvocationTargetException e) {
            e.printStackTrace();
          }
        }
        return -1;
      }
      /**
       * 獲取屏幕的寬高
       * @param context
       * @return
       */
      public static int[] pixel(Activity context){
        DisplayMetrics dm = new DisplayMetrics();
        context.getWindowManager().getDefaultDisplay().getMetrics(dm);
        return new int[]{dm.widthPixels,dm.heightPixels};
      }
    }

--popupwindow填充布局文件 

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         android:orientation="vertical">
    <LinearLayout
      android:id="@+id/popup_window"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:background="@android:color/white"
      android:orientation="vertical" >
      <TextView
        android:background="@mipmap/ic_launcher"
        android:id="@+id/title"
        android:layout_width="50dp"
        android:layout_height="50dp"/>
      </LinearLayout>
  </LinearLayout>

以上所述是小編給大家介紹的Android開發(fā)模仿qq視頻通話懸浮按鈕(實(shí)例代碼),希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)億速云網(wǎng)站的支持!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI