如何达到这个效果:
点击浮动视图外的屏幕,浮动视图消失,点击事件将传递给Activity,例如空白只有一个按钮,指向按钮,浮动视图消失,按钮是
,可以在浮动视图消失后单击,或者单击Edittext,可以输入Edittext。请告诉我如何听取或拦截活动。或者给我另一种方法来解决它。
答案 0 :(得分:0)
首先为您正在使用的视图的根视图定义id。
private final String TAG = "MOVING POPUP";
private WindowManager.LayoutParams params;
private WindowManager.LayoutParams paramsF;
private View mView;
private boolean isViewVisible = false;
private WindowManager windowManager;
private long pressStartTime;
private float pressedX;
private float pressedY;
private boolean stayedWithinClickDistance;
/**
* Max allowed duration for a "click", in milliseconds.
*/
private static final int MAX_CLICK_DURATION = 1000;
/**
* Max allowed distance to move during a "click", in DP.
*/
private static final int MAX_CLICK_DISTANCE = 15;
private void createView()
{
windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
LayoutInflater inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mView = inflater.inflate(R.layout.dialog_book_text_selection_main, null, false);
View mdismiss = mView.findViewById(R.id.btnApply);
View llRoot = mView.findViewById(R.id.llRoot);
try
{
mView.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
removeView();
}
});
mdismiss.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
removeView();
}
});
llRoot.setOnTouchListener(new 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 = paramsF.x;
initialY = paramsF.y;
initialTouchX = event.getRawX();
initialTouchY = event.getRawY();
pressStartTime = System.currentTimeMillis();
pressedX = event.getX();
pressedY = event.getY();
stayedWithinClickDistance = true;
break;
case MotionEvent.ACTION_UP:
long pressDuration = System.currentTimeMillis() - pressStartTime;
if (pressDuration < MAX_CLICK_DURATION && stayedWithinClickDistance)
{
perform your on click event here
}
case MotionEvent.ACTION_POINTER_UP:
break;
case MotionEvent.ACTION_MOVE:
if (stayedWithinClickDistance && distance(pressedX, pressedY, event.getX(), event.getY()) > MAX_CLICK_DISTANCE) {
stayedWithinClickDistance = false;
}
paramsF.x = initialX + (int) (event.getRawX() - initialTouchX);
paramsF.y = initialY + (int) (event.getRawY() - initialTouchY);
windowManager.updateViewLayout(mView, paramsF);
break;
}
return true;
}
});
} catch (Exception e) {
e.printStackTrace();
Log.e(TAG, "ON TOUCH ERROR--->" + e.getMessage());
}
setViewPosition();
addView();
}
private float distance(float x1, float y1, float x2, float y2) {
float dx = x1 - x2;
float dy = y1 - y2;
float distanceInPx = (float) Math.sqrt(dx * dx + dy * dy);
return pxToDp(distanceInPx);
}
private float pxToDp(float px) {
return px / activity.getResources().getDisplayMetrics().density;
}
private void addView()
{
if (mView != null && !isViewVisible)
{
isViewVisible = true;
setViewPosition();
windowManager.addView(mView, params);
}
}
private void removeView()
{
try {
if (mView != null && isViewVisible)
{
windowManager.removeView(mView);
isViewVisible = false;
}
} catch (Exception e) {
e.printStackTrace();
}
}
private void setViewPosition()
{
params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_APPLICATION_ATTACHED_DIALOG,
WindowManager.LayoutParams.FLAG_FULLSCREEN,
PixelFormat.TRANSLUCENT);
params.gravity = Gravity.CENTER;
params.x = 0;
params.y = 0;
paramsF = params;
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_BACK)
{
if (isViewVisible)
{
removeView();
return true;
}
}
return super.onKeyDown(keyCode, event);
}