Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8793071
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T23:04:34+00:00 2026-06-13T23:04:34+00:00

I want to make a class which captures a human signature for my Android

  • 0

I want to make a class which captures a human signature for my Android app. I have this code so far:

public class DrawView extends View implements OnTouchListener {
    private static final String TAG = "DrawView";

    List<Point> points = new ArrayList<Point>();
    Paint paint = new Paint();
    long oldTime = 0;

    public DrawView(Context context) {
        super(context);
        setFocusable(true);
        setFocusableInTouchMode(true);

        this.setOnTouchListener(this);

        // paint.setColor(Color.BLACK);
        // paint.setAntiAlias(true);

        paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(2);
        paint.setColor(Color.BLACK);
    }

    @Override
    public void onDraw(Canvas canvas) {
        Path path = new Path();

        if (points.size() > 1) {
            for (int i = points.size() - 2; i < points.size(); i++) {
                if (i >= 0) {
                    Point point = points.get(i);

                    if (i == 0) {
                        Point next = points.get(i + 1);
                        point.dx = ((next.x - point.x) / 3);
                        point.dy = ((next.y - point.y) / 3);
                    } else if (i == points.size() - 1) {
                        Point prev = points.get(i - 1);
                        point.dx = ((point.x - prev.x) / 3);
                        point.dy = ((point.y - prev.y) / 3);
                    } else {
                        Point next = points.get(i + 1);
                        Point prev = points.get(i - 1);
                        point.dx = ((next.x - prev.x) / 3);
                        point.dy = ((next.y - prev.y) / 3);
                    }
                }
            }
        }

        boolean first = true;
        for (int i = 0; i < points.size(); i++) {
            Point point = points.get(i);
            if (first) {
                first = false;
                path.moveTo(point.x, point.y);
            } else {

                Point prev = points.get(i - 1);
                path.cubicTo(prev.x + prev.dx, prev.y + prev.dy, point.x
                        - point.dx, point.y - point.dy, point.x, point.y);

            }
        }
        canvas.drawPath(path, paint);

    }

    public boolean onTouch(View view, MotionEvent event) {
        if (event.getAction() != MotionEvent.ACTION_UP) {
            Point point = new Point();
            point.x = event.getX();
            point.y = event.getY();
            points.add(point);
            invalidate();
            return true;
        }
        return super.onTouchEvent(event);

    }

    public boolean checkTime() {
        //Check if there was an input 400 ms ago
        long newTime = System.currentTimeMillis();
        long diffirence = newTime - oldTime;
        if (oldTime == 0) {
            oldTime = System.currentTimeMillis();
            return true;
        } else if (diffirence <= 400) {
            oldTime = System.currentTimeMillis();
            return true;
        }
        return false;
    }
}

class Point {
    float x, y;
    float dx, dy;

    @Override
    public String toString() {
        return x + ", " + y;
    }
}

The problem is that the line will connect to the latest point when I start drawing again, even when I stop drawing for a while. This is of course not very useful for capturing human signatures, that’s why I created the checkTime method. If you stop drawing for 400ms it returns false, when you start drawing again it will start a new line which is not connected to the old line. I only just don’t know how I can implement my method, I tried a lot but the line keeps connecting to the latest point, maybe someone can help me.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T23:04:35+00:00Added an answer on June 13, 2026 at 11:04 pm

    I used this post from Lars Vogel, but many thanks to Marc Van Daele!

    This is code wich paints the View:

    import android.content.Context;
    import android.graphics.Canvas;
    import android.graphics.Color;
    import android.graphics.Paint;
    import android.graphics.Path;
    import android.util.AttributeSet;
    import android.view.MotionEvent;
    import android.view.View;
    
    public class SingleTouchEventView extends View {
      private Paint paint = new Paint();
      private Path path = new Path();
    
      public SingleTouchEventView(Context context, AttributeSet attrs) {
        super(context, attrs);
    
        paint.setAntiAlias(true);
        paint.setStrokeWidth(6f);
        paint.setColor(Color.BLACK);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeJoin(Paint.Join.ROUND);
      }
    
      @Override
      protected void onDraw(Canvas canvas) {
        canvas.drawPath(path, paint);
      }
    
      @Override
      public boolean onTouchEvent(MotionEvent event) {
        float eventX = event.getX();
        float eventY = event.getY();
    
        switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
          path.moveTo(eventX, eventY);
          return true;
        case MotionEvent.ACTION_MOVE:
          path.lineTo(eventX, eventY);
          break;
        case MotionEvent.ACTION_UP:
          // nothing to do
          break;
        default:
          return false;
        }
    
        // Schedules a repaint.
        invalidate();
        return true;
      }
    } 
    

    You have to call this activity to start paiting:

    import android.app.Activity;
    import android.os.Bundle;
    
    public class SingleTouchActivity extends Activity {
    
    /** Called when the activity is first created. */
    
      @Override
      public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(new SingleTouchEventView(this, null));
      }
    } 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to make a filter class in which I have three methods: name
I have a class which looks approximately like this: class MeshClass { public: Anchor
I want to make a piece of code which will store a class reference
I have made a class Location which looks like this: package TruckingCompany; public class
I have style sheet with a class name changebackgroundcolor i want make change in
I have a member variable that in a class that I want to make
Questions - (Relative Code Below) I have a custom class which is a BindableDictionary().
I'm having a tough time with this one, I have a class which populates
I am create a Class which have a UIWebView property. I want to set
I want make a SharedInterrupt class(B) which will serve in series many objects in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.