Example of creating an Android Brush program (with mirror effect)

Source: Internet
Author: User

First:



The key is to use the Mpath.quadto method when detecting a finger movement, as explained by the Android SDK:

Add a quadratic Bézier from the last point, approaching control Point (x1,y1), and ending at (x2,y2). If no MoveTo () call had been made for this contour, the first point was automatically set to (0,0).

Chinese is linked with Bezier curve (x1,y1), (X2,y2) These two points, assuming that there is no moveto () This method call, the first point of the tacit feel (0,0)


The Android drawing is using the canvas API, such as drawing a solid rectangle that can be used to rewrite a view of the OnDraw ():

<span style= "White-space:pre" ></span>rect rect = new rect (100,100,500,500); Mpaint.setstrokewidth (5); Sets the thickness of the brush mpaint.setcolor (color.red); Sets the color of the Brush Mpaint.setstyle (Style.fill); Fills the entire graphic mpaint.setantialias (true); Anti-aliasing effect canvas.drawrect (rect, mpaint);

For the detection of finger movement, we can use ontouchevent to achieve:

Private float MX, my;private float Moppositex, moppositey;private static final float touch_tolerance = 4; When the finger moves more than 4 o'clock we go to set Path@overridepublic boolean ontouchevent (Motionevent event) {float x = Event.getx (); Float y = event.ge TY (); switch (Event.getaction ()) {case MotionEvent.ACTION_DOWN:touch_start (x, y); if (Ismirrordraw) {Touch_opposite_ Start (x, y);} Invalidate (); Break;case MotionEvent.ACTION_MOVE:touch_move (x, y), if (Ismirrordraw) {touch_opposite_move (x, y);} Invalidate (); Break;case MotionEvent.ACTION_UP:touch_up (); if (Ismirrordraw) {touch_opposite_up ();} Invalidate (); break;} return true;} private void Touch_start (float x, float y) {mpath.reset (); Mpath.moveto (x, y); MX = X;my = y;} private void Touch_move (float x, float y) {float dx = math.abs (x-mx); float dy = Math.Abs (y-my); if (DX >= touch_tole RANCE | | Dy >= touch_tolerance) {mpath.quadto (MX, my, (x + MX)/2, (y + MY)/2), mx = X;my = y;}} private void Touch_up () {Mpath.lineto (MX, MY); Mcanvas.drawpath (MPath, Mpaint); Mpath.reset ();}private void Touch_opposite_up () {Moppositepath.lineto (Moppositex, MY); Mcanvas.drawpath (Moppositepath, mOppoPaint); Moppositepath.reset ();} private void Touch_opposite_move (float x, float y) {float Oppositex = oppositedrawactivity.screenwidth-x;//<span sty Le= "Font-family:arial, Helvetica, Sans-serif;" >oppositedrawactivity.screenwidth is the screen width </span>float dx = math.abs (Oppositex-moppositex); float dy = Math.abs ( Y-my), if (dx >= touch_tolerance | | dy >= touch_tolerance) {moppositepath.quadto (Moppositex, MY, (Moppositex + oppo Sitex)/2, (y + MY)/2); Moppositex = Oppositex;my = y;}} private void Touch_opposite_start (float x, float y) {moppositepath.reset (); Float Oppositex = Oppositedrawactivity.screenwidth-x;moppositepath.moveto (Oppositex, y); Moppositex = Oppositex;}

Then rewrite OnDraw ():

@Overrideprotected void OnDraw (canvas canvas) {canvas.drawcolor (0xFFAAAAAA); Canvas.drawbitmap (mbitmap, 0, 0, Mbitmappaint); Canvas.drawpath (MPath, Mpaint); if (Ismirrordraw) {Canvas.drawpath (Moppositepath, Moppopaint);}}

The code can be downloaded in http://download.csdn.net/detail/baidu_nod/7572549

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.