-
Notifications
You must be signed in to change notification settings - Fork 380
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Can not to move, when has Button onClick. #29
Comments
Hope this is not too late. private static final int MAX_CLICK_DURATION = 600;
private static final int MAX_CLICK_DISTANCE = 5;
private long pressStartTime;
private float pressedX;
private float pressedY;
private boolean stayedWithinClickDistance;
someView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
onTouchHandleMove(SomeWindow.DEFAULT_ID, getWindow(SomeWindow.DEFAULT_ID), view, motionEvent);
switch (motionEvent.getAction()) {
case MotionEvent.ACTION_DOWN: {
pressStartTime = System.currentTimeMillis();
pressedX = motionEvent.getX();
pressedY = motionEvent.getY();
stayedWithinClickDistance = true;
break;
}
case MotionEvent.ACTION_MOVE: {
if (stayedWithinClickDistance && distance(pressedX, pressedY, motionEvent.getX(), motionEvent.getY()) > MAX_CLICK_DISTANCE) {
stayedWithinClickDistance = false;
}
break;
}
case MotionEvent.ACTION_UP: {
long pressDuration = System.currentTimeMillis() - pressStartTime;
if (pressDuration < MAX_CLICK_DURATION && stayedWithinClickDistance) {
// Click event has occurred
}
}
}
return true;
}
});
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 / getResources().getDisplayMetrics().density;
}
|
Although the answer a little late, but still thank you for your answer, thank you! |
Although the answer a little late, but still thank you for your answer,
thank you!
…On Tue, May 16, 2017 at 2:16 PM, Pengcheng Yin ***@***.***> wrote:
Hope this is not too late.
The window and the view (button) you try to add click listener are not
exactly the same thing. What you need to do is the pass the touch event
from button to the floating window. You may also want to detect the click
from touch event instead of using click listener. Check the code below.
private static final int MAX_CLICK_DURATION = 600;
private static final int MAX_CLICK_DISTANCE = 5;
private long pressStartTime;
private float pressedX;
private float pressedY;
private boolean stayedWithinClickDistance;
someView.setOnTouchListener(new View.OnTouchListener() {
@OverRide
public boolean onTouch(View view, MotionEvent motionEvent) {
onTouchHandleMove(SomeWindow.DEFAULT_ID, getWindow(SomeWindow.DEFAULT_ID), view, motionEvent);
switch (motionEvent.getAction()) {
case MotionEvent.ACTION_DOWN: {
pressStartTime = System.currentTimeMillis();
pressedX = motionEvent.getX();
pressedY = motionEvent.getY();
stayedWithinClickDistance = true;
break;
}
case MotionEvent.ACTION_MOVE: {
if (stayedWithinClickDistance && distance(pressedX, pressedY, motionEvent.getX(), motionEvent.getY()) > MAX_CLICK_DISTANCE) {
stayedWithinClickDistance = false;
}
break;
}
case MotionEvent.ACTION_UP: {
long pressDuration = System.currentTimeMillis() - pressStartTime;
if (pressDuration < MAX_CLICK_DURATION && stayedWithinClickDistance) {
// Click event has occurred
}
}
}
return true;
}
});
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 / getResources().getDisplayMetrics().density;
}
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#29 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/ACszisj7XvwXHQZUdo23bDuZho6E00-Iks5r6T9VgaJpZM4EEOgB>
.
|
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
When has Button onClick, can not to move.
The text was updated successfully, but these errors were encountered: