protected override void OnCreate(Bundle savedInstanceState) { base.OnCreate(savedInstanceState); // Set our view from the "main" layout resource SetContentView(Resource.Layout.activity_main); AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.SetTitle("顏色"); View view = LayoutInflater.From(this).Inflate(Resource.Layout.color, null); builder.SetView(view); adColor = builder.Create(); bColor = FindViewById <Button>(Resource.Id.b_color); etText = FindViewById <EditText>(Resource.Id.et_text); fontMetrics = paint.GetFontMetrics(); ivCanvas = FindViewById <ImageView>(Resource.Id.iv_canvas); llPaint = FindViewById <LinearLayout>(Resource.Id.ll_paint); rbEraser = FindViewById <RadioButton>(Resource.Id.rb_eraser); rbFill = FindViewById <RadioButton>(Resource.Id.rb_fill); rbHandwriting = FindViewById <RadioButton>(Resource.Id.rb_handwriting); rbPencil = FindViewById <RadioButton>(Resource.Id.rb_pencil); rbText = FindViewById <RadioButton>(Resource.Id.rb_text); sbAlpha = view.FindViewById <SeekBar>(Resource.Id.sb_alpha); sbBlue = view.FindViewById <SeekBar>(Resource.Id.sb_blue); sbGreen = view.FindViewById <SeekBar>(Resource.Id.sb_green); sbRed = view.FindViewById <SeekBar>(Resource.Id.sb_red); sbSize = FindViewById <SeekBar>(Resource.Id.sb_size); sbWidth = FindViewById <SeekBar>(Resource.Id.sb_width); adColor.DismissEvent += AdColor_DismissEvent; FindViewById <Button>(Resource.Id.b_clear).Click += BClear_Click; bColor.Click += BSolidColor_Click; FindViewById <Button>(Resource.Id.b_red).Click += BMaterialDesignColor_Click; FindViewById <Button>(Resource.Id.b_green).Click += BMaterialDesignColor_Click; FindViewById <Button>(Resource.Id.b_blue).Click += BMaterialDesignColor_Click; FindViewById <Button>(Resource.Id.b_yellow).Click += BMaterialDesignColor_Click; FindViewById <Button>(Resource.Id.b_cyan).Click += BMaterialDesignColor_Click; FindViewById <Button>(Resource.Id.b_magenta).Click += BMaterialDesignColor_Click; FindViewById <Button>(Resource.Id.b_black).Click += BMaterialDesignColor_Click; FindViewById <Button>(Resource.Id.b_white).Click += BMaterialDesignColor_Click; ivCanvas.Touch += IvCanvas_Touch; FindViewById <RadioGroup>(Resource.Id.rg_tool).CheckedChange += RgTool_CheckedChange; sbAlpha.ProgressChanged += SbColor_ProgressChanged; sbBlue.ProgressChanged += SbColor_ProgressChanged; sbGreen.ProgressChanged += SbColor_ProgressChanged; sbRed.ProgressChanged += SbColor_ProgressChanged; sbSize.ProgressChanged += SbSize_ProgressChanged; sbWidth.ProgressChanged += SbWidth_ProgressChanged; Handwriting.blackBrush = BitmapFactory.DecodeResource(Resources, Resource.Mipmap.brush); Handwriting.brush = Handwriting.blackBrush.Copy(Bitmap.Config.Argb8888, true); }
private void DrawIntoBitmap(Bitmap bm) { float x = bm.Width; float y = bm.Height; Canvas c = new Canvas (bm); Paint p = new Paint (); p.AntiAlias = true; p.Alpha = 0x80; c.DrawCircle (x / 2, y / 2, x / 2, p); p.Alpha = 0x30; p.SetXfermode (new PorterDuffXfermode (PorterDuff.Mode.Src)); p.TextSize = 60; p.TextAlign = Paint.Align.Center; Paint.FontMetrics fm = p.GetFontMetrics (); c.DrawText ("Alpha", x / 2, (y - fm.Ascent) / 2, p); }
/// <summary> /// Gets font metrics for the font in the specified paint. /// </summary> /// <param name="paint">The paint.</param> /// <param name="defaultLineHeight">Default line height.</param> /// <param name="delta">The vertical delta.</param> private void GetFontMetrics(Paint paint, out float defaultLineHeight, out float delta) { var metrics = paint.GetFontMetrics(); var ascent = -metrics.Ascent; var descent = metrics.Descent; var leading = metrics.Leading; //// http://stackoverflow.com/questions/5511830/how-does-line-spacing-work-in-core-text-and-why-is-it-different-from-nslayoutm leading = leading < 0 ? 0 : (float)Math.Floor(leading + 0.5f); var lineHeight = (float)Math.Floor(ascent + 0.5f) + (float)Math.Floor(descent + 0.5) + leading; var ascenderDelta = leading >= 0 ? 0 : (float)Math.Floor((0.2 * lineHeight) + 0.5); defaultLineHeight = lineHeight + ascenderDelta; delta = ascenderDelta - descent; }
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(); } }
public void Wrap(DroidTextLayoutBackend layout, AG.Paint paint) { var text = layout.Text; var metrics = paint.GetFontMetrics(); Baseline = -metrics.Top; LineHeight = -metrics.Ascent + metrics.Descent; LineY = -metrics.Ascent; //- (metrics.Ascent + metrics.Descent); var charWidths = new float[text.Length]; paint.GetTextWidths(text, charWidths); var iLf = text.IndexOfAny(new char[] { '\n', '\r' }); HasLineFeed = iLf >= 0; var textWidth = charWidths.Sum(); if (!HasLineFeed && (layout.Height <= 0 || layout.Height <= LineHeight) && (layout.Width <= 0 || textWidth <= layout.Width)) { LineWidth = textWidth; SingleLine(this); } else { MaxHeight = PreferedSize.Height > 0 ? PreferedSize.Height : (layout.Height <= 0 ? float.MaxValue : layout.Height); MaxWidth = PreferedSize.Width > 0 ? PreferedSize.Width : (layout.Width <= 0 ? textWidth : layout.Width); CursorPos = 0; LineStart = 0; while (LineY <= MaxHeight && CursorPos < text.Length) { LineWidth = 0f; var whitePosY = -1f; var whitepos = -1; var newLine = false; while (CursorPos < text.Length) { newLine = text [CursorPos] == '\n'; if (text [CursorPos] == '\r') { CursorPos++; continue; } if (newLine) { break; } if (char.IsWhiteSpace(text [CursorPos])) { whitepos = CursorPos; whitePosY = LineWidth; } LineWidth += charWidths [CursorPos]; if (LineWidth > MaxWidth) { if (whitepos > 0) { CursorPos = whitepos; LineWidth = whitePosY; } break; } CursorPos++; } Action line = () => { MultiLine(this); LineY += LineHeight; }; line(); CursorPos++; LineStart = CursorPos; if (newLine && CursorPos == text.Length) { line(); } } } }