diff --git a/README.md b/README.md index 9df4ca0..edbfd0c 100644 --- a/README.md +++ b/README.md @@ -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' } ``` @@ -154,7 +154,7 @@ RichPathAnimator ## TODO -- Clickable path +- ~Clickable path~ (Done) - Support clip-path - Path animation (animate a RichPath on a path) - Reverse animation diff --git a/animator/src/main/java/com/richpathanimator/AnimationBuilder.java b/animator/src/main/java/com/richpathanimator/AnimationBuilder.java index 6feec15..c53d47d 100644 --- a/animator/src/main/java/com/richpathanimator/AnimationBuilder.java +++ b/animator/src/main/java/com/richpathanimator/AnimationBuilder.java @@ -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; } diff --git a/richpath/src/main/java/com/richpath/RichPath.java b/richpath/src/main/java/com/richpath/RichPath.java index f4f5956..daebe56 100644 --- a/richpath/src/main/java/com/richpath/RichPath.java +++ b/richpath/src/main/java/com/richpath/RichPath.java @@ -65,6 +65,8 @@ public class RichPath extends Path { private PathDataNode[] pathDataNodes; private List matrices; + private OnPathClickListener onPathClickListener; + public RichPath(String pathData) { this(PathParser.createPathFromPathData(pathData)); } @@ -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(); + } } diff --git a/richpath/src/main/java/com/richpath/RichPathDrawable.java b/richpath/src/main/java/com/richpath/RichPathDrawable.java index c994fcb..2e101ab 100644 --- a/richpath/src/main/java/com/richpath/RichPathDrawable.java +++ b/richpath/src/main/java/com/richpath/RichPathDrawable.java @@ -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. @@ -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; diff --git a/richpath/src/main/java/com/richpath/RichPathView.java b/richpath/src/main/java/com/richpath/RichPathView.java index 0daf7c4..5f3638d 100644 --- a/richpath/src/main/java/com/richpath/RichPathView.java +++ b/richpath/src/main/java/com/richpath/RichPathView.java @@ -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; @@ -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); + } } diff --git a/richpath/src/main/java/com/richpath/util/PathUtils.java b/richpath/src/main/java/com/richpath/util/PathUtils.java index ddd24a4..ad1d7bd 100644 --- a/richpath/src/main/java/com/richpath/util/PathUtils.java +++ b/richpath/src/main/java/com/richpath/util/PathUtils.java @@ -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. @@ -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); + } + +} \ No newline at end of file