Merge pull request #3075 from harshlele/better-popup-resizing

Better popup resizing with pinch gestures
This commit is contained in:
Stypox 2020-04-10 22:26:59 +02:00 committed by GitHub
commit feab633e60
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 54 additions and 22 deletions

View File

@ -41,6 +41,7 @@ import android.view.GestureDetector;
import android.view.Gravity; import android.view.Gravity;
import android.view.MotionEvent; import android.view.MotionEvent;
import android.view.View; import android.view.View;
import android.view.ViewConfiguration;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.view.WindowManager; import android.view.WindowManager;
import android.view.animation.AnticipateInterpolator; import android.view.animation.AnticipateInterpolator;
@ -1010,6 +1011,14 @@ public final class PopupVideoPlayer extends Service {
private boolean isMoving; private boolean isMoving;
private boolean isResizing; private boolean isResizing;
//initial co-ordinates and distance between fingers
private double initPointerDistance = -1;
private float initFirstPointerX = -1;
private float initFirstPointerY = -1;
private float initSecPointerX = -1;
private float initSecPointerY = -1;
@Override @Override
public boolean onDoubleTap(final MotionEvent e) { public boolean onDoubleTap(final MotionEvent e) {
if (DEBUG) { if (DEBUG) {
@ -1201,6 +1210,16 @@ public final class PopupVideoPlayer extends Service {
playerImpl.hideControls(0, 0); playerImpl.hideControls(0, 0);
animateView(playerImpl.getCurrentDisplaySeek(), false, 0, 0); animateView(playerImpl.getCurrentDisplaySeek(), false, 0, 0);
animateView(playerImpl.getResizingIndicator(), true, 200, 0); animateView(playerImpl.getResizingIndicator(), true, 200, 0);
//record co-ordinates of fingers
initFirstPointerX = event.getX(0);
initFirstPointerY = event.getY(0);
initSecPointerX = event.getX(1);
initSecPointerY = event.getY(1);
//record distance between fingers
initPointerDistance = Math.hypot(initFirstPointerX - initSecPointerX,
initFirstPointerY - initSecPointerY);
isResizing = true; isResizing = true;
} }
@ -1224,6 +1243,13 @@ public final class PopupVideoPlayer extends Service {
if (isResizing) { if (isResizing) {
isResizing = false; isResizing = false;
initPointerDistance = -1;
initFirstPointerX = -1;
initFirstPointerY = -1;
initSecPointerX = -1;
initSecPointerY = -1;
animateView(playerImpl.getResizingIndicator(), false, 100, 0); animateView(playerImpl.getResizingIndicator(), false, 100, 0);
playerImpl.changeState(playerImpl.getCurrentState()); playerImpl.changeState(playerImpl.getCurrentState());
} }
@ -1238,30 +1264,36 @@ public final class PopupVideoPlayer extends Service {
} }
private boolean handleMultiDrag(final MotionEvent event) { private boolean handleMultiDrag(final MotionEvent event) {
if (event.getPointerCount() != 2) { if (initPointerDistance != -1 && event.getPointerCount() == 2) {
return false; // get the movements of the fingers
} double firstPointerMove = Math.hypot(event.getX(0) - initFirstPointerX,
event.getY(0) - initFirstPointerY);
double secPointerMove = Math.hypot(event.getX(1) - initSecPointerX,
event.getY(1) - initSecPointerY);
final float firstPointerX = event.getX(0); // minimum threshold beyond which pinch gesture will work
final float secondPointerX = event.getX(1); int minimumMove = ViewConfiguration.get(PopupVideoPlayer.this).getScaledTouchSlop();
final float diff = Math.abs(firstPointerX - secondPointerX); if (Math.max(firstPointerMove, secPointerMove) > minimumMove) {
if (firstPointerX > secondPointerX) { // calculate current distance between the pointers
// second pointer is the anchor (the leftmost pointer) double currentPointerDistance =
popupLayoutParams.x = (int) (event.getRawX() - diff); Math.hypot(event.getX(0) - event.getX(1),
} else { event.getY(0) - event.getY(1));
// first pointer is the anchor
popupLayoutParams.x = (int) event.getRawX(); // change co-ordinates of popup so the center stays at the same position
} double newWidth = (popupWidth * currentPointerDistance / initPointerDistance);
initPointerDistance = currentPointerDistance;
popupLayoutParams.x += (popupWidth - newWidth) / 2;
checkPopupPositionBounds(); checkPopupPositionBounds();
updateScreenSize(); updateScreenSize();
final int width = (int) Math.min(screenWidth, diff); updatePopupSize((int) Math.min(screenWidth, newWidth), -1);
updatePopupSize(width, -1);
return true; return true;
} }
}
return false;
}
/*////////////////////////////////////////////////////////////////////////// /*//////////////////////////////////////////////////////////////////////////
// Utils // Utils