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

Commit

Permalink
migrate from View to ImageView to use the vector as a drawable
Browse files Browse the repository at this point in the history
  • Loading branch information
tarek360 committed Jul 19, 2017
1 parent 693007d commit e8a1821
Show file tree
Hide file tree
Showing 6 changed files with 226 additions and 118 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ Add the following dependency to your module `build.gradle` file:
```gradle
dependencies {
...
compile 'com.github.tarek360.RichPath:animator:0.0.6'
compile 'com.github.tarek360.RichPath:animator:0.0.7'
}
```

Expand Down
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ android {
applicationId "com.pathanimator.sample"
minSdkVersion 11
targetSdkVersion 25
versionCode 1
versionName "1.0"
versionCode 3
versionName "0.0.5"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
vectorDrawables.useSupportLibrary = true
}
Expand Down
52 changes: 29 additions & 23 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,56 +22,62 @@

<com.richpath.RichPathView
android:id="@+id/ic_command"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:layout_width="64dp"
android:layout_height="64dp"
android:layout_margin="4dp"
android:padding="8dp"
app:vector="@drawable/ic_command" />

<com.richpath.RichPathView
android:id="@+id/ic_android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:layout_width="64dp"
android:layout_height="64dp"
android:layout_margin="4dp"
android:padding="6dp"
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:layout_width="64dp"
android:layout_height="64dp"
android:layout_margin="4dp"
android:padding="8dp"
android:onClick="animateAnimal"
app:vector="@drawable/animal" />
app:vector="@drawable/animal"/>

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

<com.richpath.RichPathView
android:id="@+id/ic_notifications"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:layout_width="64dp"
android:layout_height="64dp"
android:layout_margin="4dp"
android:padding="6dp"
android:onClick="animateNotification"
app:vector="@drawable/ic_notifications" />

<com.richpath.RichPathView
android:id="@+id/ic_playlist_add_check"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:layout_width="64dp"
android:layout_height="64dp"
android:layout_margin="4dp"
android:padding="10dp"
android:onClick="animatePlaylistAddCheck"
app:vector="@drawable/ic_playlist_add_check" />
app:vector="@drawable/ic_playlist_add_check"/>

<com.richpath.RichPathView
android:id="@+id/love_face"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:layout_width="64dp"
android:layout_height="64dp"
android:layout_margin="4dp"
android:padding="10dp"
android:onClick="animateLoveFace"
app:vector="@drawable/face_love" />

Expand Down
3 changes: 2 additions & 1 deletion richpath/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ dependencies {
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:25.3.1'
compile "com.android.support:support-v4:25.3.1"

testCompile 'junit:junit:4.12'

}
147 changes: 147 additions & 0 deletions richpath/src/main/java/com/richpath/RichPathDrawable.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
package com.richpath;

import android.graphics.Canvas;
import android.graphics.ColorFilter;
import android.graphics.Matrix;
import android.graphics.Path;
import android.graphics.PixelFormat;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.support.annotation.IntRange;
import android.support.annotation.Nullable;

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

/**
* Created by tarek on 6/29/17.
*/

class RichPathDrawable extends Drawable {

private Vector vector;
private int width;
private int height;

public RichPathDrawable(Vector vector) {
this.vector = vector;
listenToPathsUpdates();
}

@Override
protected void onBoundsChange(Rect bounds) {
super.onBoundsChange(bounds);
if (bounds.width() > 0 && bounds.height() > 0) {
width = bounds.width();
height = bounds.height();
mapPaths();

}
}

void mapPaths() {
if (vector == null) return;

float centerX = width / 2;
float centerY = height / 2;

Matrix matrix = new Matrix();

matrix.postTranslate(centerX - vector.getViewportWidth() / 2,
centerY - vector.getViewportHeight() / 2);

float widthRatio = width / vector.getViewportWidth();
float heightRatio = height / vector.getViewportHeight();

float ratio = Math.min(widthRatio, heightRatio);

matrix.postScale(ratio, ratio, centerX, centerY);

for (RichPath path : vector.paths) {
path.mapToMatrix(matrix);
path.scaleStrokeWidth(ratio);
}

}

@Nullable
public RichPath findRichPathByName(String name) {
if (vector == null) return null;

for (RichPath path : vector.paths) {
if (name.equals(path.getName())) {
return path;
}
}
return null;
}

public void listenToPathsUpdates() {

if (vector == null) return;

for (RichPath path : vector.paths) {

path.setOnRichPathUpdatedListener(new OnRichPathUpdatedListener() {
@Override
public void onPathUpdated() {
invalidateSelf();
}
});
}

}

public void addPath(String path) {
addPath(PathParser.createPathFromPathData(path));
}

public void addPath(Path path) {
if (path instanceof RichPath) {
addPath((RichPath) path);
} else {
addPath(new RichPath(path));
}
}

private void addPath(RichPath path) {

if (vector == null) return;

vector.paths.add(path);
path.setOnRichPathUpdatedListener(new OnRichPathUpdatedListener() {
@Override
public void onPathUpdated() {
invalidateSelf();
}
});
invalidateSelf();
}

@Override
public void draw(Canvas canvas) {

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

for (RichPath path : vector.paths) {
path.draw(canvas);
}
}

@Override
public void setAlpha(@IntRange(from = 0, to = 255) int alpha) {

}

@Override
public void setColorFilter(@Nullable ColorFilter colorFilter) {

}

@Override
public int getOpacity() {
return PixelFormat.TRANSLUCENT;
}

}
Loading

0 comments on commit e8a1821

Please sign in to comment.