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 DrawDataLabels(Canvas canvas) { var currentLabelIndex = 0; var offset = 0.0f; foreach (var point in _chartPoints) { var dataLabelValue = String.Format ("{0:P2}", datapoints [currentLabelIndex] / 100); if (point.Y / chartPixelHeight > .5) { offset = dataLabelOffset * -1; } else { offset = dataLabelOffset; } var yOffset = point.Y + offset; var fm = new Android.Graphics.Paint.FontMetrics (); _dataLabelPaint.GetFontMetrics (fm); canvas.DrawRect (point.X - _dataLabelPaint.MeasureText (dataLabelValue) / 2, yOffset - _dataLabelPaint.TextSize, point.X + _dataLabelPaint.MeasureText (dataLabelValue)/2, yOffset, _dataLabelBgPaint); canvas.DrawText (dataLabelValue, point.X, yOffset, _dataLabelPaint); canvas.DrawCircle (point.X, point.Y, 10.0f, _circlesPaint); currentLabelIndex++; } }
private void IvCanvas_Touch(object sender, TouchEventArgs e) { float x = e.Event.GetX(), y = e.Event.GetY(); if (bitmap == null) { bitmap = Bitmap.CreateBitmap(ivCanvas.Width, ivCanvas.Height, Bitmap.Config.Argb8888); canvas = new Canvas(bitmap); size = (float)Math.Sqrt(Math.Pow(ivCanvas.Width, 2) + Math.Pow(ivCanvas.Height, 2)); } if (e.Event.Action == MotionEventActions.Down) { prevX = x; prevY = y; } if (rbPencil.Checked) { switch (e.Event.Action) { case MotionEventActions.Down: canvas.DrawCircle(x, y, sbWidth.Progress, paint); break; case MotionEventActions.Move: canvas.DrawLine(prevX, prevY, x, y, paint); canvas.DrawCircle(x, y, sbWidth.Progress, paint); break; } } else if (rbHandwriting.Checked) { switch (e.Event.Action) { case MotionEventActions.Down: Handwriting.distance = 0; Handwriting.width = 0; break; case MotionEventActions.Move: float d = (float)Math.Sqrt(Math.Pow(x - prevX, 2) + Math.Pow(y - prevY, 2)), a = d / (float)Math.Pow(d, 2), w = 0, width = (float)Math.Pow(1 - d / size, 10) * paint.StrokeWidth, xpd = (x - prevX) / d, ypd = (y - prevY) / d; if (width >= Handwriting.width) { for (float f = 0; f < d; f += 4) { w = a * (float)Math.Pow(f, 2) / d * (width - Handwriting.width) + Handwriting.width; canvas.DrawBitmap(Handwriting.brush, new Rect(0, 0, 192, 192), new Rect((int)(xpd * f + prevX - w), (int)(ypd * f + prevY - w), (int)(xpd * f + prevX + w), (int)(ypd * f + prevY + w)), paint); } } else { for (float f = 0; f < d; f += 4) { w = (float)Math.Sqrt(f / a) / d * (width - Handwriting.width) + Handwriting.width; canvas.DrawBitmap(Handwriting.brush, new Rect(0, 0, 192, 192), new Rect((int)(xpd * f + prevX - w), (int)(ypd * f + prevY - w), (int)(xpd * f + prevX + w), (int)(ypd * f + prevY + w)), paint); } } Handwriting.distance = d; Handwriting.width = w; break; case MotionEventActions.Up: break; } } else if (rbFill.Checked) { if (e.Event.Action == MotionEventActions.Down) { canvas.DrawPaint(paint); } } else if (rbText.Checked) { fontMetrics = paint.GetFontMetrics(); canvas.DrawText(etText.Text, x, y - fontMetrics.Top / 2 - fontMetrics.Bottom / 2, paint); } else if (rbEraser.Checked) { switch (e.Event.Action) { case MotionEventActions.Down: canvas.DrawCircle(x, y, sbWidth.Progress, paint); break; case MotionEventActions.Move: canvas.DrawLine(prevX, prevY, x, y, paint); canvas.DrawCircle(x, y, sbWidth.Progress, paint); break; } } if (e.Event.Action == MotionEventActions.Move) { prevX = x; prevY = y; } ivCanvas.SetImageBitmap(bitmap); }