Skip to content

Commit

Permalink
Add ability to intercept expanding animation
Browse files Browse the repository at this point in the history
  • Loading branch information
hluhovskyi committed May 22, 2017
1 parent ba0adf3 commit 5b43226
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 5 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Button which is visible while user holds it. Main use case is controlling audio
Add library as dependency to your `build.gradle`.

```
compile 'com.dewarder:holdingbutton:0.0.7'
compile 'com.dewarder:holdingbutton:0.0.8'
```

## How to use
Expand Down
4 changes: 2 additions & 2 deletions holdingbutton/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ apply plugin: 'com.github.dcendents.android-maven'
apply plugin: 'com.jfrog.bintray'

ext {
artifactVersion = '0.0.7'
artifactVersion = '0.0.8'
artifactName = 'holdingbutton'
siteUrl = 'https://github.com/dewarder/HoldingButton'
gitUrl = 'https://github.com/dewarder/HoldingButton.git'
Expand Down Expand Up @@ -110,7 +110,7 @@ bintray {
vcsUrl = gitUrl
version {
name = artifactVersion
desc = 'Add ability to enable/disable HoldingButton'
desc = 'Add ability to intercept expanding animation'
released = new Date()
vcsTag = "$artifactVersion"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;
Expand All @@ -60,6 +61,7 @@ public class HoldingButtonLayout extends FrameLayout {
private boolean mIsCancel = false;
private boolean mIsExpanded = false;

private HoldingButtonTouchListener mTouchListener = new SimpleHoldingButtonTouchListener();
private final DrawableListener mDrawableListener = new DrawableListener();
private final List<HoldingButtonLayoutListener> mListeners = new ArrayList<>();

Expand Down Expand Up @@ -178,7 +180,7 @@ public boolean onInterceptTouchEvent(MotionEvent ev) {
final int action = ev.getActionMasked();
switch (action) {
case MotionEvent.ACTION_DOWN: {
return isButtonEnabled() && isViewTouched(mHoldingView, ev);
return isButtonEnabled() && shouldInterceptAnimation() && isViewTouched(mHoldingView, ev);
}
}

Expand All @@ -191,7 +193,7 @@ public boolean onTouchEvent(MotionEvent event) {

switch (action) {
case MotionEvent.ACTION_DOWN: {
if (isButtonEnabled() && isViewTouched(mHoldingView, event)) {
if (isButtonEnabled() && shouldInterceptAnimation() && isViewTouched(mHoldingView, event)) {
mHoldingView.getLocationInWindow(mHoldingViewLocation);
getLocationInWindow(mViewLocation);

Expand Down Expand Up @@ -284,6 +286,10 @@ protected ViewGroup getDecorView() {
return view;
}

public void setTouchListener(@NonNull HoldingButtonTouchListener listener) {
mTouchListener = listener;
}

public void addListener(HoldingButtonLayoutListener listener) {
mListeners.add(listener);
}
Expand All @@ -292,6 +298,10 @@ public void removeListener(HoldingButtonLayoutListener listener) {
mListeners.remove(listener);
}

private boolean shouldInterceptAnimation() {
return mTouchListener.onHoldingViewTouched();
}

private boolean isViewTouched(View view, MotionEvent event) {
view.getDrawingRect(mHoldingViewRect);
view.getLocationOnScreen(mHoldingViewLocation);
Expand Down Expand Up @@ -442,6 +452,14 @@ private void notifyOnOffsetChanged(float offset, boolean isCancel) {
}
}

private class SimpleHoldingButtonTouchListener implements HoldingButtonTouchListener {

@Override
public boolean onHoldingViewTouched() {
return true;
}
}

private class DrawableListener implements HoldingDrawableListener {

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.dewarder.holdinglibrary;

/**
* Interface is used to intercept touch events.
* Can be used for cases when permissions or something else are needed and expanding animation isn't needed
*/
public interface HoldingButtonTouchListener {

/**
* Indicates that holding view is touched.
* Method is called before any method of `HoldingButtonLayoutListener`.
* @return false - if expanding animation must be canceled, true - if animation must be proceeded
*/
boolean onHoldingViewTouched();
}

0 comments on commit 5b43226

Please sign in to comment.