Skip to content

Instantly share code, notes, and snippets.

@has-taiar
Last active October 31, 2017 02:40
Show Gist options
  • Save has-taiar/9968787 to your computer and use it in GitHub Desktop.
Save has-taiar/9968787 to your computer and use it in GitHub Desktop.
Android Signature Capture View
using System;
using Android.Content;
using Android.Graphics;
using Android.Views;
namespace MyApp.Views
{
public class CaptureSignatureView : View
{
public CaptureSignatureView(Context c, SignatureData signatureData) : base(c)
{
SignatureData = signatureData;
_Path = new Path();
_BitmapPaint = new Paint(PaintFlags.Dither);
_paint = new Paint
{
AntiAlias = true,
Dither = true,
Color = Color.Argb(255, 0, 0, 0)
};
_paint.SetStyle(Paint.Style.Stroke);
_paint.StrokeJoin = Paint.Join.Round;
_paint.StrokeCap = Paint.Cap.Round;
_paint.StrokeWidth = 8;
}
protected override void OnSizeChanged(int w, int h, int oldw, int oldh)
{
base.OnSizeChanged(w, h, oldw, oldh);
_Bitmap = Bitmap.CreateBitmap(w, (h > 0 ? h : ((View)this.Parent).Height), Bitmap.Config.Argb8888);
_Canvas = new Canvas(_Bitmap);
}
protected override void OnDraw(Canvas canvas)
{
canvas.DrawColor(Color.White);
canvas.DrawBitmap(_Bitmap, 0, 0, _BitmapPaint);
canvas.DrawPath(_Path, _paint);
}
private float _mX, _mY;
private const float TouchTolerance = 4;
private void TouchStart(float x, float y)
{
_Path.Reset();
_Path.MoveTo(x, y);
_mX = x;
_mY = y;
SignatureData.AddPoint(SignatureState.Start, (int)x, (int)y);
}
private void TouchMove(float x, float y)
{
float dx = Math.Abs(x - _mX);
float dy = Math.Abs(y - _mY);
if (dx >= TouchTolerance || dy >= TouchTolerance)
{
_Path.QuadTo(_mX, _mY, (x + _mX) / 2, (y + _mY) / 2);
SignatureData.AddPoint(SignatureState.Move, (int)x, (int)y);
_mX = x;
_mY = y;
}
}
private void TouchUp()
{
if (!_Path.IsEmpty)
{
_Path.LineTo(_mX, _mY);
_Canvas.DrawPath(_Path, _paint);
}
else
{
_Canvas.DrawPoint(_mX, _mY, _paint);
}
SignatureData.AddPoint(SignatureState.End, (int)_mX, (int)_mY);
_Path.Reset();
}
public override bool OnTouchEvent(MotionEvent e)
{
var x = e.GetX();
var y = e.GetY();
switch (e.Action)
{
case MotionEventActions.Down:
TouchStart(x, y);
Invalidate();
break;
case MotionEventActions.Move:
TouchMove(x, y);
Invalidate();
break;
case MotionEventActions.Up:
TouchUp();
Invalidate();
break;
}
return true;
}
public void ClearCanvas()
{
_Canvas.DrawColor(Color.White);
Invalidate();
}
public Bitmap CanvasBitmap()
{
return _Bitmap;
}
public void Clear()
{
ClearCanvas();
SignatureData = new SignatureData();
}
#region Implementation
private Bitmap _Bitmap;
private Canvas _Canvas;
private readonly Path _Path;
private readonly Paint _BitmapPaint;
private readonly Paint _paint;
public SignatureData SignatureData;
#endregion
}
}
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
namespace MyApp.Classes
{
public enum SignatureState
{
Start,
Move,
End
}
public class SignatureData
{
public List<List<Point>> Paths { get { return _Paths; } }
public event EventHandler PointAdded;
public Point LastPoint()
{
if (currentPath != null && currentPath.Count > 0)
{
return currentPath.Last();
}
return new Point(0, 0);
}
public void Clear()
{
_Paths = new List<List<Point>>();
currentPath = new List<Point>();
}
public void AddPoint(SignatureState state, int x, int y)
{
if (state == SignatureState.Start)
{
currentPath = new List<Point>();
}
if (x != 0 && y != 0)
{
currentPath.Add(new Point(x, y));
}
if (state == SignatureState.End)
{
if (currentPath != null)
{
_Paths.Add(currentPath);
}
}
OnPointAdded(EventArgs.Empty);
}
public int Length
{
get { return _Paths.Count; }
}
#region Implementation
protected void OnPointAdded(EventArgs e)
{
if (PointAdded != null)
{
PointAdded(this, e);
}
}
private List<Point> currentPath = new List<Point>();
private List<List<Point>> _Paths = new List<List<Point>>();
#endregion
}
}
@tuyenpx
Copy link

tuyenpx commented Oct 31, 2017

How to implement this code, bro?

@namhv
Copy link

namhv commented Oct 31, 2017

How to implement in Android native project?

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