private PointF ToNativePointFPixels( Point point) { return(new PointF( NativeConversions.DevicePixelsToPixels(this.NativeContext, point.X), NativeConversions.DevicePixelsToPixels(this.NativeContext, point.Y))); }
private RectF ToNativeRectFPixels( Rectangle rectangle) { return(new RectF( NativeConversions.DevicePixelsToPixels(this.NativeContext, rectangle.X), NativeConversions.DevicePixelsToPixels(this.NativeContext, rectangle.Y), NativeConversions.DevicePixelsToPixels(this.NativeContext, rectangle.Right), NativeConversions.DevicePixelsToPixels(this.NativeContext, rectangle.Bottom))); }
public void DrawRectangle( Rectangle rectangle, Color color, float strokeWidth = 1.0f) { var rect = ToNativeRectFPixels(rectangle); var paint = new Paint() { Color = color.ToAndroidColor(), StrokeWidth = NativeConversions.DevicePixelsToPixels(this.NativeContext, strokeWidth), AntiAlias = true, }; paint.SetStyle(Paint.Style.Stroke); this._canvas.DrawRect(rect, paint); paint.Dispose(); }
// TODO: Replace with a Shadow property on View // ANDROID: View.Elevation public void FillEllipseShadow( Rectangle rectangle, Color color, float shadowBlur = 8.0f) { rectangle = rectangle.Inflate(-2 * shadowBlur, -2 * shadowBlur); var rect = ToNativeRectFPixels(rectangle); var shadowDp = NativeConversions.DevicePixelsToPixels(this.NativeContext, shadowBlur); var paint = new Paint() { Color = color.ToAndroidColor(), AntiAlias = true, }; paint.SetShadowLayer(shadowDp, shadowDp, shadowDp, global::Android.Graphics.Color.Black); paint.SetStyle(Paint.Style.Fill); this._canvas.DrawOval(rect, paint); paint.Dispose(); }
public void DrawText( string text, Rectangle bounds, Color color, TextFormat textFormat) { if (null == textFormat) { throw new ArgumentNullException(nameof(textFormat)); } if (String.IsNullOrEmpty(text)) { return; } var dpBounds = this.ToNativeRectFPixels(bounds); var paint = new Paint() { TextSize = NativeConversions.ScaledPixelsToPixels(this.NativeContext, textFormat.FontSize), TextAlign = Paint.Align.Left, Color = color.ToAndroidColor(), AntiAlias = true, }; var typeface = GetTypefaceFromTextFormat(textFormat); paint.SetTypeface(typeface); float originX; if (textFormat.HorizontalAlignment == LayoutAlignment.Start) { originX = 0; } else if (textFormat.HorizontalAlignment == LayoutAlignment.Center) { originX = 0.5f; } else if (textFormat.HorizontalAlignment == LayoutAlignment.End) { originX = 1.0f; } else if (textFormat.HorizontalAlignment == LayoutAlignment.Fill) { originX = 0.5f; } else { throw new NotSupportedException("Unsupported alignment."); } float originY; if (textFormat.VerticalAlignment == LayoutAlignment.Start) { originY = 0; } else if (textFormat.VerticalAlignment == LayoutAlignment.Center) { originY = 0.5f; } else if (textFormat.VerticalAlignment == LayoutAlignment.End) { originY = 1.0f; } else if (textFormat.VerticalAlignment == LayoutAlignment.Fill) { originY = 0.5f; } else { throw new NotSupportedException("Unsupported alignment."); } float x, y; if ((originX == 0) && (originY == 0)) { x = dpBounds.Left; y = dpBounds.Top; } else { Rect textBounds = new Rect(); paint.GetTextBounds(text, 0, text.Length, textBounds); x = ((dpBounds.Width() - textBounds.Width()) * originX) + dpBounds.Left; y = ((dpBounds.Height() - textBounds.Height()) * originY) + dpBounds.Top; } this._canvas.DrawText(text, 0, text.Length, x, y, paint); }