Android to implement brush effect in canvas (i)--pen

Source: Internet
Author: User

Title:

The company asked to do a graffiti board, to have pens, brushes and other brush effects, online search a lot, but the effect is not good, decided to study under their own. Don't say much nonsense, get to the point.

First, the Seibel curve is clear, in the process of drawing a curve is a vector.

Second, the curve in the process of painting to have a thickness change (is simple to achieve)

Thirdly, two two vectors a1b1 and a2b2 are also drawn on both sides of the original vector. Such as:



, the length of the segment A1A2 has a F1 (x) relationship with the brush movement speed. The black curve is the effect that vector AB draws out through Seibel.

The following describes the logic of the brush implementation:

In order to achieve the effect of the brush, in fact, we have drawn a total of three bell curves, respectively, according to the Vector a1b1,ab,a2b2 (see). A1, A2 is obtained through the AB vector, a point is the midpoint of the vector a1a2, ab perpendicular to the a1b1.

After many tests, I found that calculating the length of the A1A2 is a key problem, and the result of a simple linear function shows that it is very bad. Personal recommendation: Use the change of the normal function or cosine function to obtain. The code is as follows:

<span style= "White-space:pre" > </span>private final float key_paint_width=2.5f;//when the speed is very slow, draw the curve is how many times the width of the brush, This value is tested without gaps or small.
/**
* Convert the velocity to 2.5 times times the width of the brush according to the variable cosine function y=0.5*[cos (X*PI) +1] (0<x<1).
* x<0, Width is maximum width, 2.5 times times Brush width
* 0<x<1, according to the above function, to convert
* X>1, according to the function y=width/x, when y<0.2, take the minimum value 0.2
* @param delaytime
* @return
*/
Private float ControlPaint (double v) {
cosine function
Y=0.5*[cos (X*PI) +1]
float result=key_paint_width*paintsize;
if (v<0) {
}else if (v<1) {
result= (float) (0.5*paintsize*key_paint_width* (Math.Cos (V*MATH.PI) +1));
}else{
result= ((float) (paintsize/v>0.1?paintsize/v:0.1));
}
return result;
}

The conversion function needs to be converted according to the appropriate value of the brush speed measured by itself, so there is not much to repeat.

Achieve writing effects such as (test on a tablet (resolution 1280*800)):

Original address: http://blog.csdn.net/sbduxing/article/details/39343465

Android to implement brush effect in canvas (i)--pen

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.