本文實(shí)例為大家分享了Android實(shí)現(xiàn)觸摸移動(dòng)的懸浮窗的具體代碼,供大家參考,具體內(nèi)容如下
成都創(chuàng)新互聯(lián)公司長(zhǎng)期為成百上千客戶提供的網(wǎng)站建設(shè)服務(wù),團(tuán)隊(duì)從業(yè)經(jīng)驗(yàn)10年,關(guān)注不同地域、不同群體,并針對(duì)不同對(duì)象提供差異化的產(chǎn)品和服務(wù);打造開放共贏平臺(tái),與合作伙伴共同營(yíng)造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為雙鴨山企業(yè)提供專業(yè)的網(wǎng)站建設(shè)、成都做網(wǎng)站,雙鴨山網(wǎng)站改版等技術(shù)服務(wù)。擁有10年豐富建站經(jīng)驗(yàn)和眾多成功案例,為您定制開發(fā)。
1.觸摸移動(dòng)的懸浮窗的快速實(shí)現(xiàn) UI
2. 懸浮窗實(shí)現(xiàn)
2.1 懸浮窗權(quán)限
為了偷懶,我SDK選擇比較低的17,如果不配置權(quán)限,則應(yīng)用會(huì)運(yùn)行報(bào)錯(cuò)
正規(guī)流程,查詢是否有懸浮窗權(quán)限,如果沒有需要提示,并跳轉(zhuǎn)到懸浮窗設(shè)置界面“即是否允許運(yùn)行在其他應(yīng)用上面”
2.2 懸浮窗配置初始化
這里主要關(guān)注 mParams.type 和 mParams.flags 這兩個(gè)參數(shù)是核心,我們也可以使用View來加載一個(gè)布局,為了側(cè)重懸浮窗界面,我直接使用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; // 實(shí)現(xiàn)懸浮窗可以移動(dòng)的屬性 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; // 實(shí)現(xiàn)懸浮窗可以移動(dòng)的屬性 mParams.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE; mParams.format = PixelFormat.TRANSPARENT; } } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。