android studio悬浮窗
时间: 2023-12-25 13:29:53 浏览: 236
在Android Studio中实现悬浮窗可以通过创建一个Service来实现。下面是一个简单的示例代码:
```java
import android.app.Service;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.PixelFormat;
import android.os.Build;
import android.os.IBinder;
import android.util.Log;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.LinearLayout;
public class FloatingWindowService extends Service {
private WindowManager windowManager;
private LinearLayout floatingLayout;
@Override
public void onCreate() {
super.onCreate();
// 创建悬浮窗布局
floatingLayout = new LinearLayout(this);
floatingLayout.setBackgroundColor(Color.TRANSPARENT);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT
);
floatingLayout.setLayoutParams(layoutParams);
// 创建悬浮窗按钮
Button floatingButton = new Button(this);
floatingButton.setText("悬浮按钮");
floatingButton.setBackgroundColor(Color.BLUE);
floatingButton.setTextColor(Color.WHITE);
floatingButton.setAlpha(0.7f);
floatingButton.setOnTouchListener(new FloatingOnTouchListener());
floatingLayout.addView(floatingButton);
// 设置悬浮窗参数
WindowManager.LayoutParams params;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT
);
} else {
params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_PHONE,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT
);
}
params.gravity = Gravity.TOP | Gravity.START;
params.x = 0;
params.y = 100;
// 添加悬浮窗到窗口管理器
windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
windowManager.addView(floatingLayout, params);
}
@Override
public void onDestroy() {
super.onDestroy();
if (floatingLayout != null) {
windowManager.removeView(floatingLayout);
}
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
private class FloatingOnTouchListener implements View.OnTouchListener {
private int initialX;
private int initialY;
private float initialTouchX;
private float initialTouchY;
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
initialX = windowManagerParams.x;
initialY = windowManagerParams.y;
initialTouchX = event.getRawX();
initialTouchY = event.getRawY();
return true;
case MotionEvent.ACTION_MOVE:
int newX = initialX + (int) (event.getRawX() - initialTouchX);
int newY = initialY + (int) (event.getRawY() - initialTouchY);
windowManagerParams.x = newX;
windowManagerParams.y = newY;
windowManager.updateViewLayout(floatingLayout, windowManagerParams);
return true;
}
return false;
}
}
}
```
请注意,为了在Android 8.0及更高版本上正常工作,需要在AndroidManifest.xml文件中添加以下权限:
```xml
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
```
此外,还需要在AndroidManifest.xml文件中注册Service:
```xml
<service android:name=".FloatingWindowService" />
```
请确保在使用悬浮窗时遵守相关的隐私和安全规定。
阅读全文