nicolasgramlich / andenginesvgtextureregionextension Goto Github PK
View Code? Open in Web Editor NEWAndEngine - SVGTextureRegion Extension
Home Page: http://www.andengine.org
AndEngine - SVGTextureRegion Extension
Home Page: http://www.andengine.org
the new Android Lint tool suggests the following:
Use android.util.FloatMath#ceil() instead of java.lang.Math#ceil to avoid argument float to double conversion
for lines 154 and 155 of SVGHandler.java
Use new SparseIntArray(...) instead for better performance
for line 21 of SVGDirectColorMapper.java
Pattern fill now isn't supported in this extension.
I implemented it in my modification of andengine. I modified it hardly, so I cannot push it, but I can describe how I did it.
Here's new methodes for *Handler:
protected void parsePattern(Attributes attributes) {
this.attributes = attributes;
canvas.push(currentCanvas);
currentCanvas = mSVGPaint.createCanvasForPatternFill(attributes);
}
@Override
protected void parsePatternEnd() {
currentCanvas = canvas.pop();
}
New SVGPaint method
public Canvas createCanvasForPatternFill(Attributes pAttributes) {
final String id = SAXHelper.getStringAttribute(pAttributes, ATTRIBUTE_ID);
if (id == null) {
return null;
}
Float widthF = SAXHelper.getFloatAttribute(pAttributes, ATTRIBUTE_WIDTH, 1);
Float heightF = SAXHelper.getFloatAttribute(pAttributes, ATTRIBUTE_HEIGHT, 1);
int width = (int) Math.ceil(widthF);
int height = (int) Math.ceil(heightF);
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
String viewBox = SAXHelper.getStringAttribute(pAttributes, "viewBox");
if (viewBox != null) {
String[] strings = viewBox.split("\\s");
float posX = Float.valueOf(strings[0]);
float posY = Float.valueOf(strings[1]);
canvas.translate(-posX, -posY);
}
canvas.scale(SVGPatternFill.DETALIZATION_SCALE, SVGPatternFill.DETALIZATION_SCALE);
final SVGGradient svgGradient = new SVGPatternFill(id, pAttributes, bitmap);
this.mSVGGradientMap.put(id, svgGradient);
return canvas;
}
And SVGPatternFill class
public class SVGPatternFill extends SVGGradient {
public static final float DETALIZATION_SCALE = 2f;
private Bitmap bitmap;
public SVGPatternFill(String id, Attributes attributes, Bitmap bitmap) {
super(id, Type.PATTERN, attributes);
this.bitmap = bitmap;
}
@Override
public Shader createShader() {
if (this.mShader != null) {
return this.mShader;
}
mShader = new BitmapShader(bitmap, Shader.TileMode.REPEAT, Shader.TileMode.REPEAT);
this.mMatrix = this.getTransform();
if (this.mMatrix == null) {
mMatrix = new Matrix();
}
mMatrix.postScale(1/DETALIZATION_SCALE, 1/DETALIZATION_SCALE);
this.mShader.setLocalMatrix(this.mMatrix);
return this.mShader;
}
@Override
protected void resolveHref(HashMap<String, SVGGradient> pSVGGradientMap) {
super.resolveHref(pSVGGradientMap);
bitmap = ((SVGPatternFill) mParent).bitmap;
}
@Override
protected Matrix getTransform() {
if (this.mMatrix != null) {
return this.mMatrix;
} else {
final String transfromString = this.mSVGAttributes.getStringAttribute(ATTRIBUTE_PATTERN_TRANSFORM, false);
return getTransformFromString(transfromString);
}
}
}
Probably not best solution, but I just get acceptable result and want to share it :)
Pick specified group by id/class to create tile. For SVG, there is no need to make it like a sprite-like graphic
Pattern fill now isn't supported in this extension.
I implemented it in my modification of andengine. I modified it hardly, so I cannot push it, but I can describe how I did it.
Here's new methodes for *Handler:
protected void parsePattern(Attributes attributes) {
this.attributes = attributes;
canvas.push(currentCanvas);
currentCanvas = mSVGPaint.createCanvasForPatternFill(attributes);
}
@Override
protected void parsePatternEnd() {
currentCanvas = canvas.pop();
}
New SVGPaint method
public Canvas createCanvasForPatternFill(Attributes pAttributes) {
final String id = SAXHelper.getStringAttribute(pAttributes, ATTRIBUTE_ID);
if (id == null) {
return null;
}
Float widthF = SAXHelper.getFloatAttribute(pAttributes, ATTRIBUTE_WIDTH, 1);
Float heightF = SAXHelper.getFloatAttribute(pAttributes, ATTRIBUTE_HEIGHT, 1);
int width = (int) Math.ceil(widthF);
int height = (int) Math.ceil(heightF);
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
String viewBox = SAXHelper.getStringAttribute(pAttributes, "viewBox");
if (viewBox != null) {
String[] strings = viewBox.split("\\s");
float posX = Float.valueOf(strings[0]);
float posY = Float.valueOf(strings[1]);
canvas.translate(-posX, -posY);
}
canvas.scale(SVGPatternFill.DETALIZATION_SCALE, SVGPatternFill.DETALIZATION_SCALE);
final SVGGradient svgGradient = new SVGPatternFill(id, pAttributes, bitmap);
this.mSVGGradientMap.put(id, svgGradient);
return canvas;
}
And SVGPatternFill class
public class SVGPatternFill extends SVGGradient {
public static final float DETALIZATION_SCALE = 2f;
private Bitmap bitmap;
public SVGPatternFill(String id, Attributes attributes, Bitmap bitmap) {
super(id, Type.PATTERN, attributes);
this.bitmap = bitmap;
}
@Override
public Shader createShader() {
if (this.mShader != null) {
return this.mShader;
}
mShader = new BitmapShader(bitmap, Shader.TileMode.REPEAT, Shader.TileMode.REPEAT);
this.mMatrix = this.getTransform();
if (this.mMatrix == null) {
mMatrix = new Matrix();
}
mMatrix.postScale(1/DETALIZATION_SCALE, 1/DETALIZATION_SCALE);
this.mShader.setLocalMatrix(this.mMatrix);
return this.mShader;
}
@Override
protected void resolveHref(HashMap<String, SVGGradient> pSVGGradientMap) {
super.resolveHref(pSVGGradientMap);
bitmap = ((SVGPatternFill) mParent).bitmap;
}
@Override
protected Matrix getTransform() {
if (this.mMatrix != null) {
return this.mMatrix;
} else {
final String transfromString = this.mSVGAttributes.getStringAttribute(ATTRIBUTE_PATTERN_TRANSFORM, false);
return getTransformFromString(transfromString);
}
}
}
Probably not best solution, but I just get acceptable result and want to share it :)
Adobe Illustrator generated for me SVG file that contains following lines:
<polyline fill="none" stroke="#000000" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round" points="
25.19,53.59 24.7,53.72 19.68,55.04 "/>
To fix this error, we need just trim string before splitting it.
final String[] parts = pString.trim().split("[\\s,]+");
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.