您好,登錄后才能下訂單哦!
本文實例為大家分享了Android實現觸摸移動的懸浮窗的具體代碼,供大家參考,具體內容如下
1.觸摸移動的懸浮窗的快速實現 UI
2. 懸浮窗實現
2.1 懸浮窗權限
為了偷懶,我SDK選擇比較低的17,如果不配置權限,則應用會運行報錯
正規流程,查詢是否有懸浮窗權限,如果沒有需要提示,并跳轉到懸浮窗設置界面“即是否允許運行在其他應用上面”
<uses-sdk android:minSdkVersion="17" android:targetSdkVersion="17" /> <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
2.2 懸浮窗配置初始化
這里主要關注 mParams.type 和 mParams.flags 這兩個參數是核心,我們也可以使用View來加載一個布局,為了側重懸浮窗界面,我直接使用TextView演示
private void initFloatWindow() { if (mShow == null) { mWm = (WindowManager) getSystemService(Context.WINDOW_SERVICE); mShow = new TextView(this); mShow.setText("I Love You"); mShow.setTextColor(Color.RED); mShow.setTextSize(50); mShow.setOnTouchListener(new View.OnTouchListener() { int lastX = 0; int lastY = 0; int paramX = 0; int paramY = 0; @Override public boolean onTouch(View view, MotionEvent motionEvent) { switch (motionEvent.getAction()) { case MotionEvent.ACTION_DOWN: lastX = (int) motionEvent.getRawX(); lastY = (int) motionEvent.getRawY(); paramX = mParams.x; paramY = mParams.y; break; case MotionEvent.ACTION_MOVE: int dx = (int) motionEvent.getRawX() - lastX; int dy = (int) motionEvent.getRawY() - lastY; mParams.x = paramX + dx; mParams.y = paramY + dy; mWm.updateViewLayout(mShow, mParams); break; } return true; } }); mParams = new WindowManager.LayoutParams(); mParams.width = WindowManager.LayoutParams.WRAP_CONTENT; mParams.height = WindowManager.LayoutParams.WRAP_CONTENT; mParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT; // 實現懸浮窗可以移動的屬性 mParams.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE; mParams.format = PixelFormat.TRANSPARENT; } }
2.3 懸浮窗顯示
mWm.addView(mShow, mParams);
2.4 懸浮窗隱藏
mWm.removeView(mShow);
2.5 完整示例代碼
package com.wu.sufadi; import android.app.Activity; import android.content.Context; import android.graphics.Color; import android.graphics.PixelFormat; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.view.LayoutInflater; import android.view.MotionEvent; import android.view.View; import android.view.WindowManager; import android.widget.TextView; public class MainActivity extends Activity { private TextView mShow; private WindowManager mWm; private WindowManager.LayoutParams mParams; private static final int MSG_SHOW_WINDOW = 1; private static final int MSG_HIDE_WINDOW = 2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initFloatWindow(); } public void click_show(View view) { showFloatWindow(); } public void click_hide(View view) { hideFloatWindow(); } Handler handler = new Handler() { @Override public void handleMessage(Message msg) { super.handleMessage(msg); switch (msg.what) { case MSG_SHOW_WINDOW: mWm.addView(mShow, mParams); break; case MSG_HIDE_WINDOW: mWm.removeView(mShow); break; default: break; } } }; private void showFloatWindow() { handler.sendEmptyMessage(MSG_SHOW_WINDOW); } private void hideFloatWindow() { handler.sendEmptyMessage(MSG_HIDE_WINDOW); } private void initFloatWindow() { if (mShow == null) { mWm = (WindowManager) getSystemService(Context.WINDOW_SERVICE); mShow = new TextView(this); mShow.setText("I Love You"); mShow.setTextColor(Color.RED); mShow.setTextSize(50); mShow.setOnTouchListener(new View.OnTouchListener() { int lastX = 0; int lastY = 0; int paramX = 0; int paramY = 0; @Override public boolean onTouch(View view, MotionEvent motionEvent) { switch (motionEvent.getAction()) { case MotionEvent.ACTION_DOWN: lastX = (int) motionEvent.getRawX(); lastY = (int) motionEvent.getRawY(); paramX = mParams.x; paramY = mParams.y; break; case MotionEvent.ACTION_MOVE: int dx = (int) motionEvent.getRawX() - lastX; int dy = (int) motionEvent.getRawY() - lastY; mParams.x = paramX + dx; mParams.y = paramY + dy; mWm.updateViewLayout(mShow, mParams); break; } return true; } }); mParams = new WindowManager.LayoutParams(); mParams.width = WindowManager.LayoutParams.WRAP_CONTENT; mParams.height = WindowManager.LayoutParams.WRAP_CONTENT; mParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT; // 實現懸浮窗可以移動的屬性 mParams.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE; mParams.format = PixelFormat.TRANSPARENT; } } }
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。