-
Notifications
You must be signed in to change notification settings - Fork 605
Support for orientation, useful three new listeners and improvements about compatibility with ExoPlayer #86
base: develop
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -76,6 +76,12 @@ public class DraggableView extends RelativeLayout { | |
private int marginBottom, marginRight; | ||
private int dragViewId, secondViewId; | ||
|
||
private CountDownTimer onMinimizeCountdownTimer; | ||
private CountDownTimer onMaximizeCountdownTimer; | ||
|
||
private boolean clickedToMaximize; | ||
private boolean clickedToMinimize; | ||
|
||
public DraggableView(Context context) { | ||
super(context); | ||
} | ||
|
@@ -243,7 +249,7 @@ public void setTopViewResize(boolean topViewResize) { | |
*/ | ||
public void maximize() { | ||
smoothSlideTo(SLIDE_TOP); | ||
notifyMaximizeToListener(); | ||
onMaximizeCountdownTimer.start(); | ||
} | ||
|
||
/** | ||
|
@@ -252,15 +258,15 @@ public void maximize() { | |
*/ | ||
public void minimize() { | ||
smoothSlideTo(SLIDE_BOTTOM); | ||
notifyMinimizeToListener(); | ||
onMinimizeCountdownTimer.start(); | ||
} | ||
|
||
/** | ||
* Close the custom view applying an animation to close the view to the right side of the screen. | ||
*/ | ||
public void closeToRight() { | ||
if (viewDragHelper.smoothSlideViewTo(dragView, transformer.getOriginalWidth(), | ||
getHeight() - transformer.getMinHeightPlusMargin())) { | ||
getHeight() - transformer.getMinHeightPlusMargin())) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. All this extra whitespace you have here and in the rest of the file will be gone if you format the project using the correct checkstyle. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll be formatting the code with the correct checkstyle along with the other changes. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Cool, thanks! |
||
ViewCompat.postInvalidateOnAnimation(this); | ||
notifyCloseToRightListener(); | ||
} | ||
|
@@ -271,7 +277,7 @@ public void closeToRight() { | |
*/ | ||
public void closeToLeft() { | ||
if (viewDragHelper.smoothSlideViewTo(dragView, -transformer.getOriginalWidth(), | ||
getHeight() - transformer.getMinHeightPlusMargin())) { | ||
getHeight() - transformer.getMinHeightPlusMargin())) { | ||
ViewCompat.postInvalidateOnAnimation(this); | ||
notifyCloseToLeftListener(); | ||
} | ||
|
@@ -359,14 +365,17 @@ public boolean isClosed() { | |
* @return true if the touch event is realized over the drag or second view. | ||
*/ | ||
@Override public boolean onTouchEvent(MotionEvent ev) { | ||
listener.onTouchListener(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It could be useful to pass the event to the listener method. |
||
int actionMasked = MotionEventCompat.getActionMasked(ev); | ||
if ((actionMasked & MotionEventCompat.ACTION_MASK) == MotionEvent.ACTION_DOWN) { | ||
activePointerId = MotionEventCompat.getPointerId(ev, actionMasked); | ||
} | ||
if (activePointerId == INVALID_POINTER) { | ||
return false; | ||
} | ||
viewDragHelper.processTouchEvent(ev); | ||
if(getContext().getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT){ | ||
viewDragHelper.processTouchEvent(ev); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do you process the touch event just if the configuration is portrait? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In case of usage a video player such as ExoPlayer, this processes the drag event when you try to hover the player controls, breaks the user experience about controlling the player. A control has been added for that purpose. Could be optional for people who doesn't have hover for player controls and aims to drag even on Landscape. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Definitively this should be configurable. You should add a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @pedrovgs I thought it is better to give all the options (all, landscape, portrait) for the user, in case of different needs I'll be adding a flag to support all of those to trigger touch events. You can check it out after the commit with new implementations. |
||
if (isClosed()) { | ||
return false; | ||
} | ||
|
@@ -390,8 +399,12 @@ private void analyzeTouchToMaximizeIfNeeded(MotionEvent ev, boolean isDragViewHi | |
float clickOffset = ev.getX() - lastTouchActionDownXPosition; | ||
if (shouldMaximizeOnClick(ev, clickOffset, isDragViewHit)) { | ||
if (isMinimized() && isClickToMaximizeEnabled()) { | ||
listener.clickedToMaximize(); | ||
clickedToMaximize = true; | ||
maximize(); | ||
} else if (isMaximized() && isClickToMinimizeEnabled()) { | ||
listener.clickedToMinimize(); | ||
clickedToMinimize = true; | ||
minimize(); | ||
} | ||
} | ||
|
@@ -403,8 +416,8 @@ private void analyzeTouchToMaximizeIfNeeded(MotionEvent ev, boolean isDragViewHi | |
|
||
public boolean shouldMaximizeOnClick(MotionEvent ev, float deltaX, boolean isDragViewHit) { | ||
return (Math.abs(deltaX) < MIN_SLIDING_DISTANCE_ON_CLICK) | ||
&& ev.getAction() != MotionEvent.ACTION_MOVE | ||
&& isDragViewHit; | ||
&& ev.getAction() != MotionEvent.ACTION_MOVE | ||
&& isDragViewHit; | ||
} | ||
|
||
/** | ||
|
@@ -419,7 +432,7 @@ public boolean shouldMaximizeOnClick(MotionEvent ev, float deltaX, boolean isDra | |
*/ | ||
private MotionEvent cloneMotionEventWithAction(MotionEvent event, int action) { | ||
return MotionEvent.obtain(event.getDownTime(), event.getEventTime(), action, event.getX(), | ||
event.getY(), event.getMetaState()); | ||
event.getY(), event.getMetaState()); | ||
} | ||
|
||
/** | ||
|
@@ -616,9 +629,9 @@ private boolean isViewHit(View view, int x, int y) { | |
int screenX = parentLocation[0] + x; | ||
int screenY = parentLocation[1] + y; | ||
return screenX >= viewLocation[0] | ||
&& screenX < viewLocation[0] + view.getWidth() | ||
&& screenY >= viewLocation[1] | ||
&& screenY < viewLocation[1] + view.getHeight(); | ||
&& screenX < viewLocation[0] + view.getWidth() | ||
&& screenY >= viewLocation[1] | ||
&& screenY < viewLocation[1] + view.getHeight(); | ||
} | ||
|
||
/** | ||
|
@@ -659,14 +672,14 @@ private void initializeTransformer() { | |
private void initializeAttributes(AttributeSet attrs) { | ||
TypedArray attributes = getContext().obtainStyledAttributes(attrs, R.styleable.draggable_view); | ||
this.enableHorizontalAlphaEffect = | ||
attributes.getBoolean(R.styleable.draggable_view_enable_minimized_horizontal_alpha_effect, | ||
DEFAULT_ENABLE_HORIZONTAL_ALPHA_EFFECT); | ||
attributes.getBoolean(R.styleable.draggable_view_enable_minimized_horizontal_alpha_effect, | ||
DEFAULT_ENABLE_HORIZONTAL_ALPHA_EFFECT); | ||
this.enableClickToMaximize = | ||
attributes.getBoolean(R.styleable.draggable_view_enable_click_to_maximize_view, | ||
DEFAULT_ENABLE_CLICK_TO_MAXIMIZE); | ||
attributes.getBoolean(R.styleable.draggable_view_enable_click_to_maximize_view, | ||
DEFAULT_ENABLE_CLICK_TO_MAXIMIZE); | ||
this.enableClickToMinimize = | ||
attributes.getBoolean(R.styleable.draggable_view_enable_click_to_minimize_view, | ||
DEFAULT_ENABLE_CLICK_TO_MINIMIZE); | ||
attributes.getBoolean(R.styleable.draggable_view_enable_click_to_minimize_view, | ||
DEFAULT_ENABLE_CLICK_TO_MINIMIZE); | ||
this.topViewResize = | ||
attributes.getBoolean(R.styleable.draggable_view_top_view_resize, DEFAULT_TOP_VIEW_RESIZE); | ||
this.topViewHeight = attributes.getDimensionPixelSize(R.styleable.draggable_view_top_view_height, | ||
|
@@ -694,6 +707,7 @@ private void initializeAttributes(AttributeSet attrs) { | |
* @return true if the view is slided. | ||
*/ | ||
private boolean smoothSlideTo(float slideOffset) { | ||
listener.smoothSlide(); | ||
final int topBound = getPaddingTop(); | ||
int x = (int) (slideOffset * (getWidth() - transformer.getMinWidthPlusMarginRight())); | ||
int y = (int) (topBound + slideOffset * getVerticalDragRange()); | ||
|
@@ -784,4 +798,73 @@ private void notifyCloseToLeftListener() { | |
public int getDraggedViewHeightPlusMarginTop() { | ||
return transformer.getMinHeightPlusMargin(); | ||
} | ||
|
||
public void initializeTimers(){ | ||
|
||
onMaximizeCountdownTimer = new CountDownTimer(2000, 10) { | ||
@Override | ||
public void onTick(long l) { | ||
|
||
if (getSecondViewAlpha() > 0.95) { | ||
notifyMaximizeToListener(); | ||
new Handler().post(new Runnable() { | ||
@Override | ||
public void run() { | ||
onMaximizeCountdownTimer.cancel(); | ||
} | ||
}); | ||
} | ||
} | ||
|
||
@Override | ||
public void onFinish() { | ||
|
||
} | ||
}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @rencsaridogan this is not the way to go to implement the listener mechanism. You are using a polling mechanism to notify your listener and depending on the device this approach will notify at the second X or X+1. You should notify your listener close to the method where the alpha is modified and remove the usage of this count down timer. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You are right. Since the alpha is modified constantly in that case, removing the extra timers and handling the notifiers within the alpha change sounds logical. I'll be implementing and testing that version. Thanks! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @rencsaridogan if you add an example to the project using exo player could be great. We could use it to improve the library usage combined with this component. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @pedrovgs While I'm coding the changes through, I will add a sample for ExoPlayer along with others for the purpose of improving usage of both components together. I believe since industry is focused on using ExoPlayer based players, so this could be great to give it a full support! |
||
|
||
onMinimizeCountdownTimer = new CountDownTimer(2000,10) { | ||
@Override | ||
public void onTick(long l) { | ||
if (getSecondViewAlpha() < 0.15) { | ||
notifyMinimizeToListener(); | ||
new Handler().post(new Runnable() { | ||
@Override | ||
public void run() { | ||
onMinimizeCountdownTimer.cancel(); | ||
} | ||
}); | ||
} | ||
} | ||
|
||
@Override | ||
public void onFinish() { | ||
|
||
} | ||
}; | ||
} | ||
|
||
public float getSecondViewAlpha(){ | ||
return secondView.getAlpha(); | ||
} | ||
|
||
public boolean isClickedMaximize(){ | ||
return clickedToMaximize; | ||
} | ||
|
||
public void setClickedtoMaximize(){ | ||
clickedToMaximize = false; | ||
} | ||
|
||
public boolean isClickedMinimize(){ | ||
return clickedToMinimize; | ||
} | ||
|
||
public void minimizeButtonClicked(){ | ||
clickedToMinimize = true; | ||
} | ||
|
||
public void setClickedtoMinimize(){ | ||
clickedToMinimize = false; | ||
} | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should change the naming of the methods you've added to match with the naming of the project. If you review the methods already implemented all start using
on
as prefix. Based on that, we should rename these last three methods to:There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Working on it.