最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android實(shí)現(xiàn)全局懸浮框

 更新時(shí)間:2021年01月26日 11:54:05   作者:tracydragonlxy  
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)全局懸浮框,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了Android實(shí)現(xiàn)全局懸浮框的具體代碼,供大家參考,具體內(nèi)容如下

效果圖:

代碼實(shí)現(xiàn):

Androidmanifest.xml添加彈框權(quán)限

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

自定義懸浮窗類FloatWindow.java

public class FloatWindow implements View.OnTouchListener {

 private Context mContext;
 private WindowManager.LayoutParams mWindowParams;
 private WindowManager mWindowManager;

 private View mFloatLayout;
 private float mInViewX;
 private float mInViewY;
 private float mDownInScreenX;
 private float mDownInScreenY;
 private float mInScreenX;
 private float mInScreenY;
 private TextView infoText;

 public FloatWindow(Context context) {
  this.mContext = context;
  initFloatWindow();
 }

 private void initFloatWindow() {
  LayoutInflater inflater = LayoutInflater.from(mContext);
  if(inflater == null)
   return;
  mFloatLayout = (View) inflater.inflate(R.layout.layout_float, null);
  infoText = mFloatLayout.findViewById(R.id.textView);
  mFloatLayout.setOnTouchListener(this);

  mWindowParams = new WindowManager.LayoutParams();
  mWindowManager = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
  if (Build.VERSION.SDK_INT >= 26) {//8.0新特性
   mWindowParams.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
  }else{
   mWindowParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
  }
  mWindowParams.format = PixelFormat.RGBA_8888;
  mWindowParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
  mWindowParams.gravity = Gravity.START | Gravity.TOP;
  mWindowParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
  mWindowParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
 }

 @Override
 public boolean onTouch(View view, MotionEvent motionEvent) {
  return floatLayoutTouch(motionEvent);
 }

 private boolean floatLayoutTouch(MotionEvent motionEvent) {
  switch (motionEvent.getAction()) {
   case MotionEvent.ACTION_DOWN:
    // 獲取相對(duì)View的坐標(biāo),即以此View左上角為原點(diǎn)
    mInViewX = motionEvent.getX();
    mInViewY = motionEvent.getY();
    // 獲取相對(duì)屏幕的坐標(biāo),即以屏幕左上角為原點(diǎn)
    mDownInScreenX = motionEvent.getRawX();
    mDownInScreenY = motionEvent.getRawY() - getSysBarHeight(mContext);
    mInScreenX = motionEvent.getRawX();
    mInScreenY = motionEvent.getRawY() - getSysBarHeight(mContext);
    break;
   case MotionEvent.ACTION_MOVE:
    // 更新浮動(dòng)窗口位置參數(shù)
    mInScreenX = motionEvent.getRawX();
    mInScreenY = motionEvent.getRawY() - getSysBarHeight(mContext);
    mWindowParams.x = (int) (mInScreenX- mInViewX);
    mWindowParams.y = (int) (mInScreenY - mInViewY);
    // 手指移動(dòng)的時(shí)候更新小懸浮窗的位置
    mWindowManager.updateViewLayout(mFloatLayout, mWindowParams);
    break;
   case MotionEvent.ACTION_UP:
    // 如果手指離開(kāi)屏幕時(shí),xDownInScreen和xInScreen相等,且yDownInScreen和yInScreen相等,則視為觸發(fā)了單擊事件。
    if (mDownInScreenX == mInScreenX && mDownInScreenY == mInScreenY){

    }
    break;
  }
  return true;
 }

 public void showFloatWindow(){
  if (mFloatLayout.getParent() == null){
   DisplayMetrics metrics = new DisplayMetrics();
   // 默認(rèn)固定位置,靠屏幕右邊緣的中間
   mWindowManager.getDefaultDisplay().getMetrics(metrics);
   mWindowParams.x = metrics.widthPixels;
   mWindowParams.y = metrics.heightPixels/2 - getSysBarHeight(mContext);
   mWindowManager.addView(mFloatLayout, mWindowParams);
  }
 }

 public void updateText(final String s) {
  infoText.setText(s);
 }

 public void hideFloatWindow(){
  if (mFloatLayout.getParent() != null)
   mWindowManager.removeView(mFloatLayout);
 }

 public void setFloatLayoutAlpha(boolean alpha){
  if (alpha)
   mFloatLayout.setAlpha((float) 0.5);
  else
   mFloatLayout.setAlpha(1);
 }

 // 獲取系統(tǒng)狀態(tài)欄高度
 public static int getSysBarHeight(Context contex) {
  Class<?> c;
  Object obj;
  Field field;
  int x;
  int sbar = 0;
  try {
   c = Class.forName("com.android.internal.R$dimen");
   obj = c.newInstance();
   field = c.getField("status_bar_height");
   x = Integer.parseInt(field.get(obj).toString());
   sbar = contex.getResources().getDimensionPixelSize(x);
  } catch (Exception e1) {
   e1.printStackTrace();
  }
  return sbar;
 }
}

自定義懸浮窗界面布局文件layout_float.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 xmlns:app="http://schemas.android.com/apk/res-auto">

 <ImageView
  android:id="@+id/imageView"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:src="@mipmap/float_win"
  app:layout_constraintStart_toStartOf="parent"
  app:layout_constraintTop_toTopOf="parent"/>

 <TextView
  android:id="@+id/textView"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:background="#00ffffff"
  android:text="hello"
  android:textSize="12sp"
  app:layout_constraintLeft_toLeftOf="@id/imageView"
  app:layout_constraintRight_toRightOf="@id/imageView"
  app:layout_constraintTop_toBottomOf="@id/imageView"/>

</android.support.constraint.ConstraintLayout>

在Activity中使用懸浮窗。

public class MainActivity extends AppCompatActivity {

 private Button btnShow;
 FloatWindow floatWindow;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);

  // 權(quán)限判斷
  if (Build.VERSION.SDK_INT >= 23) {
   if(!Settings.canDrawOverlays(getApplicationContext())) {
    // 啟動(dòng)Activity讓用戶授權(quán)
    Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,Uri.parse("package:" + getPackageName()));
    startActivityForResult(intent,10);
   } else {
    // 執(zhí)行6.0以上繪制代碼
    initView();
   }
  } else {
   // 執(zhí)行6.0以下繪制代碼
   initView();
  }
 }

 @Override
 protected void onResume() {
  super.onResume();
  // 權(quán)限判斷
  if (Build.VERSION.SDK_INT >= 23) {
   if(Settings.canDrawOverlays(getApplicationContext())) {
    initView();
   }
  } else {
   //執(zhí)行6.0以下繪制代碼
   initView();
  }
 }

 private void initView() {
  setContentView(R.layout.activity_main);
  floatWindow = new FloatWindow(getApplicationContext());

  btnShow = findViewById(R.id.btn_show);
  btnShow.setOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(View view) {
    if (null != floatWindow) {
     floatWindow.showFloatWindow();
    }
   }
  });

  Button btnrefresh = findViewById(R.id.btn_refresh);
  btnrefresh.setOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(View view) {
    int random = (int) (Math.random() * 10);
    if (null != floatWindow) {
     floatWindow.updateText(String.valueOf(random));
    }
   }
  });
 }

 @Override
 protected void onDestroy() {
  super.onDestroy();
  if (null != floatWindow) {
   floatWindow.hideFloatWindow();
  }
 }
}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Android Studio 中aidl的自定義類的使用詳解

    Android Studio 中aidl的自定義類的使用詳解

    這篇文章主要介紹了Android Studio 中aidl的自定義類的使用詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-03-03
  • Android如何獲取雙卡手機(jī)IMEI的方法示例

    Android如何獲取雙卡手機(jī)IMEI的方法示例

    這篇文章主要介紹了Android如何獲取雙卡手機(jī)IMEI的方法示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-09-09
  • Android如何實(shí)現(xiàn)一個(gè)DocumentProvider示例詳解

    Android如何實(shí)現(xiàn)一個(gè)DocumentProvider示例詳解

    這篇文章主要為大家介紹了Android如何實(shí)現(xiàn)一個(gè)DocumentProvider示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-12-12
  • 一文帶你了解Flutter數(shù)據(jù)表格的使用

    一文帶你了解Flutter數(shù)據(jù)表格的使用

    目前,越來(lái)越多的管理層(所謂的領(lǐng)導(dǎo))都希望在手機(jī)端查看各種各樣的數(shù)據(jù)報(bào)表,以達(dá)到隨時(shí)隨地關(guān)注經(jīng)營(yíng)業(yè)績(jī)(監(jiān)督干活)的目的。本篇我們就來(lái)介紹?Flutter?的數(shù)據(jù)表格的使用,希望對(duì)大家有所幫助
    2022-11-11
  • Flutter搞定寬高不統(tǒng)一布局開(kāi)發(fā)的方法詳解

    Flutter搞定寬高不統(tǒng)一布局開(kāi)發(fā)的方法詳解

    我們?cè)陂_(kāi)發(fā)移動(dòng)端界面的時(shí)候,經(jīng)常會(huì)遇到一組尺寸不一的組件需要作為同一組展示,所以本文就將利用Wrap組件搞定寬高不統(tǒng)一布局開(kāi)發(fā),需要的可以參考一下
    2023-06-06
  • Android基礎(chǔ)之Activity生命周期

    Android基礎(chǔ)之Activity生命周期

    activity類是Android 應(yīng)用生命周期的重要部分。在系統(tǒng)中的Activity被一個(gè)Activity棧所管理。當(dāng)一個(gè)新的Activity啟動(dòng)時(shí),將被放置到棧頂,成為運(yùn)行中的Activity,前一個(gè)Activity保留在棧中,不再放到前臺(tái),直到新的Activity退出為止。
    2016-05-05
  • Android仿銀行客戶簽名并且保存簽名的截圖文件并命名為本地時(shí)間

    Android仿銀行客戶簽名并且保存簽名的截圖文件并命名為本地時(shí)間

    本文通過(guò)實(shí)例代碼給大家介紹了Android仿銀行客戶簽名并且保存簽名的截圖文件并命名為本地時(shí)間,需要的朋友可以參考下
    2017-07-07
  • Android apk 項(xiàng)目一鍵打包并上傳到蒲公英的實(shí)現(xiàn)方法

    Android apk 項(xiàng)目一鍵打包并上傳到蒲公英的實(shí)現(xiàn)方法

    這篇文章主要介紹了Android apk 項(xiàng)目一鍵打包并上傳到蒲公英,本文通過(guò)示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-06-06
  • android 自定義控件 自定義屬性詳細(xì)介紹

    android 自定義控件 自定義屬性詳細(xì)介紹

    在android相關(guān)應(yīng)用開(kāi)發(fā)過(guò)程中,固定的一些屬性可能滿足不了開(kāi)發(fā)的需求,所以在一些特殊情況下,需要自定義控件與屬性,本文將以此問(wèn)題進(jìn)行詳細(xì)介紹,需要的朋友可以參考下
    2012-11-11
  • android listview實(shí)現(xiàn)新聞列表展示效果

    android listview實(shí)現(xiàn)新聞列表展示效果

    這篇文章主要為大家詳細(xì)介紹了android listview實(shí)現(xiàn)新聞列表展示效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-03-03

最新評(píng)論

买车| 高雄县| 化德县| 浪卡子县| 哈巴河县| 双牌县| 建阳市| 湖口县| 长武县| 平罗县| 微山县| 绥宁县| 阿荣旗| 任丘市| 金塔县| 山丹县| 沁阳市| 四会市| 灵璧县| 镇远县| 潞西市| 鸡泽县| 黄石市| 噶尔县| 湄潭县| 曲沃县| 寿光市| 永顺县| 嘉鱼县| 麻栗坡县| 万山特区| 孟村| 丹巴县| 金阳县| 泰顺县| 垦利县| 固阳县| 敦化市| 资讯 | 仪征市| 双牌县|