android 用java编写一个悬浮窗图片按钮,id为cat,点击cat 环绕展开 a b c 三个 image,自动吸附边缘时收回 a b c三个image
时间: 2024-03-14 08:47:20 浏览: 105
好的,以下是一个简单的实现悬浮窗图片按钮的示例代码,你可以参考一下:
```
public class FloatingCatButton extends Service implements View.OnTouchListener {
private WindowManager windowManager;
private ImageView catView;
private ImageView aView;
private ImageView bView;
private ImageView cView;
private WindowManager.LayoutParams catLayoutParams;
private WindowManager.LayoutParams aLayoutParams;
private WindowManager.LayoutParams bLayoutParams;
private WindowManager.LayoutParams cLayoutParams;
private int catWidth;
private int screenWidth;
private int screenHeight;
private int statusBarHeight;
private int lastX;
private int lastY;
private boolean isExpanded;
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
// 获取屏幕宽高和状态栏高度
DisplayMetrics displayMetrics = new DisplayMetrics();
windowManager.getDefaultDisplay().getMetrics(displayMetrics);
screenWidth = displayMetrics.widthPixels;
screenHeight = displayMetrics.heightPixels;
statusBarHeight = getStatusBarHeight();
// 初始化悬浮窗和按钮布局参数
catView = new ImageView(this);
catView.setImageResource(R.drawable.cat);
catLayoutParams = new WindowManager.LayoutParams();
catLayoutParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
catLayoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
catLayoutParams.gravity = Gravity.TOP | Gravity.LEFT;
catLayoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
| WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS;
catLayoutParams.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
catView.setOnTouchListener(this);
aView = new ImageView(this);
aView.setImageResource(R.drawable.a);
aLayoutParams = new WindowManager.LayoutParams();
aLayoutParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
aLayoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
aLayoutParams.gravity = Gravity.TOP | Gravity.LEFT;
aLayoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
| WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS;
aLayoutParams.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
aView.setVisibility(View.GONE);
bView = new ImageView(this);
bView.setImageResource(R.drawable.b);
bLayoutParams = new WindowManager.LayoutParams();
bLayoutParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
bLayoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
bLayoutParams.gravity = Gravity.TOP | Gravity.LEFT;
bLayoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
| WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS;
bLayoutParams.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
bView.setVisibility(View.GONE);
cView = new ImageView(this);
cView.setImageResource(R.drawable.c);
cLayoutParams = new WindowManager.LayoutParams();
cLayoutParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
cLayoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
cLayoutParams.gravity = Gravity.TOP | Gravity.LEFT;
cLayoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
| WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS;
cLayoutParams.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
cView.setVisibility(View.GONE);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
windowManager.addView(catView, catLayoutParams);
windowManager.addView(aView, aLayoutParams);
windowManager.addView(bView, bLayoutParams);
windowManager.addView(cView, cLayoutParams);
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
super.onDestroy();
if (catView != null) {
windowManager.removeView(catView);
}
if (aView != null) {
windowManager.removeView(aView);
}
if (bView != null) {
windowManager.removeView(bView);
}
if (cView != null) {
windowManager.removeView(cView);
}
}
@Override
public boolean onTouch(View view, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
// 记录按下时的坐标
lastX = (int) event.getRawX();
lastY = (int) event.getRawY();
catWidth = view.getWidth();
break;
case MotionEvent.ACTION_MOVE:
// 移动悬浮窗
int dx = (int) (event.getRawX() - lastX);
int dy = (int) (event.getRawY() - lastY);
lastX = (int) event.getRawX();
lastY = (int) event.getRawY();
catLayoutParams.x += dx;
catLayoutParams.y += dy;
// 限制悬浮窗的位置不超出屏幕
if (catLayoutParams.x < 0) {
catLayoutParams.x = 0;
}
if (catLayoutParams.x > screenWidth - catWidth) {
catLayoutParams.x = screenWidth - catWidth;
}
if (catLayoutParams.y < 0) {
catLayoutParams.y = 0;
}
if (catLayoutParams.y > screenHeight - catWidth - statusBarHeight) {
catLayoutParams.y = screenHeight - catWidth - statusBarHeight;
}
windowManager.updateViewLayout(view, catLayoutParams);
break;
case MotionEvent.ACTION_UP:
// 根据悬浮窗位置展开或收回按钮
if (isExpanded) {
retractButtons();
} else {
expandButtons();
}
break;
}
return true;
}
// 展开按钮
private void expandButtons() {
aView.setVisibility(View.VISIBLE);
bView.setVisibility(View.VISIBLE);
cView.setVisibility(View.VISIBLE);
// 计算展开后三个按钮的位置
int catX = catLayoutParams.x + catWidth / 2;
int catY = catLayoutParams.y + catWidth / 2 - statusBarHeight;
int aX = (int) (catX - catWidth * 1.5);
int aY = (int) (catY - catWidth * 0.5);
int bX = catX - catWidth / 2;
int bY = catY - catWidth * 2;
int cX = (int) (catX + catWidth * 0.5);
int cY = (int) (catY - catWidth * 0.5);
// 设置展开后三个按钮的位置
aLayoutParams.x = aX;
aLayoutParams.y = aY;
bLayoutParams.x = bX;
bLayoutParams.y = bY;
cLayoutParams.x = cX;
cLayoutParams.y = cY;
windowManager.updateViewLayout(aView, aLayoutParams);
windowManager.updateViewLayout(bView, bLayoutParams);
windowManager.updateViewLayout(cView, cLayoutParams);
isExpanded = true;
}
// 收回按钮
private void retractButtons() {
aView.setVisibility(View.GONE);
bView.setVisibility(View.GONE);
cView.setVisibility(View.GONE);
isExpanded = false;
}
// 获取状态栏高度
private int getStatusBarHeight() {
int statusBarHeight = 0;
int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
if (resourceId > 0) {
statusBarHeight = getResources().getDimensionPixelSize(resourceId);
}
return statusBarHeight;
}
}
```
其中,上述代码实现了一个悬浮窗图片按钮,当点击按钮时,会展开三个 image 按钮,分别为a、b、c,展开后的三个按钮会自动围绕图片按钮旋转展开,并且当悬浮窗靠近屏幕边缘时,a、b、c三个 image 按钮会自动吸附到屏幕边缘,收回时同理。
阅读全文