This repository has been archived by the owner on Oct 8, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 257
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
migrate from View to ImageView to use the vector as a drawable
- Loading branch information
Showing
6 changed files
with
226 additions
and
118 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
147 changes: 147 additions & 0 deletions
147
richpath/src/main/java/com/richpath/RichPathDrawable.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
Oops, something went wrong.