Android Stylus Ideas

Source: Internet
Author: User
Tags gety

Work needs, to do some understanding of this


General handwriting to Android canvas a little understanding should know, only need to use DrawPath can draw on the view.


The key to the stylus is to change the strokewidth of the path.


This is very big, after all, Setpaint can only set a paint, once the paint parameters changed, the entire path will change.


So we can only make a path.


We can start by opening a ArrayList (point) to record the coordinates that we have across the Surfaceview (recommended to use Surfaceview instead of view on the drawing function, after all, to lighten the pressure on the UI thread)

Point tmppoint = new Point (), Tmppoint.set ((int) event.getx (), (int) event.gety ());p Ointstack.add (tmppoint);
Let's think about what the stylus changes are, speed!

So, we have to record the speed we have across the screen, and! This speed is to correspond with our coordinate point ArrayList one by one, so that we can draw the words we want later.

Android gives us the Velocitytracker to record speed

We just need to call the Velocitytracker object when we swipe, and we can record our speed.

Case MotionEvent.ACTION_DOWN:if (Mvelocitytracker = = null) {//Retrieve a new Velocitytracker object to watch the velocity of a motion.mvelocitytracker = Velocitytracker.obtain ();} else {//Reset the velocity tracker back to its initial state.mVelocityTracker.clear ();} Add a user ' s movement to the Tracker.mVelocityTracker.addMovement (event);p Ath.moveto (Event.getx (), event.gety ()); Draw (); Break;case MotionEvent.ACTION_MOVE:mVelocityTracker.addMovement (event); Mvelocitytracker.computecurrentvelocity (+);d ouble speed = GetSpeed (velocitytrackercompat.getyvelocity ( Mvelocitytracker,event.getactionindex ()), Velocitytrackercompat.getxvelocity (Mvelocitytracker, Event.getactionindex ())); Point tmppoint = new Point (), Tmppoint.set ((int) event.getx (), (int) event.gety ());p Ointstack.add (tmppoint); Strokestack.add ((float) (speed/200));d Raw ();
Everyone must be curious about what the draw () method is.

The Draw method uses the method of refreshing Surfaceview

Note that we define the refresharea here so that we can only update the area around our fingers, which can make our UI refresh much faster!

We use Path's Qualto method to draw multiple Bezier curves with the three points of the finger (the first two points + the center control point), while the strokewidth of the path is determined by the corresponding speed.

public void Draw () {Refresharea = new Rect (Drawing_point_end.x-scale, Drawing_point_end.y-scale, Drawing_point_end.x + Scale, drawing_point_end.y+ scale); Canvas canvas = Getholder (). Lockcanvas (Refresharea); Canvas.drawcolor (Color.White); for (int i = 2; I < pointstack.size (); i++) {Path PATH = new Path ();d Rawing_point_start = Pointstack.get (i-2);d rawing_point_middle = Pointstack.get (i-1);p Ath . MoveTo (Drawing_point_start.x, Drawing_point_start.y), if (Strokestack.get (i) >) {p.setstrokewidth (+);} else { P.setstrokewidth (Strokestack.get (i)); Drawing_point_end = Pointstack.get (i);p ath.quadto (drawing_point_middle.x, Drawing_point_middle.y,drawing_point_ End.X, DRAWING_POINT_END.Y); Canvas.drawpath (path, p);} Getholder (). Unlockcanvasandpost (canvas);}
It's like a stylus.


This function I just came up with the prototype, the follow-up work to other colleagues, we also on the basis of good play it

Android Stylus Ideas

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.