public Bitmap GetBitmapMarker(Context mContext, int resourceId, string text) { Resources resources = mContext.Resources; float scale = resources.DisplayMetrics.Density; Bitmap bitmap = BitmapFactory.DecodeResource(resources, resourceId); Bitmap.Config bitmapConfig = bitmap.GetConfig(); // set default bitmap config if none if (bitmapConfig == null) bitmapConfig = Bitmap.Config.Argb8888; bitmap = bitmap.Copy(bitmapConfig, true); Canvas canvas = new Canvas(bitmap); Paint paint = new Paint(PaintFlags.AntiAlias); paint.Color = global::Android.Graphics.Color.Black; paint.TextSize = ((int)(14 * scale)); paint.SetShadowLayer(1f, 0f, 1f, global::Android.Graphics.Color.White); // draw text to the Canvas center Rect bounds = new Rect(); paint.GetTextBounds(text, 0, text.Length, bounds); int x = (bitmap.Width - bounds.Width()) / 2; int y = (bitmap.Height + bounds.Height()) / 2 - 20; canvas.DrawText(text, x, y, paint); return bitmap; }
public override void Draw(Canvas canvas) { LoadResources (); var blackPaint = new Paint () { Color = black.Value }; var whitePaint = new Paint () { Color = white.Value }; XamGame.RenderBoard ((RectangleF rect, Square.ColourNames color) => { var paint = color == Square.ColourNames.White ? whitePaint : blackPaint; canvas.DrawRect (rect.X, rect.Y, rect.Right, rect.Bottom, paint); }, (RectangleF rect, object image) => { if (image != null) canvas.DrawBitmap ((Bitmap) image, rect.Left, rect.Top, null); }); // New Game button whitePaint.Color = white.Value; whitePaint.SetStyle (Paint.Style.Fill); whitePaint.TextSize = 30; whitePaint.AntiAlias = true; Rect bounds = new Rect (); whitePaint.GetTextBounds ("New Game", 0, 8, bounds); canvas.DrawText ("New Game", (this.Width - bounds.Width ()) / 2, this.Bottom - (XamGame.BoardUpperLeftCorner.Y - bounds.Height ()) / 2, whitePaint); whitePaint.Dispose (); blackPaint.Dispose (); base.Draw (canvas); }
protected internal override void ComputeRender(float renderProgress) { RectF arcBounds = mCurrentBounds; //compute gas tube bounds mGasTubeBounds.Set(arcBounds.CenterX() - mGasTubeWidth / 2.0f, arcBounds.CenterY(), arcBounds.CenterX() + mGasTubeWidth / 2.0f, arcBounds.CenterY() + mGasTubeHeight); //compute pipe body bounds mPipeBodyBounds.Set(arcBounds.CenterX() + mGasTubeWidth / 2.0f - mPipeBodyWidth / 2.0f, arcBounds.CenterY() - mPipeBodyHeight, arcBounds.CenterX() + mGasTubeWidth / 2.0f + mPipeBodyWidth / 2.0f, arcBounds.CenterY()); //compute cannula bounds mCannulaBounds.Set(arcBounds.CenterX() + mGasTubeWidth / 2.0f - mCannulaWidth / 2.0f, arcBounds.CenterY() - mCannulaHeight - mCannulaOffsetY, arcBounds.CenterX() + mGasTubeWidth / 2.0f + mCannulaWidth / 2.0f, arcBounds.CenterY() - mCannulaOffsetY); //compute balloon bounds float insetX = mBalloonWidth * 0.333f * (1 - mProgress); float insetY = mBalloonHeight * 0.667f * (1 - mProgress); mBalloonBounds.Set(arcBounds.CenterX() - mGasTubeWidth / 2.0f - mBalloonWidth / 2.0f + insetX, arcBounds.CenterY() - mBalloonHeight + insetY, arcBounds.CenterX() - mGasTubeWidth / 2.0f + mBalloonWidth / 2.0f - insetX, arcBounds.CenterY()); if (renderProgress <= START_INHALE_DURATION_OFFSET) { mCannulaBounds.Offset(0, -mCannulaMaxOffsetY * renderProgress / START_INHALE_DURATION_OFFSET); mProgress = 0.0f; mProgressText = 10 + PERCENT_SIGN; mPaint.TextSize = mTextSize; mPaint.GetTextBounds(mProgressText, 0, mProgressText.Length, mProgressBounds); } else { float exhaleProgress = ACCELERATE_INTERPOLATOR.GetInterpolation(1.0f - (renderProgress - START_INHALE_DURATION_OFFSET) / (1.0f - START_INHALE_DURATION_OFFSET)); mCannulaBounds.Offset(0, -mCannulaMaxOffsetY * exhaleProgress); mProgress = 1.0f - exhaleProgress; mProgressText = AdjustProgress((int)(exhaleProgress * 100.0f)) + PERCENT_SIGN; mPaint.TextSize = mTextSize; mPaint.GetTextBounds(mProgressText, 0, mProgressText.Length, mProgressBounds); } }
public override void Draw(Canvas canvas, ICharSequence text, Int32 start, Int32 end, Single x, Int32 top, Int32 y, Int32 bottom, Paint paint) { ApplyCustomTypeFace(paint, _type); paint.GetTextBounds(_icon, 0, 1, TEXT_BOUNDS); canvas.Save(); var baselineRatio = _baselineAligned ? 0f : BASELINE_RATIO; if (_rotate) { var rotation = (SystemClock.CurrentThreadTimeMillis() - _rotationStartTime) / (Single)ROTATION_DURATION * 360f; var centerX = x + TEXT_BOUNDS.Width() / 2f; var centerY = y - TEXT_BOUNDS.Height() / 2f + TEXT_BOUNDS.Height() * baselineRatio; canvas.Rotate(rotation, centerX, centerY); } canvas.DrawText(_icon, x - TEXT_BOUNDS.Left, y - TEXT_BOUNDS.Bottom + TEXT_BOUNDS.Height() * baselineRatio, paint); canvas.Restore(); }
internal void DrawText(string text, Paint p, Typeface font, float size, Rectangle rect, ReoGridHorAlign halign, ReoGridVerAlign valign) { p.SetTypeface(font); p.TextSize = size; var measuredRect = new Rect(); p.GetTextBounds(text, 0, text.Length, measuredRect); float x = rect.Left, y = rect.Top; switch (halign) { case ReoGridHorAlign.General: case ReoGridHorAlign.Left: x = rect.Left; break; case ReoGridHorAlign.Center: x = rect.Left + (rect.Width - measuredRect.Width()) / 2; break; case ReoGridHorAlign.Right: x = rect.Right - measuredRect.Width(); break; } switch (valign) { case ReoGridVerAlign.Top: y = rect.Top + measuredRect.Height(); break; case ReoGridVerAlign.Middle: y = rect.Bottom - (rect.Height - measuredRect.Height()) / 2; break; case ReoGridVerAlign.General: case ReoGridVerAlign.Bottom: y = rect.Bottom; break; } this.canvas.DrawText(text, x, y, p); }
public override void Draw(Canvas canvas, ICharSequence text, int start, int end, float x, int top, int y, int bottom, Paint paint) { ApplyCustomTypeFace(paint, _typeface); paint.GetTextBounds(_icon, 0, 1, TextBounds); canvas.Save(); if (_rotate) { var rotation = (DateTimeHelpers.CurrentUnixTimeMillis() - _rotationStartTime)/(float) RotationDuration* 360f; var centerX = x + TextBounds.Width()/2f; var centerY = y - TextBounds.Height()/2f + TextBounds.Height()*BaselineRatio; canvas.Rotate(rotation, centerX, centerY); } canvas.DrawText(_icon, x - TextBounds.Left, y - TextBounds.Bottom + TextBounds.Height()*BaselineRatio, paint); canvas.Restore(); }
public override void Draw(Canvas canvas, ICharSequence text, int start, int end, float x, int top, int y, int bottom, Paint paint) { ApplyCustomTypeFace(paint, type); paint.GetTextBounds(icon, 0, 1, TEXT_BOUNDS); canvas.Save(); float baselineRatio = baselineAligned ? 0f : BASELINE_RATIO; if (rotate) { long time = DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond; float rotation = (time - rotationStartTime) / (float)ROTATION_DURATION * 360f; float centerX = x + TEXT_BOUNDS.Width() / 2f; float centerY = y - TEXT_BOUNDS.Height() / 2f + TEXT_BOUNDS.Height() * baselineRatio; canvas.Rotate(rotation, centerX, centerY); } canvas.DrawText(icon, x - TEXT_BOUNDS.Left, y - TEXT_BOUNDS.Bottom + TEXT_BOUNDS.Height() * baselineRatio, paint); canvas.Restore(); }
public Size MeasureText(string text, string fontFamily = null, double fontSize = 10, double fontWeight = 500) { if (string.IsNullOrEmpty(text)) { return Size.Empty; } using (var paint = new Paint()) { paint.AntiAlias = true; paint.TextSize = (float)fontSize; var bounds = new Rect(); paint.GetTextBounds(text, 0, text.Length, bounds); // var width = paint.MeasureText(text); return new Size(bounds.Width(), bounds.Height()); } }
public void DrawText(Point p, string text, Color fill, string fontFamily = null, double fontSize = 10, double fontWeight = 500, double rotate = 0, HorizontalAlignment halign = HorizontalAlignment.Left, VerticalAlignment valign = VerticalAlignment.Top, Size? maxSize = new Size?()) { using (var paint = new Paint()) { paint.AntiAlias = true; paint.TextSize = (float)fontSize; paint.Color = fill; var bounds = new Rect(); paint.GetTextBounds(text, 0, text.Length, bounds); float dx = 0; if (halign == HorizontalAlignment.Center) { dx = -bounds.Width() / 2; } if (halign == HorizontalAlignment.Right) { dx = -bounds.Width(); } float dy = 0; if (valign == VerticalAlignment.Center) { dy = +bounds.Height() / 2; } if (valign == VerticalAlignment.Top) { dy = bounds.Height(); } canvas.Save(); canvas.Translate(dx, dy); canvas.Rotate((float)rotate); canvas.Translate((float)p.X, (float)p.Y); canvas.DrawText(text, 0, 0, paint); canvas.Restore(); } }
protected override void OnDraw(Canvas canvas) { base.OnDraw (canvas); //create an instance of class Paint, set color and font size var textPaint = new Paint { AntiAlias = true, Color = _textColor, TextSize = _textSize }; //In order to show text in a middle, we need to know its size var bounds = new Rect (); textPaint.GetTextBounds (_text, 0, _text.Length, bounds); //Now we store font size in bounds variable and can calculate it's position var x = (Width / 2) - bounds.CenterX (); var y = (Height / 2) - bounds.CenterY (); //drawing text with appropriate color and size in the center canvas.DrawText (_text, x, y, textPaint); }
public BasicRectangle MeasureText(string text, BasicRectangle rectangle, string fontFace, float fontSize) { var paint = new Paint { AntiAlias = true, TextSize = fontSize * Density }; var rectText = new Rect(); paint.GetTextBounds(text, 0, text.Length, rectText); return new BasicRectangle(rectText.Left, rectText.Top, rectText.Width(), rectText.Height()); }
public void DrawText(string text, BasicPoint point, BasicColor color, string fontFace, float fontSize) { var paint = new Paint { AntiAlias = true, Color = GenericControlHelper.ToColor(color), TextSize = fontSize * Density, }; var boundsText = new Rect(); paint.GetTextBounds(text, 0, text.Length, boundsText); _canvas.DrawText(text, point.X - boundsText.Left, point.Y - boundsText.Top, paint); //_canvas.DrawText(text, point.X, point.Y, paint); }
/// <Docs>The Canvas to which the View is rendered.</Docs> /// <summary> /// Draw the specified canvas. /// </summary> /// <param name="canvas">Canvas to draw onto.</param> public override void Draw(Canvas canvas) { BadgeImage image = (BadgeImage)Element; // Set color of icon if (Control.Drawable != null) { if (image.Selected) { Control.Drawable.SetColorFilter(filterColor); } else { Control.Drawable.SetColorFilter(filterGray); } } base.Draw(canvas); if (image.Number > 0) { using (Paint paint = new Paint()) { paint.Color = image.Selected ? Xamarin.Forms.Color.Red.ToAndroid() : Xamarin.Forms.Color.Gray.ToAndroid(); paint.StrokeWidth = 0f; paint.SetStyle(Paint.Style.FillAndStroke); // Calc text size paint.TextSize = (int)this.DipToPixel(16); paint.FakeBoldText = true; string text = image.Number.ToString(); Rect textBounds = new Rect(0, 0, 0, 0); paint.GetTextBounds(text, 0, text.Length, textBounds); float textWidth = paint.MeasureText(text); float textHeight = textBounds.Height(); float badgeWidth = textWidth + this.DipToPixel(9); float badgeHeight = textHeight + this.DipToPixel(9); if (badgeWidth < badgeHeight) { badgeWidth = badgeHeight; } double offsetX = (image.Bounds.Width - image.Bounds.Width) / 2; double offsetY = (image.Bounds.Height - image.Bounds.Height) / 2; float left = this.DipToPixel(image.Bounds.Width) - badgeWidth; float top = 1; float right = left + badgeWidth; float bottom = top + badgeHeight; float radius = (badgeHeight / 2f) - 1f; using (Path path = new Path()) { canvas.DrawRoundRect(new RectF(left, top, left + badgeWidth, top + badgeHeight), radius, radius, paint); paint.Color = Xamarin.Forms.Color.White.ToAndroid(); canvas.DrawText(image.Number.ToString(), left + ((badgeWidth - textWidth) / 2) - 1, bottom - ((badgeHeight - textHeight) / 2), paint); } } } }
private void InitializeImages() { if(_pressedImage != null) { _pressedImage.Recycle(); _pressedImage.Dispose(); } if(_unpressedImage != null) { _unpressedImage.Recycle(); _unpressedImage.Dispose(); } _unpressedImage = Bitmap.CreateBitmap(Width, Settings.IsSquared ? Width : Height, Bitmap.Config.Argb8888); _pressedImage = Bitmap.CreateBitmap(Width, Settings.IsSquared ? Width : Height, Bitmap.Config.Argb8888); Canvas unpressedCanvas = new Canvas(_unpressedImage); Canvas pressedCanvas = new Canvas(_pressedImage); Settings.SetTextSize(ComplexUnitType.Px, Settings.TextSize == 0f ? Height * Settings.TextSizeRatio : Settings.TextSize); Settings.StrokeBorderWidth = Height * Settings.StrokeBorderWidthRatio; Settings.StrokeTextWidth = Height * Settings.StrokeTextWidthRatio; // Background fill paint Paint fillBackPaint = new Paint(); fillBackPaint.Color = Settings.FillColor; fillBackPaint.AntiAlias = true; // Background stroke paint Paint strokeBackPaint = new Paint(); strokeBackPaint.Color = Settings.StrokeColor; strokeBackPaint.SetStyle(Paint.Style.Stroke); strokeBackPaint.StrokeWidth = Settings.StrokeBorderWidth; strokeBackPaint.AntiAlias = true; // Text paint Paint textPaint = new Paint(); textPaint.Color = Settings.IsTextStroked ? Settings.FillColor : Settings.StrokeColor; textPaint.TextAlign = Paint.Align.Center; textPaint.TextSize = Settings.TextSize; textPaint.SetTypeface(Settings.Typeface); textPaint.AntiAlias = true; // Text stroke paint Paint strokePaint = new Paint(); strokePaint.Color = Settings.StrokeColor; strokePaint.TextAlign = Paint.Align.Center; strokePaint.TextSize = Settings.TextSize; strokePaint.SetTypeface(Settings.Typeface); strokePaint.SetStyle(Paint.Style.Stroke); strokePaint.StrokeWidth = Settings.StrokeTextWidth; strokePaint.AntiAlias = true; // Background bounds Rect local = new Rect(); this.GetLocalVisibleRect(local); RectF bounds = new RectF(local); bounds.Top += Settings.StrokeBorderWidth/2; bounds.Left += Settings.StrokeBorderWidth/2; bounds.Right -= Settings.StrokeBorderWidth/2; bounds.Bottom -= Settings.StrokeBorderWidth/2; while(bounds.Top > Height) { bounds.Top -= Height; } while(bounds.Bottom > Height) { bounds.Bottom -= Height; } while(bounds.Left > Width) { bounds.Left -= Width; } while(bounds.Right > Width) { bounds.Right -= Width; } // Text location Rect r = new Rect(); strokePaint.GetTextBounds(Text, 0, Text.Length, r); while(r.Width() + Settings.StrokeTextWidth >= bounds.Width()) { Settings.SetTextSize(ComplexUnitType.Px, Settings.TextSize-1); textPaint.TextSize = Settings.TextSize; strokePaint.TextSize = Settings.TextSize; strokePaint.GetTextBounds(Text, 0, Text.Length, r); } float x=0, y=0; switch (Settings.Gravity) { case GravityFlags.Top: y = PaddingTop + r.Height()/2; break; case GravityFlags.Bottom: y = Height - r.Height()/2 - PaddingBottom; break; default: y = Height / 2f + r.Height() / 2f - r.Bottom; break; } switch (Settings.Gravity) { case GravityFlags.Left: x = PaddingLeft + r.Width()/2; break; case GravityFlags.Right: x = Width - r.Width()/2 - PaddingRight; break; default: x = Width/2; break; } // Draw unpressed DrawBackground(unpressedCanvas, bounds, fillBackPaint, strokeBackPaint); if(Settings.IsTextStroked) unpressedCanvas.DrawText(Text, x, y, strokePaint); unpressedCanvas.DrawText(Text, x, y, textPaint); // Change colors fillBackPaint.Color = Settings.StrokeColor; strokeBackPaint.Color = Settings.FillColor; strokePaint.Color = Settings.FillColor; textPaint.Color = Settings.IsTextStroked ? Settings.StrokeColor : Settings.FillColor; // Draw pressed DrawBackground(pressedCanvas, bounds, fillBackPaint, strokeBackPaint); if(Settings.IsTextStroked) pressedCanvas.DrawText(Text, x, y, strokePaint); pressedCanvas.DrawText(Text, x, y, textPaint); // Set images for states StateListDrawable states = new StateListDrawable(); states.AddState(new int[] {Android.Resource.Attribute.StatePressed}, new BitmapDrawable(_pressedImage)); states.AddState(new int[] {Android.Resource.Attribute.StateFocused}, new BitmapDrawable(_pressedImage)); states.AddState(new int[] {Android.Resource.Attribute.StateSelected}, new BitmapDrawable(_pressedImage)); states.AddState(new int[] { }, new BitmapDrawable(_unpressedImage)); SetBackgroundDrawable(states); strokePaint.Dispose(); textPaint.Dispose(); strokeBackPaint.Dispose(); fillBackPaint.Dispose(); unpressedCanvas.Dispose(); pressedCanvas.Dispose(); }
public Size MeasureString(string text, Font font) { using (var p = new Paint(this.paint)) using (var bounds = new Rect()) using (var fm = p.GetFontMetrics()) { p.TextSize = font.Size; p.SetTypeface(font.FontFamily.Typeface); p.SetStyle(Paint.Style.Stroke); p.GetTextBounds(text, 0, text.Length, bounds); var width = bounds.Width(); var height = -fm.Top + fm.Bottom; return new SizeF(width, height).ToSize(); } }