feat(android): added ability to create background linear gradients. (#116)

This commit is contained in:
vultix
2018-05-03 04:19:18 -06:00
committed by Svetoslav
parent 54fd9774e7
commit 365dad7cc7
2 changed files with 54 additions and 1 deletions

View File

@@ -6,6 +6,7 @@ import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Path;
@@ -49,6 +50,7 @@ public class BorderDrawable extends ColorDrawable implements BitmapOwner {
private int backgroundColor;
private String backgroundImage;
private Bitmap backgroundBitmap;
private LinearGradientDefinition backgroundGradient;
private String backgroundRepeat;
private String backgroundPosition;
private CSSValue[] backgroundPositionParsedCSSValues;
@@ -149,6 +151,8 @@ public class BorderDrawable extends ColorDrawable implements BitmapOwner {
return backgroundBitmap;
}
public LinearGradientDefinition getBackgroundGradient() { return backgroundGradient; }
public String getBackgroundRepeat() {
return backgroundRepeat;
}
@@ -223,6 +227,7 @@ public class BorderDrawable extends ColorDrawable implements BitmapOwner {
int backgroundColor,
String backgroundImageUri,
Bitmap backgroundBitmap,
LinearGradientDefinition backgroundGradient,
Context context,
String backgroundRepeat,
String backgroundPosition,
@@ -250,6 +255,7 @@ public class BorderDrawable extends ColorDrawable implements BitmapOwner {
this.backgroundColor = backgroundColor;
this.backgroundImage = backgroundImageUri;
this.backgroundBitmap = backgroundBitmap;
this.backgroundGradient = backgroundGradient;
this.backgroundRepeat = backgroundRepeat;
this.backgroundPosition = backgroundPosition;
this.backgroundPositionParsedCSSValues = backgroundPositionParsedCSSValues;
@@ -361,6 +367,24 @@ public class BorderDrawable extends ColorDrawable implements BitmapOwner {
}
}
if (this.backgroundGradient != null) {
LinearGradientDefinition def = this.backgroundGradient;
Paint backgroundGradientPaint = new Paint();
LinearGradient shader = new LinearGradient(
def.getStartX() * width, def.getStartY() * height,
def.getEndX() * width, def.getEndY() * height,
def.getColors(), def.getStops(), Shader.TileMode.MIRROR);
backgroundGradientPaint.setAntiAlias(true);
backgroundGradientPaint.setFilterBitmap(true);
backgroundGradientPaint.setShader(shader);
if (this.clipPath != null && !this.clipPath.isEmpty()) {
drawClipPath(this.clipPath, canvas, backgroundGradientPaint, backgroundBoundsF, this.density);
} else {
canvas.drawPath(backgroundPath, backgroundGradientPaint);
}
}
// draw border
if (this.clipPath != null && !this.clipPath.isEmpty()) {
float borderWidth = this.getUniformBorderWidth();
@@ -386,7 +410,7 @@ public class BorderDrawable extends ColorDrawable implements BitmapOwner {
float[] borderOuterRadii = {
borderTopLeftRadius, borderTopLeftRadius,
borderTopRightRadius, borderTopRightRadius,
borderBottomRightRadius, borderBottomRightRadius,
borderBottomRightRadius, borderBottomRightRadius,
borderBottomLeftRadius, borderBottomLeftRadius
};
borderPath.addRoundRect(borderOuterRect, borderOuterRadii, Path.Direction.CW);

View File

@@ -0,0 +1,29 @@
package org.nativescript.widgets;
/**
* Created by Vultix on 3/12/2018.
*/
public class LinearGradientDefinition {
private float startX;
private float startY;
private float endX;
private float endY;
private int[] colors;
private float[] stops;
public float getStartX() { return startX; }
public float getStartY() { return startY; }
public float getEndX() { return endX; }
public float getEndY() { return endY; }
public int[] getColors() { return colors; }
public float[] getStops() { return stops; }
public LinearGradientDefinition(float startX, float startY, float endX, float endY, int[] colors, float[] stops) {
this.startX = startX;
this.startY = startY;
this.endX = endX;
this.endY = endY;
this.colors = colors;
this.stops = stops;
}
}