diff --git a/ui/border/border.android.ts b/ui/border/border.android.ts index 577b559ea..88839a50a 100644 --- a/ui/border/border.android.ts +++ b/ui/border/border.android.ts @@ -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) (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 = this._nativeView; - - var backgroundDrawable = nativeView.getBackground(); - if (!(backgroundDrawable instanceof android.graphics.drawable.GradientDrawable)) { - backgroundDrawable = new android.graphics.drawable.GradientDrawable(); - nativeView.setBackgroundDrawable(backgroundDrawable); - } - - var gd = 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); - } + (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)); } -} \ No newline at end of file +} + +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); + } +} diff --git a/ui/border/border.d.ts b/ui/border/border.d.ts index 6f8770306..560af09a9 100644 --- a/ui/border/border.d.ts +++ b/ui/border/border.d.ts @@ -30,7 +30,7 @@ declare module "ui/border" { borderColor: color.Color; //@private - _updateAndroidBorder(); + _updateAndroidBorder(bmp?: android.graphics.Bitmap); //@endprivate } } \ No newline at end of file diff --git a/ui/styling/stylers.android.ts b/ui/styling/stylers.android.ts index 42d510694..836c5c0bb 100644 --- a/ui/styling/stylers.android.ts +++ b/ui/styling/stylers.android.ts @@ -42,7 +42,7 @@ export class DefaultStyler implements definition.stylers.Styler { var bmp = 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 = view; + border._updateAndroidBorder(newValue); + } + + private static resetBackgroundImageSourceProperty(view: view.View, nativeValue: any) { + var border = view; + if (types.isDefined(nativeValue)) { + (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"); + } }