diff --git a/README.md b/README.md index 1d62a73..cb1f477 100644 --- a/README.md +++ b/README.md @@ -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' } ``` diff --git a/app/build.gradle b/app/build.gradle index 080f64d..008367f 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -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 } diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 969ca27..4d8867b 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -22,56 +22,62 @@ + app:vector="@drawable/animal"/> + app:vector="@drawable/ic_playlist_add_check"/> diff --git a/richpath/build.gradle b/richpath/build.gradle index 837ce4f..c16df51 100644 --- a/richpath/build.gradle +++ b/richpath/build.gradle @@ -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' } diff --git a/richpath/src/main/java/com/richpath/RichPathDrawable.java b/richpath/src/main/java/com/richpath/RichPathDrawable.java new file mode 100644 index 0000000..a2bc38c --- /dev/null +++ b/richpath/src/main/java/com/richpath/RichPathDrawable.java @@ -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; + } + +} diff --git a/richpath/src/main/java/com/richpath/RichPathView.java b/richpath/src/main/java/com/richpath/RichPathView.java index d36f864..94c3144 100644 --- a/richpath/src/main/java/com/richpath/RichPathView.java +++ b/richpath/src/main/java/com/richpath/RichPathView.java @@ -3,18 +3,15 @@ import android.content.Context; import android.content.res.TypedArray; import android.content.res.XmlResourceParser; -import android.graphics.Canvas; -import android.graphics.Matrix; import android.graphics.Path; import android.support.annotation.DrawableRes; import android.support.annotation.Nullable; import android.util.AttributeSet; import android.view.View; +import android.widget.ImageView; -import com.richpath.listener.OnRichPathUpdatedListener; import com.richpath.model.Vector; import com.richpath.pathparser.PathParser; -import com.richpath.util.Utils; import com.richpath.util.XmlParser; import org.xmlpull.v1.XmlPullParserException; @@ -25,11 +22,10 @@ * Created by tarek on 6/29/17. */ -public class RichPathView extends View { +public class RichPathView extends ImageView { private Vector vector; - private int width; - private int height; + private RichPathDrawable richPathDrawable; public RichPathView(Context context) { this(context, null); @@ -79,9 +75,13 @@ public void setVectorDrawable(@DrawableRes int resId) { @SuppressWarnings("ResourceType") XmlResourceParser xpp = getContext().getResources().getXml(resId); vector = new Vector(); + try { XmlParser.parseVector(vector, xpp, getContext()); - listenToUpdatingPath(); + + richPathDrawable = new RichPathDrawable(vector); + setImageDrawable(richPathDrawable); + } catch (IOException e) { e.printStackTrace(); } catch (XmlPullParserException e) { @@ -91,108 +91,62 @@ public void setVectorDrawable(@DrawableRes int resId) { @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { - super.onMeasure(widthMeasureSpec, heightMeasureSpec); - - if (vector == null) return; - - setMeasuredDimension((int) vector.getWidth(), - (int) vector.getHeight()); - } - - @Override - protected void onSizeChanged(int w, int h, int oldw, int oldh) { - super.onSizeChanged(w, h, oldw, oldh); - if (w > 0 && h > 0) { - width = w; - height = h; - mapPaths(); - } - } - void mapPaths() { if (vector == null) return; - float centerX = width / 2; - float centerY = height / 2; - - Matrix matrix = new Matrix(); + int desiredWidth = (int) vector.getWidth(); + int desiredHeight = (int) vector.getHeight(); - matrix.postTranslate(centerX - vector.getViewportWidth() / 2, - centerY - vector.getViewportHeight() / 2); + int widthMode = MeasureSpec.getMode(widthMeasureSpec); + int widthSize = MeasureSpec.getSize(widthMeasureSpec); + int heightMode = MeasureSpec.getMode(heightMeasureSpec); + int heightSize = MeasureSpec.getSize(heightMeasureSpec); - float widthRatio = width / vector.getViewportWidth(); - float heightRatio = height / vector.getViewportHeight(); - float ratio = Math.min(widthRatio, heightRatio); - - matrix.postScale(ratio, ratio, centerX, centerY); + //Measure Width + int width; + if (widthMode == MeasureSpec.EXACTLY) { + //Must be this size + width = widthSize; + } else if (widthMode == MeasureSpec.AT_MOST) { + //Can't be bigger than... + width = Math.min(desiredWidth, widthSize); + } else { + //Be whatever you want + width = desiredWidth; + } - for (RichPath path : vector.paths) { - path.mapToMatrix(matrix); - path.scaleStrokeWidth(ratio); + //Measure Height + int height; + if (heightMode == MeasureSpec.EXACTLY) { + //Must be this size + height = heightSize; + } else if (heightMode == MeasureSpec.AT_MOST) { + //Can't be bigger than... + height = Math.min(desiredHeight, heightSize); + } else { + //Be whatever you want + height = desiredHeight; } + //MUST CALL THIS + setMeasuredDimension(width, height); } @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 listenToUpdatingPath() { - if (vector == null) return; - - for (RichPath path : vector.paths) { - path.setOnRichPathUpdatedListener(new OnRichPathUpdatedListener() { - @Override - public void onPathUpdated() { - invalidate(); - } - }); - } - + return richPathDrawable == null ? null : richPathDrawable.findRichPathByName(name); } 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)); + if (richPathDrawable != null) { + richPathDrawable.addPath(PathParser.createPathFromPathData(path)); } } - private void addPath(RichPath path) { - - if (vector == null) return; - vector.paths.add(path); - path.setOnRichPathUpdatedListener(new OnRichPathUpdatedListener() { - @Override - public void onPathUpdated() { - invalidate(); - } - }); - invalidate(); - } - - @Override - protected void onDraw(Canvas canvas) { - super.onDraw(canvas); - - if (vector == null || vector.paths.size() < 0) return; - - for (RichPath path : vector.paths) { - path.draw(canvas); + public void addPath(Path path) { + if (richPathDrawable != null) { + richPathDrawable.addPath(path); } }