コード例 #1
0
        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));
        }
コード例 #2
0
 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;
 }
コード例 #3
0
ファイル: GLView1.cs プロジェクト: Adameg/mobile-samples
		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);
			}
		}
コード例 #4
0
 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;
 }
コード例 #5
0
 /**
  * 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;
 }
コード例 #6
0
 /**
  * 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;
 }
コード例 #7
0
        public void DrawText(double x, double y, double width, double height, string text, Model.UI.Color textColor, Model.UI.TextAlignment alignment)
        {
            Paint paint = new Paint();
            switch (alignment)
            {
                case Model.UI.TextAlignment.Left:
                    paint.TextAlign = Android.Graphics.Paint.Align.Left;
                    break;
                case Model.UI.TextAlignment.Right:
                    paint.TextAlign = Android.Graphics.Paint.Align.Right;
                    break;
                case Model.UI.TextAlignment.Centered:
                    paint.TextAlign = Android.Graphics.Paint.Align.Center;
                    break;
                default:
                    break;
            }
            
            string[] lines = text.Split('\n');
            double minFontSize = height / lines.Length;
            paint.AntiAlias = true;
            paint.Color = new Android.Graphics.Color((byte)textColor.Red, (byte)textColor.Green, (byte)textColor.Blue, (byte)(textColor.Alpha * 255));
            foreach (string line in lines)
            {
                paint.TextSize = (float)(height/ lines.Length);
                if (paint.MeasureText(line) > width)
                {
                    minFontSize = Math.Min(minFontSize, width / paint.MeasureText(line) * (height / lines.Length));
                }
            }
            paint.TextSize = (float)minFontSize;
            for (int index = 0; index < lines.Length; index++)
            {
                canvas.DrawText(lines[index], (float)(x + (alignment == Model.UI.TextAlignment.Centered ? width / 2 : 0)), (float)(y + height - paint.Descent() - ((height / lines.Length) * (0.5 - index + lines.Length - 1) - paint.TextSize / 2)), paint);
            }

        }