I am creating an application where in one of the activity I need the user to sign. With the code I have, I can sign on the whole screen, but I want that space to be limited. That is to say that there is a box on the screen and that the signature can not exceed that space. How could I do it?
This is my code:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Vista vista = new Vista(this);
setContentView(vista);
}
class Vista extends View{
float x = 0;
float y = 0;
String accion = "accion";
Path path = new Path();
public Vista(Context context){
super(context);
}
public void onDraw(Canvas canvas){
//Definir caracteristicas del pincel
Paint paint = new Paint();
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(3);//Grosor línea
paint.setColor(Color.BLACK);//Color línea
if (accion == "down")
path.moveTo(x,y);
if (accion == "move")
path.lineTo(x,y);
canvas.drawPath(path,paint);
}
public boolean onTouchEvent(MotionEvent e){
x = e.getX();
y = e.getY();
if (e.getAction() == MotionEvent.ACTION_DOWN)
accion = "down";
if (e.getAction() == MotionEvent.ACTION_MOVE)
accion = "move";
invalidate();
return true;
}
}
}