最近项目要求,需要实现悬浮按钮,参照IOS悬浮按钮功能,自定义一个悬浮组件。
package com.lenovo.bootsettings.view;
import android.content.Context;
import android.graphics.Canvas;
import android.util.DisplayMetrics;
import android.view.MotionEvent;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import com.lenovo.bootsettings.R;
import com.lenovo.bootsettings.tools.LenovoApplication;
public class MyFloatView extends ImageView {
private float mTouchStartX;
private float mTouchStartY;
private float x;
private float y;
private int screenWidth, screenHeight;
/**
* View的宽高
*/
private int width;
private int height;
private WindowManager wm = (WindowManager) getContext()
.getApplicationContext().getSystemService("window");
// 此wmParams变量为获取的全局变量,用以保存悬浮窗口的属性
private WindowManager.LayoutParams wmParams = ((LenovoApplication) getContext()
.getApplicationContext()).getMywmParams();
public MyFloatView(Context context) {
super(context);
DisplayMetrics outMetrics = new DisplayMetrics();
wm.getDefaultDisplay().getMetrics(outMetrics);
screenWidth = outMetrics.widthPixels;
screenHeight = outMetrics.heightPixels;
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
width = getWidth();
height = getHeight();
}
@Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
// 获取相对屏幕的坐标,即以屏幕左上角为原点
x = event.getRawX();
y = event.getRawY(); // 25是系统状态栏的高度
//Log.i("zhj", "currX" + x + "====currY" + y);
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN: // 捕获手指触摸按下动作
// 获取相对View的坐标,即以此View左上角为原点
clearAnimation();
mTouchStartX = event.getX();
mTouchStartY = event.getY();
break;
case MotionEvent.ACTION_MOVE://捕获手指触摸移动动作
updateViewPosition();
break;
case MotionEvent.ACTION_UP://捕获手指触摸离开动作
updateViewToBoundary(x, y);
mTouchStartX = mTouchStartY = 0;
break;
default:
break;
}
return true;
}
private void updateViewPosition() {
//更新浮动窗口位置参数
wmParams.x = (int) (x - mTouchStartX);
wmParams.y = (int) (y - mTouchStartY);
wm.updateViewLayout(this, wmParams);//刷新显示
}
public void updateViewToBoundary(float currentX, float currentY) {
//更新浮动窗口
int halfOfScreenWidth = screenWidth / 2;
if (currentY < height + 100) {
wmParams.x = (int) (x - mTouchStartX);
wmParams.y = 10;
} else if (currentY > screenHeight - 250) {
wmParams.x = (int) (x - mTouchStartX);
wmParams.y = screenHeight - height;
}else {
if (currentX > halfOfScreenWidth) {
wmParams.x = screenWidth - width - 10;
} else {
wmParams.x = 10;
}
wmParams.y = (int) (y - mTouchStartY);
}
wm.updateViewLayout(this, wmParams);//刷新显示
}
}
使用说明:
(PS:我是写在Services 中,开机自启动应用)
一、在Services 中调用
private WindowManager wm;
private WindowManager.LayoutParams wmParams;
private MyFloatView myFV;
private float DownX = 0, DownY = 0;
private float moveX = 0, moveY = 0;
private long currentMS = 0;
@SuppressLint("ClickableViewAccessibility")
private void createFloatView() {
myFV = new MyFloatView(getApplicationContext());
myFV.setImageResource(R.drawable.app_icon);
// 获取WindowManager
wm = (WindowManager) getApplicationContext().getSystemService("window");
// 设置LayoutParams(全局变量)相关参数
wmParams = ((LenovoApplication) getApplication()).getMywmParams();
wmParams.type = WindowManager.LayoutParams.TYPE_PHONE;// 设置window type
wmParams.format = PixelFormat.RGBA_8888;// 设置图片格式,效果为背景透明
// 设置Window flag
wmParams.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
| WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
wmParams.gravity = Gravity.TOP | Gravity.LEFT;
DisplayMetrics outMetrics = new DisplayMetrics();
wm.getDefaultDisplay().getMetrics(outMetrics);
wmParams.x = 0;
wmParams.y = (outMetrics.heightPixels / 2);
Log.d(TAG, "createFloatView: y = " + (outMetrics.heightPixels / 2));
// 设置悬浮窗口长宽数据
wmParams.width = 80;
wmParams.height = 80;
// 显示myFloatView图像
wm.addView(myFV, wmParams);
mHandler.sendEmptyMessage(0x002);
myFV.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
mHandler.sendEmptyMessage(0x001);
DownX = event.getX();//float DownX
DownY = event.getY();//float DownY
moveX = 0;
moveY = 0;
currentMS = System.currentTimeMillis();//long currentMS 获取系统时间
break;
case MotionEvent.ACTION_MOVE:
moveX += Math.abs(event.getX() - DownX);//X轴距离
moveY += Math.abs(event.getY() - DownY);//y轴距离
DownX = event.getX();
DownY = event.getY();
break;
case MotionEvent.ACTION_UP:
if (mHandler.hasMessages(0x002)) {
mHandler.removeMessages(0x002);
}
mHandler.sendEmptyMessageDelayed(0x002, 5000);
long moveTime = System.currentTimeMillis() - currentMS;//移动时间
if (moveTime > 200 && (moveX > 20 || moveY > 20)) {
myFV.updateViewToBoundary(event.getRawX() , event.getRawY() -25);
return true;
} else {
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
break;
}
return false;
}
});
}
二、清单文件中 添加权限
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
<uses-permission android:name="android.permission.SYSTEM_OVERLAY_WINDOW" />
三、在全局Appliction 中初始化窗口
private WindowManager.LayoutParams wmParams = new WindowManager.LayoutParams();
public WindowManager.LayoutParams getMywmParams() {
return wmParams;
}