Optimisations

This commit is contained in:
Rossen Hristov
2016-06-24 14:47:52 +03:00
parent abf4cc585b
commit cfdaa8a598

View File

@@ -13,6 +13,7 @@ import android.graphics.Rect;
import android.graphics.RectF; import android.graphics.RectF;
import android.graphics.drawable.ColorDrawable; import android.graphics.drawable.ColorDrawable;
import java.util.Locale; import java.util.Locale;
import java.util.regex.Pattern;
/** /**
* Created by hristov on 6/15/2016. * Created by hristov on 6/15/2016.
@@ -25,8 +26,6 @@ public class BorderDrawable extends ColorDrawable {
private String clipPath; private String clipPath;
private int backgroundColor; private int backgroundColor;
private Bitmap backgroundImage; private Bitmap backgroundImage;
private float backgroundImageWidth;
private float backgroundImageHeight;
private String backgroundRepeat; private String backgroundRepeat;
private String backgroundPosition; private String backgroundPosition;
private CSSValue[] backgroundPositionParsedCSSValues; private CSSValue[] backgroundPositionParsedCSSValues;
@@ -57,14 +56,6 @@ public class BorderDrawable extends ColorDrawable {
return backgroundImage; return backgroundImage;
} }
public float getBackgroundImageWidth() {
return backgroundImageWidth;
}
public float getBackgroundImageHeight() {
return backgroundImageHeight;
}
public String getBackgroundRepeat() { public String getBackgroundRepeat() {
return backgroundRepeat; return backgroundRepeat;
} }
@@ -88,8 +79,6 @@ public class BorderDrawable extends ColorDrawable {
String clipPath, String clipPath,
int backgroundColor, int backgroundColor,
Bitmap backgroundImage, Bitmap backgroundImage,
float backgroundImageWidth,
float backgroundImageHeight,
String backgroundRepeat, String backgroundRepeat,
String backgroundPosition, String backgroundPosition,
CSSValue[] backgroundPositionParsedCSSValues, CSSValue[] backgroundPositionParsedCSSValues,
@@ -101,8 +90,6 @@ public class BorderDrawable extends ColorDrawable {
this.clipPath = clipPath; this.clipPath = clipPath;
this.backgroundColor = backgroundColor; this.backgroundColor = backgroundColor;
this.backgroundImage = backgroundImage; this.backgroundImage = backgroundImage;
this.backgroundImageWidth = backgroundImageWidth;
this.backgroundImageHeight = backgroundImageHeight;
this.backgroundRepeat = backgroundRepeat; this.backgroundRepeat = backgroundRepeat;
this.backgroundPosition = backgroundPosition; this.backgroundPosition = backgroundPosition;
this.backgroundPositionParsedCSSValues = backgroundPositionParsedCSSValues; this.backgroundPositionParsedCSSValues = backgroundPositionParsedCSSValues;
@@ -226,34 +213,32 @@ public class BorderDrawable extends ColorDrawable {
path1.op(path2, Path.Op.INTERSECT); path1.op(path2, Path.Op.INTERSECT);
} }
private static Pattern spaceAndComma = Pattern.compile("[\\s,]+");
private static Pattern space = Pattern.compile("\\s+");
private static void drawClipPath(String clipPath, Canvas canvas, Paint paint, RectF bounds, float density) { private static void drawClipPath(String clipPath, Canvas canvas, Paint paint, RectF bounds, float density) {
// Sample string is polygon(20% 0%, 0% 20%, 30% 50%, 0% 80%, 20% 100%, 50% 70%, 80% 100%, 100% 80%, 70% 50%, 100% 20%, 80% 0%, 50% 30%); // Sample string is polygon(20% 0%, 0% 20%, 30% 50%, 0% 80%, 20% 100%, 50% 70%, 80% 100%, 100% 80%, 70% 50%, 100% 20%, 80% 0%, 50% 30%);
String functionName = clipPath.substring(0, clipPath.indexOf("(")); String functionName = clipPath.substring(0, clipPath.indexOf("("));
String value = clipPath.replace(functionName+"(", "").replace(")", ""); String value = clipPath.substring(clipPath.indexOf("(") + 1, clipPath.indexOf(")"));
String[] arr; String[] arr;
float left;
float top;
float right;
float bottom;
switch (functionName){ switch (functionName){
case "rect": case "rect":
arr = value.split("[\\s,]+"); arr = spaceAndComma.split(value);
top = cssValueToDevicePixels(arr[0], bounds.top, density); float top = cssValueToDevicePixels(arr[0], bounds.top, density);
left = cssValueToDevicePixels(arr[1], bounds.left, density); float left = cssValueToDevicePixels(arr[1], bounds.left, density);
bottom = cssValueToDevicePixels(arr[2], bounds.bottom, density); float bottom = cssValueToDevicePixels(arr[2], bounds.bottom, density);
right = cssValueToDevicePixels(arr[3], bounds.right, density); float right = cssValueToDevicePixels(arr[3], bounds.right, density);
canvas.drawRect(left, top, right, bottom, paint); canvas.drawRect(left, top, right, bottom, paint);
break; break;
case "circle": case "circle":
arr = value.split("\\s+"); arr = space.split(value);
float radius = cssValueToDevicePixels(arr[0], (bounds.width() > bounds.height() ? bounds.height() : bounds.width()) / 2, density); float radius = cssValueToDevicePixels(arr[0], (bounds.width() > bounds.height() ? bounds.height() : bounds.width()) / 2, density);
float y = cssValueToDevicePixels(arr[2], bounds.height(), density); float y = cssValueToDevicePixels(arr[2], bounds.height(), density);
float x = cssValueToDevicePixels(arr[3], bounds.width(), density); float x = cssValueToDevicePixels(arr[3], bounds.width(), density);
canvas.drawCircle(x, y, radius, paint); canvas.drawCircle(x, y, radius, paint);
break; break;
case "ellipse": case "ellipse":
arr = value.split("\\s+"); arr = space.split(value);
float rX = cssValueToDevicePixels(arr[0], bounds.right, density); float rX = cssValueToDevicePixels(arr[0], bounds.right, density);
float rY = cssValueToDevicePixels(arr[1], bounds.bottom, density); float rY = cssValueToDevicePixels(arr[1], bounds.bottom, density);
float cX = cssValueToDevicePixels(arr[3], bounds.right, density); float cX = cssValueToDevicePixels(arr[3], bounds.right, density);
@@ -269,7 +254,7 @@ public class BorderDrawable extends ColorDrawable {
PointF firstPoint = null; PointF firstPoint = null;
arr = value.split(","); arr = value.split(",");
for (String s : arr) { for (String s : arr) {
String[] xy = s.trim().split("\\s+"); String[] xy = space.split(s.trim());
PointF point = new PointF(cssValueToDevicePixels(xy[0], bounds.width(), density), cssValueToDevicePixels(xy[1], bounds.height(), density)); PointF point = new PointF(cssValueToDevicePixels(xy[0], bounds.width(), density), cssValueToDevicePixels(xy[1], bounds.height(), density));
if (firstPoint == null) { if (firstPoint == null) {
@@ -308,8 +293,8 @@ public class BorderDrawable extends ColorDrawable {
} }
} }
float imageWidth = this.backgroundImageWidth; float imageWidth = this.backgroundImage.getWidth();
float imageHeight = this.backgroundImageHeight; float imageHeight = this.backgroundImage.getHeight();
// size // size
if (this.backgroundSize != null && !this.backgroundSize.isEmpty()) { if (this.backgroundSize != null && !this.backgroundSize.isEmpty()) {
@@ -420,23 +405,15 @@ public class BorderDrawable extends ColorDrawable {
float result; float result;
source = source.trim(); source = source.trim();
if (source.contains("px")) { if (source.indexOf("px") > -1) {
result = Float.parseFloat(source.replace("px", "")); result = Float.parseFloat(source.replace("px", ""));
} }
else if (source.contains("%") && total > 0) { else if (source.indexOf("%") > -1 && total > 0) {
result = (Float.parseFloat(source.replace("%", "")) / 100) * toDeviceIndependentPixels(total, density); result = (Float.parseFloat(source.replace("%", "")) / 100) * (total / density);
} else { } else {
result = Float.parseFloat(source); result = Float.parseFloat(source);
} }
return toDevicePixels(result, density); return result * density;
}
private static float toDevicePixels(float value, float density) {
return value * density;
}
private static float toDeviceIndependentPixels(float value, float density) {
return value / density;
} }
private class BackgroundDrawParams { private class BackgroundDrawParams {