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

Commit

Permalink
support path morphing
Browse files Browse the repository at this point in the history
  • Loading branch information
tarek360 committed Jul 14, 2017
1 parent bb3949c commit 1c267a2
Show file tree
Hide file tree
Showing 12 changed files with 177 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,15 @@ private AnimationBuilder color(String propertyName, int... colors) {
return this;
}

public AnimationBuilder pathData(String... pathData) {
for (final RichPath path : paths) {
ObjectAnimator objectAnimator =
ObjectAnimator.ofObject(path, "pathData", new PathEvaluator(), pathData);
applyAnimatorProperties(objectAnimator, path);
}
return this;
}

private void applyAnimatorProperties(ValueAnimator animator, final RichPath path) {

if (path == null) {
Expand Down
62 changes: 62 additions & 0 deletions animator/src/main/java/com/richpathanimator/PathEvaluator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package com.richpathanimator;

import android.animation.TypeEvaluator;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
* Created by tarek on 7/14/17.
*/

public class PathEvaluator implements TypeEvaluator<String> {

private String pathHolder;
private String startPath;
private String endPath;

private List<Float> startFloats = new ArrayList<>();
private List<Float> endFloats = new ArrayList<>();

@Override
public String evaluate(float fraction, String startPath, String endPath) {


if (pathHolder == null
|| !this.startPath.equals(startPath) || !this.endPath.equals(endPath)) {

this.pathHolder = endPath;
this.startPath = startPath;
this.endPath = endPath;

startFloats.clear();
endFloats.clear();

Pattern p = Pattern.compile("[-]?[0-9]*\\.?[0-9]+");

Matcher startMatcher = p.matcher(startPath);
Matcher endMatcher = p.matcher(endPath);

while (startMatcher.find() && endMatcher.find()) {
startFloats.add(Float.parseFloat(startMatcher.group()));
endFloats.add(Float.parseFloat(endMatcher.group()));
pathHolder = p.matcher(pathHolder).replaceFirst("#");
}
}

String evaluatedPath = pathHolder;

for (int i = 0; i < startFloats.size(); i++) {

float startFloat = startFloats.get(i);
float value = startFloat + fraction * (endFloats.get(i) - startFloat);

evaluatedPath = evaluatedPath.replaceFirst("#", String.valueOf(value));
}

return evaluatedPath;
}

}
26 changes: 26 additions & 0 deletions app/src/main/java/com/richpathanimator/sample/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public class MainActivity extends AppCompatActivity {
private RichPathView notificationsRichPathView;
private RichPathView playlistAddCheckRichPathView;
private RichPathView loveFaceRichPathView;
private RichPathView animalRichPathView;

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand All @@ -33,6 +34,7 @@ protected void onCreate(Bundle savedInstanceState) {
notificationsRichPathView = (RichPathView) findViewById(R.id.ic_notifications);
playlistAddCheckRichPathView = (RichPathView) findViewById(R.id.ic_playlist_add_check);
loveFaceRichPathView = (RichPathView) findViewById(R.id.love_face);
animalRichPathView = (RichPathView) findViewById(R.id.animal);

animateCommand();
}
Expand Down Expand Up @@ -122,6 +124,30 @@ public void onStop() {
.start();
}

public void animateAnimal(View view) {

String hippoPathData = getString(R.string.hippo_path);
String elephantPathData = getString(R.string.elephant_path);
String bullPathData = getString(R.string.bull_path);

final RichPath richPath = animalRichPathView.findRichPathByName("path");

RichPathAnimator
.animate(richPath)
.pathData(elephantPathData)
.duration(600)

.thenAnimate(richPath)
.pathData(bullPathData)
.duration(600)

.thenAnimate(richPath)
.pathData(hippoPathData)
.duration(600)

.start();
}

private boolean reverse = false;

public void animateArrowToSearch(View view) {
Expand Down
12 changes: 12 additions & 0 deletions app/src/main/res/drawable/animal.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<vector
xmlns:android="http://schemas.android.com/apk/res/android"
android:width="34.08dp"
android:height="24.08dp"
android:viewportWidth="409"
android:viewportHeight="280">
<path
android:name="path"
android:pathData="@string/hippo_path"
android:fillColor="#FFF7F7F7"/>
</vector>
13 changes: 13 additions & 0 deletions app/src/main/res/drawable/square.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="32"
android:viewportHeight="32">

<path
android:name="square"
android:fillColor="#FF2254"
android:pathData="M0,0 L32,0 L32,32 L0,32z" />

</vector>
8 changes: 8 additions & 0 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@
android:onClick="animateAndroid"
app:vector="@drawable/ic_android" />

<com.richpath.RichPathView
android:id="@+id/animal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:onClick="animateAnimal"
app:vector="@drawable/animal" />

<com.richpath.RichPathView
android:id="@+id/ic_arrow_search"
android:layout_width="wrap_content"
Expand Down
Loading

0 comments on commit 1c267a2

Please sign in to comment.