Android handwriting ideas

Source: Internet
Author: User
Tags gety

Android handwriting ideas

Work needs, and I have some knowledge about this.

 

Generally, you should know that you only need to use drawPath to draw images on The view.

 

The key to writing a pen is to change the strokeWidth of the path.

 

This is too big. After all, setPaint can only set one paint. Once the paint parameter is changed, the entire path will change.

 

Therefore, we can only find another path.

 

We can first open an arraylist (Point) to record the coordinates on surfaceview (we recommend using surfaceview instead of view in the painting function, after all, it can reduce the pressure on the ui thread ).

 

Point tmpPoint = new Point();tmpPoint.set((int) event.getX(), (int) event.getY());pointStack.add(tmpPoint);
Let's think about the speed of the PEN change!

 

Therefore, we must record the speed at which we have crossed the screen, and! This speed must correspond to our coordinate point arraylist so that we can draw the words we want later.

Android provides VelocityTracker for us to record the speed.

We only need to call the VelocityTracker object when sliding to 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);path.moveTo(event.getX(), event.getY());draw();break;case MotionEvent.ACTION_MOVE:mVelocityTracker.addMovement(event);mVelocityTracker.computeCurrentVelocity(1000);double speed = getSpeed(VelocityTrackerCompat.getYVelocity(mVelocityTracker,event.getActionIndex()),VelocityTrackerCompat.getXVelocity(mVelocityTracker,event.getActionIndex()));Point tmpPoint = new Point();tmpPoint.set((int) event.getX(), (int) event.getY());pointStack.add(tmpPoint);strokeStack.add((float) (speed / 200));draw();break;
Everyone must be curious. What is the draw () method?

 

The draw method is used to refresh surfaceview.

Note: We have defined refreshArea here to allow us to update only the areas near our fingers, and to refresh our ui much faster!

We use the qualTo method of path to draw multiple besels through the three points (the first and last two points + the middle Control Points) crossed by fingers, the strokeWidth of a 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();drawing_point_start = pointStack.get(i - 2);drawing_point_middle = pointStack.get(i - 1);path.moveTo(drawing_point_start.x, drawing_point_start.y);if (strokeStack.get(i) > 25) {p.setStrokeWidth(25);} else {p.setStrokeWidth(strokeStack.get(i));}drawing_point_end = pointStack.get(i);path.quadTo(drawing_point_middle.x, drawing_point_middle.y,drawing_point_end.x, drawing_point_end.y);canvas.drawPath(path, p);}getHolder().unlockCanvasAndPost(canvas);}
Just like a pen.

 

 

I just came up with a prototype for this feature. I will hand it over to other colleagues for you to make full use of it.
 

Related Article

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.