Android實現(xiàn)全局懸浮框
本文實例為大家分享了Android實現(xiàn)全局懸浮框的具體代碼,供大家參考,具體內(nèi)容如下
效果圖:
代碼實現(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: // 獲取相對View的坐標,即以此View左上角為原點 mInViewX = motionEvent.getX(); mInViewY = motionEvent.getY(); // 獲取相對屏幕的坐標,即以屏幕左上角為原點 mDownInScreenX = motionEvent.getRawX(); mDownInScreenY = motionEvent.getRawY() - getSysBarHeight(mContext); mInScreenX = motionEvent.getRawX(); mInScreenY = motionEvent.getRawY() - getSysBarHeight(mContext); break; case MotionEvent.ACTION_MOVE: // 更新浮動窗口位置參數(shù) mInScreenX = motionEvent.getRawX(); mInScreenY = motionEvent.getRawY() - getSysBarHeight(mContext); mWindowParams.x = (int) (mInScreenX- mInViewX); mWindowParams.y = (int) (mInScreenY - mInViewY); // 手指移動的時候更新小懸浮窗的位置 mWindowManager.updateViewLayout(mFloatLayout, mWindowParams); break; case MotionEvent.ACTION_UP: // 如果手指離開屏幕時,xDownInScreen和xInScreen相等,且yDownInScreen和yInScreen相等,則視為觸發(fā)了單擊事件。 if (mDownInScreenX == mInScreenX && mDownInScreenY == mInScreenY){ } break; } return true; } public void showFloatWindow(){ if (mFloatLayout.getParent() == null){ DisplayMetrics metrics = new DisplayMetrics(); // 默認固定位置,靠屏幕右邊緣的中間 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())) { // 啟動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(); } } }
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android開發(fā)實現(xiàn)的Log統(tǒng)一管理類
這篇文章主要介紹了Android開發(fā)實現(xiàn)的Log統(tǒng)一管理類,涉及Android日志管理及方法重載等相關(guān)操作技巧,需要的朋友可以參考下2017-12-12Android開啟ADB網(wǎng)絡(luò)調(diào)試方法
今天小編就為大家分享一篇Android開啟ADB網(wǎng)絡(luò)調(diào)試方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-08-08Android 接收微信、QQ其他應(yīng)用打開第三方分享功能
這篇文章主要介紹了Android 接收微信、QQ其他應(yīng)用打開,第三方分享 ,思路很簡單通過在AndroidManifest.xml注冊ACTION事件,在用于接收分享的Activity里面加接收代碼,感興趣的朋友可以一起學習下2022-11-11Android數(shù)據(jù)雙向綁定原理實現(xiàn)和應(yīng)用場景
本文介紹了Android數(shù)據(jù)雙向綁定的原理和實現(xiàn)方式,包括基于觀察者模式和數(shù)據(jù)綁定框架的實現(xiàn)方法,以及應(yīng)用場景和優(yōu)缺點的分析,幫助開發(fā)者了解和應(yīng)用數(shù)據(jù)雙向綁定技術(shù),提升應(yīng)用的交互性和響應(yīng)速度2023-04-04Android XmlResourceParser出錯解決辦法
這篇文章主要介紹了Android XmlResourceParser出錯解決辦法的相關(guān)資料,需要的朋友可以參考下2017-05-05Android 中FloatingActionButton(懸浮按鈕)實例詳解
這篇文章主要介紹了Android 中FloatingActionButton(懸浮按鈕)實例詳解的相關(guān)資料,需要的朋友可以參考下2017-05-05Android搜索結(jié)果顯示高亮實例(有數(shù)據(jù)滑動底部自動刷新)
本篇文章主要介紹了Android搜索結(jié)果顯示高亮實例(有數(shù)據(jù)滑動底部自動刷新),非常具有實用價值,需要的朋友可以參考下2017-04-04android開發(fā)通過Scroller實現(xiàn)過渡滑動效果操作示例
這篇文章主要介紹了android開發(fā)通過Scroller實現(xiàn)過渡滑動效果,結(jié)合實例形式分析了Android Scroller類實現(xiàn)過渡滑動效果的基本原理與實現(xiàn)技巧,需要的朋友可以參考下2020-01-01