public Task <Bitmap> LoadImageAsync( ImageSource imagesource, Context context, CancellationToken cancelationToken = default(CancellationToken)) { Bitmap image = null; var fontsource = imagesource as FontImageSource; if (fontsource != null) { using var paint = new Paint { TextSize = TypedValue.ApplyDimension(ComplexUnitType.Dip, (float)fontsource.Size, context.Resources.DisplayMetrics), Color = (fontsource.Color != null ? fontsource.Color : Colors.White).ToAndroid(), TextAlign = Paint.Align.Left, AntiAlias = true, }; paint.SetTypeface(fontsource.FontFamily.ToTypeface()); var width = (int)(paint.MeasureText(fontsource.Glyph) + .5f); var baseline = (int)(-paint.Ascent() + .5f); var height = (int)(baseline + paint.Descent() + .5f); image = Bitmap.CreateBitmap(width, height, Bitmap.Config.Argb8888); using var canvas = new Canvas(image); canvas.DrawText(fontsource.Glyph, 0, baseline, paint); } return(Task.FromResult(image)); }
public AndroidFontMetrics(Paint paint) { _widths = new float[NumWidths]; paint.GetTextWidths (_chars, 0, NumWidths, _widths); Ascent = (int)(Math.Abs (paint.Ascent ()) + 0.5f); Descent = (int)(paint.Descent ()/2 + 0.5f); Height = Ascent; }
void CreateBitmapData (string str, out byte[] bitmapData, out int width, out int height) { Paint paint = new Paint (); paint.TextSize = 128; paint.TextAlign = Paint.Align.Left; paint.SetTypeface (Typeface.Default); width = height = 256; float textWidth = paint.MeasureText (str); using (Bitmap bitmap = Bitmap.CreateBitmap (width, height, Bitmap.Config.Argb8888)) { Canvas canvas = new Canvas (bitmap); paint.Color = str != " " ? Color.White : Color.LightGray; canvas.DrawRect (new Rect (0, 0, width, height), paint); paint.Color = Color.Black; canvas.DrawText (str, (256 - textWidth) / 2f, (256 - paint.Descent () - paint.Ascent ()) / 2f, paint); bitmapData = new byte [width * height * 4]; Java.Nio.ByteBuffer buffer = Java.Nio.ByteBuffer.Allocate (bitmapData.Length); bitmap.CopyPixelsToBuffer (buffer); buffer.Rewind (); buffer.Get (bitmapData, 0, bitmapData.Length); } }
private Rect CalcBounds(int index, Paint paint) { var bounds = new Rect(); var title = GetTitle(index); bounds.Right = (int)paint.MeasureText(title); bounds.Bottom = (int)(paint.Descent() - paint.Ascent()); return bounds; }
/** * Calculate the bounds for a view's title * * @param index * @param paint * @return */ private RectF CalcBounds(int index, Paint paint) { //Calculate the text bounds RectF bounds = new RectF(); bounds.Right = paint.MeasureText(mTitleProvider.GetTitle(index)); bounds.Bottom = paint.Descent() - paint.Ascent(); return bounds; }
/** * Calculate the bounds for a view's title * * @param index * @param paint * @return */ private Rect calcBounds(int index, Paint paint) { //Calculate the text bounds Rect bounds = new Rect(); string title = getTitle(index); bounds.Right = (int)paint.MeasureText(title, 0, title.Length); bounds.Bottom = (int)(paint.Descent() - paint.Ascent()); return bounds; }