Skip to content

Instantly share code, notes, and snippets.

@furi2
Created February 19, 2014 06:28
Show Gist options
  • Save furi2/9087034 to your computer and use it in GitHub Desktop.
Save furi2/9087034 to your computer and use it in GitHub Desktop.
PaintView, For Hand Drawing on a Drawable Image (Xamarin)
using System;
using Android.App;
using Android.Content;
using Android.Views;
using Android.Widget;
using Android.Graphics;
using Android.Util;
namespace ArborTrac.Droid.UI.Controls
{
public class PaintView : ImageView
{
private Paint _Paint = new Paint();
private const int THICKNESS = 10;
private PointF _StartPt = new PointF();
private PointF _EndPt = new PointF();
private Bitmap _Bmp = null;
private Canvas _Canvas = null;
public PaintView(Context context, IAttributeSet attrs)
: base(context, attrs)
{
this.Initialize();
}
void Initialize()
{
_Paint.Color = new Color(255, 0, 0);
_Paint.StrokeWidth = THICKNESS;
_Paint.StrokeCap = Paint.Cap.Round;
_Bmp = BitmapFactory.DecodeResource(Resources, Resource.Drawable.tree_trunk);
_Bmp = _Bmp.Copy(Bitmap.Config.Argb8888, true);
}
override protected void OnMeasure(int wSpec, int hSpec)
{
base.OnMeasure(wSpec, hSpec);
int w = MeasureSpec.GetSize(wSpec);
int h = MeasureSpec.GetSize(hSpec);
_Bmp = Bitmap.CreateScaledBitmap(_Bmp, w, h, false);
_Canvas = new Canvas(_Bmp);
this.SetImageBitmap(_Bmp);
}
override public bool OnTouchEvent(MotionEvent e)
{
switch (e.Action)
{
case MotionEventActions.Down:
_StartPt.Set(e.GetX() - 1, e.GetY() - 1);// for just a tapping
DrawLine(e);
break;
case MotionEventActions.Move:
DrawLine(e);
break;
case MotionEventActions.Up:
DrawLine(e);
break;
}
return true;
}
private void DrawLine(MotionEvent e)
{
_EndPt.Set(e.GetX(), e.GetY());
_Canvas.DrawLine(_StartPt.X, _StartPt.Y, _EndPt.X, _EndPt.Y, _Paint);
_StartPt.Set(_EndPt);
Invalidate();
}
}
}
@furi2
Copy link
Author

furi2 commented Feb 19, 2014

The referenced article:
http://dixq.net/Android/s01_02.html

@Kajan83
Copy link

Kajan83 commented Aug 9, 2017

image

@Kajan83
Copy link

Kajan83 commented Aug 9, 2017

i need to develop the xamarin android code please help me if anyone has done similar

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment