I want to implement a floating window function in my app which will respond on user input:
1.When user click the floating window, my app will take a screenshot of the current display and save it to the /sdcard directory. During this process, the floating window should disappear or hide for a while so that the screenshot just contains all that is displayed besides the floating window itself. And if user double click the floating window (say click twice within 1000ms) the floating window will be destroyed and removed.
2.When user touch the floating window and move it around the screen, the floating window should update its position accordingly.
I've tried for quite a long time, and my code is listed below. The problem is that I can't get the floating window disappear in time, so every screenshot I took contains the floating window's view. What's more boring is that sometimes when I clicked the floating window twice, my app will just take screenshots twice which shouldn't.
Could anyone help me with this?
Ps. The class itself is a service that will be triggered once a certain button int the MainActivity is clicked.
THE CODE:
public class ServiceFloating extends Service {
private WindowManager windowManager;
private WindowManager.LayoutParams paramsF;
private ImageView floatIcon;
long lastPressTime;
private Boolean mHasDoubleClicked;
public RootUtil rootUtil;
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
floatIcon = new ImageView(this);
floatIcon.setImageResource(R.drawable.floating);
final WindowManager.LayoutParams 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.LEFT;
params.x = 0;
params.y = 100;
windowManager.addView(floatIcon, params);
try {
floatIcon.setOnTouchListener(new View.OnTouchListener() {
private WindowManager.LayoutParams paramsF = params;
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:
// Get current time in nano seconds.
long pressTime = System.currentTimeMillis();
// If double click...(click interval should be longer than 1000)
if (pressTime - lastPressTime <= 1000) {
ServiceFloating.this.stopSelf();
mHasDoubleClicked = true;
} else {
mHasDoubleClicked = false;
}
lastPressTime = pressTime;
initialX = paramsF.x;
initialY = paramsF.y;
initialTouchX = event.getRawX();
initialTouchY = event.getRawY();
break;
case MotionEvent.ACTION_UP:
break;
case MotionEvent.ACTION_MOVE:
paramsF.x = initialX + (int) (event.getRawX() - initialTouchX);
paramsF.y = initialY + (int) (event.getRawY() - initialTouchY);
windowManager.updateViewLayout(floatIcon, paramsF);
break;
}
return false;
}
});
} catch (Exception e) {
// TODO: handle exception
}
floatIcon.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
//initiatePopupWindow(floatIcon);
if (!mHasDoubleClicked) {
if (rootUtil == null) {
rootUtil = RootUtil.getInstance();
}
Vibrator vb = (Vibrator) getSystemService(VIBRATOR_SERVICE);
vb.vibrate(100);
String tempFileName = String.valueOf(System.currentTimeMillis());
rootUtil.execute("screencap -p /sdcard/" + tempFileName + ".png");
}
}
});
}
}