mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-11-05 13:26:48 +08:00
backgroundImage support for Border
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
import proxy = require("ui/core/proxy");
|
||||
import dependencyObservable = require("ui/core/dependency-observable");
|
||||
import utils = require("utils/utils");
|
||||
import color = require("color");
|
||||
|
||||
// merge the exports of the common file with the exports of this file
|
||||
declare var exports;
|
||||
@@ -17,35 +18,50 @@ function onBorderPropertyChanged(data: dependencyObservable.PropertyChangeData)
|
||||
(<proxy.PropertyMetadata>borderCommon.Border.borderColorProperty.metadata).onSetNativeValue = onBorderPropertyChanged;
|
||||
|
||||
export class Border extends borderCommon.Border {
|
||||
public _updateAndroidBorder() {
|
||||
public _updateAndroidBorder(bmp?: android.graphics.Bitmap) {
|
||||
if (!this._nativeView) {
|
||||
return;
|
||||
}
|
||||
|
||||
var nativeView = <android.view.ViewGroup>this._nativeView;
|
||||
|
||||
var backgroundDrawable = nativeView.getBackground();
|
||||
if (!(backgroundDrawable instanceof android.graphics.drawable.GradientDrawable)) {
|
||||
backgroundDrawable = new android.graphics.drawable.GradientDrawable();
|
||||
nativeView.setBackgroundDrawable(backgroundDrawable);
|
||||
}
|
||||
|
||||
var gd = <android.graphics.drawable.GradientDrawable>backgroundDrawable;
|
||||
var density = utils.layout.getDisplayDensity();
|
||||
gd.setCornerRadius(this.cornerRadius * density);
|
||||
|
||||
if (this.borderColor) {
|
||||
gd.setStroke(this.borderWidth * density, this.borderColor.android);
|
||||
}
|
||||
else {
|
||||
gd.setStroke(this.borderWidth * density, android.graphics.Color.TRANSPARENT);
|
||||
}
|
||||
|
||||
if (this.backgroundColor) {
|
||||
gd.setColor(this.backgroundColor.android);
|
||||
}
|
||||
else {
|
||||
gd.setColor(android.graphics.Color.TRANSPARENT);
|
||||
}
|
||||
(<android.view.ViewGroup>this._nativeView).setBackgroundDrawable(new BorderGradientDrawable(this.borderWidth,
|
||||
this.borderColor ? this.borderColor.android : android.graphics.Color.TRANSPARENT,
|
||||
this.backgroundColor ? this.backgroundColor.android : android.graphics.Color.TRANSPARENT,
|
||||
this.cornerRadius,
|
||||
bmp));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class BorderGradientDrawable extends android.graphics.drawable.GradientDrawable {
|
||||
private paint: android.graphics.Paint;
|
||||
private stroke: number;
|
||||
private bitmap: android.graphics.Bitmap
|
||||
|
||||
constructor(borderWidth: number, borderColor: number, backgroundColor: number, cornerRadius: number, bitmap?: android.graphics.Bitmap) {
|
||||
super();
|
||||
|
||||
this.bitmap = bitmap;
|
||||
|
||||
if (bitmap) {
|
||||
this.paint = new android.graphics.Paint();
|
||||
} else {
|
||||
this.setColor(backgroundColor);
|
||||
}
|
||||
|
||||
var density = utils.layout.getDisplayDensity();
|
||||
|
||||
this.stroke = borderWidth * density;
|
||||
this.setStroke(this.stroke, borderColor);
|
||||
|
||||
var cornerRadius = borderWidth * density;
|
||||
this.setCornerRadius(cornerRadius);
|
||||
|
||||
return global.__native(this);
|
||||
}
|
||||
|
||||
public draw(canvas: android.graphics.Canvas): void {
|
||||
if (this.paint) {
|
||||
canvas.drawBitmap(this.bitmap, this.stroke, this.stroke, this.paint);
|
||||
}
|
||||
super.draw(canvas);
|
||||
}
|
||||
}
|
||||
|
||||
2
ui/border/border.d.ts
vendored
2
ui/border/border.d.ts
vendored
@@ -30,7 +30,7 @@ declare module "ui/border" {
|
||||
borderColor: color.Color;
|
||||
|
||||
//@private
|
||||
_updateAndroidBorder();
|
||||
_updateAndroidBorder(bmp?: android.graphics.Bitmap);
|
||||
//@endprivate
|
||||
}
|
||||
}
|
||||
@@ -42,7 +42,7 @@ export class DefaultStyler implements definition.stylers.Styler {
|
||||
var bmp = <android.graphics.Bitmap>newValue;
|
||||
var d = new android.graphics.drawable.BitmapDrawable(bmp);
|
||||
d.setTileModeXY(android.graphics.Shader.TileMode.REPEAT, android.graphics.Shader.TileMode.REPEAT);
|
||||
d.setDither(true);
|
||||
d.setDither(true);
|
||||
nativeView.setBackgroundDrawable(d);
|
||||
}
|
||||
|
||||
@@ -356,10 +356,40 @@ export class BorderStyler implements definition.stylers.Styler {
|
||||
border._updateAndroidBorder();
|
||||
}
|
||||
|
||||
public static registerHandlers() {
|
||||
//Background image methods
|
||||
private static setBackgroundImageSourceProperty(view: view.View, newValue: any) {
|
||||
var border = <border.Border>view;
|
||||
border._updateAndroidBorder(newValue);
|
||||
}
|
||||
|
||||
private static resetBackgroundImageSourceProperty(view: view.View, nativeValue: any) {
|
||||
var border = <border.Border>view;
|
||||
if (types.isDefined(nativeValue)) {
|
||||
(<android.view.View>view.android).setBackgroundDrawable(nativeValue);
|
||||
border._updateAndroidBorder();
|
||||
}
|
||||
}
|
||||
|
||||
private static getNativeBackgroundImageSourceValue(view: view.View): any {
|
||||
var drawable = view.android.getBackground();
|
||||
|
||||
if (drawable instanceof android.graphics.drawable.GradientDrawable) {
|
||||
return drawable;
|
||||
}
|
||||
|
||||
return undefined;
|
||||
}
|
||||
|
||||
public static registerHandlers() {
|
||||
style.registerHandler(style.backgroundColorProperty, new stylersCommon.StylePropertyChangedHandler(
|
||||
BorderStyler.setBackgroundProperty,
|
||||
BorderStyler.resetBackgroundProperty), "Border");
|
||||
|
||||
style.registerHandler(style.backgroundImageSourceProperty, new stylersCommon.StylePropertyChangedHandler(
|
||||
BorderStyler.setBackgroundImageSourceProperty,
|
||||
BorderStyler.resetBackgroundImageSourceProperty,
|
||||
BorderStyler.getNativeBackgroundImageSourceValue), "Border");
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user