Reputation: 209
im trying to have a SignatureView to capture my user's signature after they fill up a form, but my codes are not making it draw. i found this drawing codes on youtube and tried them but it does not draw anything, although the scroll lock is working. the following is the code to
the xml for the signatureview
<SignatureView
android:background="#FFFFFF"
android:layout_width="match_parent"
android:layout_height="400dp"
android:id="@+id/signatureView"/>
the code for the signatureview to enable drawing
public class SignatureView extends View {
public static int BRUSH_SIZE = 5;
public static final int DEFAULT_COLOR = Color.BLACK;
public static final int DEFAUL_BG_COLOR = Color.WHITE;
private static final float TOUCH_TOLERANCE = 4;
private float mX, mY;
private Path mPath;
private Paint mPaint;
private ArrayList<FingerPath> paths = new ArrayList<>();
private int currentColor;
private int backgroundColor = DEFAUL_BG_COLOR;
private int strokewidth;
private Bitmap mBitmap;
private Canvas mCanvas;
private Paint mBitmapPaint = new Paint(Paint.DITHER_FLAG);
public SignatureView(Context context){
this(context, null);
}
public SignatureView(Context context, AttributeSet attrs){
super(context, attrs);
mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setDither(true);
mPaint.setColor(DEFAULT_COLOR);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeJoin(Paint.Join.ROUND);
mPaint.setStrokeCap(Paint.Cap.ROUND);
mPaint.setXfermode(null);
mPaint.setAlpha(0xff);
}
public void init(DisplayMetrics metrics){
int height = metrics.heightPixels;
int width = metrics.widthPixels;
mBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
mCanvas = new Canvas(mBitmap);
currentColor = DEFAULT_COLOR;
strokewidth = BRUSH_SIZE;
}
protected void onDraw(Canvas canvas){
canvas.save();
mCanvas.drawColor(backgroundColor);
for (FingerPath fp : paths){
mPaint.setColor(fp.color);
mPaint.setStrokeWidth(fp.strokewidth);
}
canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);
canvas.restore();
}
private void touchStart(float x, float y){
mPath = new Path();
FingerPath fp = new FingerPath(currentColor, strokewidth, mPath);
paths.add(fp);
mPath.reset();
mPath.moveTo(x, y);
mX = x;
mY = y;
}
private void touchMove(float x, float y){
float dx = Math.abs(x - mX);
float dy = Math.abs(y - mY);
if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE){
mPath.quadTo(mX, mY, (x + mX) / 2, (y + mY) /2);
mX = x;
mY = y;
}
}
private void touchUp(){
mPath.lineTo(mX, mY);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
float x = event.getX();
float y = event.getY();
getParent().requestDisallowInterceptTouchEvent(true);
switch(event.getAction()){
case MotionEvent.ACTION_DOWN :
touchStart(x, y);
invalidate();
break;
case MotionEvent.ACTION_MOVE :
touchMove(x, y);
invalidate();
break;
case MotionEvent.ACTION_UP :
touchUp();
invalidate();
break;
}
return true;
}
the fingerpath class
public class FingerPath {
public int color;
public int strokewidth;
public Path path;
public FingerPath(int color, int strokewidth, Path path){
this.color = color;
this.strokewidth = strokewidth;
this.path = path;
}
}
the java class for the page
public class Application_applicant extends AppCompatActivity {
private SignatureView sv;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_app_applicant);
sv = (SignatureView)findViewById(R.id.signatureView);
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
sv.init(metrics);
}
}
Upvotes: 1
Views: 3734
Reputation: 2268
Here's a few libraries I found online for this: https://bitbucket.org/warwick/fingerpaintex/src/master/ https://github.com/AgnaldoNP/FingerSignView https://www.thecrazyprogrammer.com/2017/02/android-signature-capture-example.html https://github.com/warting/android-signaturepad
Upvotes: 1
Reputation: 209
Upvotes: 1
Reputation: 1168
Please have a look MyView
class in the APIs demo of Android from this link: https://android.googlesource.com/platform/development/+/master/samples/ApiDemos/src/com/example/android/apis/graphics/FingerPaint.java
You can find some demos in this project. It's very useful
Upvotes: 0