Skip to content
This repository has been archived by the owner on Oct 8, 2021. It is now read-only.

Commit

Permalink
introduce OnPathClickListener
Browse files Browse the repository at this point in the history
  • Loading branch information
tarek360 committed Aug 14, 2017
1 parent a1598df commit 6d3f904
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 6 deletions.
4 changes: 2 additions & 2 deletions README.md
Expand Up @@ -117,7 +117,7 @@ Add the following dependency to your module `build.gradle` file:
```gradle
dependencies {
...
compile 'com.github.tarek360.RichPath:animator:0.0.8'
compile 'com.github.tarek360.RichPath:animator:0.0.9'
}
```

Expand Down Expand Up @@ -154,7 +154,7 @@ RichPathAnimator

## TODO

- Clickable path
- ~Clickable path~ (Done)
- Support clip-path
- Path animation (animate a RichPath on a path)
- Reverse animation
Expand Down
Expand Up @@ -122,7 +122,7 @@ public AnimationBuilder startDelaySet(long startDelay) {
public AnimationBuilder interpolator(Interpolator interpolator) {
this.interpolator = interpolator;
for (ValueAnimator animator : animators) {
animator.setDuration(duration);
animator.setInterpolator(interpolator);
}
return this;
}
Expand Down
14 changes: 14 additions & 0 deletions richpath/src/main/java/com/richpath/RichPath.java
Expand Up @@ -65,6 +65,8 @@ public class RichPath extends Path {
private PathDataNode[] pathDataNodes;
private List<Matrix> matrices;

private OnPathClickListener onPathClickListener;

public RichPath(String pathData) {
this(PathParser.createPathFromPathData(pathData));
}
Expand Down Expand Up @@ -491,4 +493,16 @@ private int applyAlpha(int color, float alpha) {
color |= ((int) (alphaBytes * alpha)) << 24;
return color;
}

public void setOnPathClickListener(OnPathClickListener onPathClickListener) {
this.onPathClickListener = onPathClickListener;
}

public OnPathClickListener getOnPathClickListener() {
return onPathClickListener;
}

public interface OnPathClickListener {
void onClick();
}
}
31 changes: 30 additions & 1 deletion richpath/src/main/java/com/richpath/RichPathDrawable.java
Expand Up @@ -8,11 +8,15 @@
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.support.annotation.IntRange;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.view.MotionEventCompat;
import android.view.MotionEvent;

import com.richpath.listener.OnRichPathUpdatedListener;
import com.richpath.model.Vector;
import com.richpath.pathparser.PathParser;
import com.richpath.util.PathUtils;

/**
* Created by tarek on 6/29/17.
Expand Down Expand Up @@ -160,8 +164,33 @@ public void onPathUpdated() {
invalidateSelf();
}

boolean onTouchEvent(MotionEvent event) {

if (vector == null) return false;

int action = MotionEventCompat.getActionMasked(event);

switch (action) {
case MotionEvent.ACTION_UP:

for (int i = vector.paths.size() - 1; i >= 0; i--) {
RichPath richPath = vector.paths.get(i);
RichPath.OnPathClickListener onPathClickListener = richPath.getOnPathClickListener();
if (onPathClickListener != null) {
if (PathUtils.isTouched(richPath, event.getX(), event.getY())) {
onPathClickListener.onClick();
return true;
}
}
}
break;
}

return true;
}

@Override
public void draw(Canvas canvas) {
public void draw(@NonNull Canvas canvas) {

if (vector == null || vector.paths.size() < 0) return;

Expand Down
12 changes: 12 additions & 0 deletions richpath/src/main/java/com/richpath/RichPathView.java
Expand Up @@ -7,7 +7,9 @@
import android.support.annotation.DrawableRes;
import android.support.annotation.IntRange;
import android.support.annotation.Nullable;
import android.support.v4.view.MotionEventCompat;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;

Expand Down Expand Up @@ -192,4 +194,14 @@ public void addPath(Path path) {
}
}

@Override
public boolean onTouchEvent(MotionEvent event) {
int action = MotionEventCompat.getActionMasked(event);
switch (action) {
case MotionEvent.ACTION_UP:
performClick();
break;
}
return richPathDrawable.onTouchEvent(event);
}
}
19 changes: 17 additions & 2 deletions richpath/src/main/java/com/richpath/util/PathUtils.java
Expand Up @@ -3,9 +3,9 @@
import android.graphics.Matrix;
import android.graphics.Path;
import android.graphics.RectF;
import android.graphics.Region;

import com.richpath.pathparser.PathDataNode;
import com.richpath.pathparser.PathParser;

/**
* Created by tarek on 6/29/17.
Expand Down Expand Up @@ -107,4 +107,19 @@ public static void setPathDataNodes(Path path, PathDataNode[] pathDataNodes) {
path.reset();
PathDataNode.nodesToPath(pathDataNodes, path);
}
}

public static boolean isTouched(Path path, float x, float y) {
Region region = new Region();
RectF rectF = new RectF();
path.computeBounds(rectF, true);
region.setPath(path,
new Region((int) rectF.left, (int) rectF.top, (int) rectF.right, (int) rectF.bottom));
int offset = 10;
return region.contains((int) x, (int) y)
|| region.contains((int) x + offset, (int) y + offset)
|| region.contains((int) x + offset, (int) y - offset)
|| region.contains((int) x - offset, (int) y - offset)
|| region.contains((int) x - offset, (int) y + offset);
}

}

0 comments on commit 6d3f904

Please sign in to comment.