Android ApiDemos example resolution (66): Graphics-& gt; FingerPaint

Source: Internet
Author: User

In this example, FingerPaint allows you to use your fingers to graffiti on the screen. It supports color selection, multiple painting modes, and part of the drawing can be erased.

In this example, Path is used to record the draw Path of the finger on the screen through the onTouchEvent event.

[Java]
@ Override
Public boolean onTouchEvent (MotionEvent event ){
Float x = event. getX ();
Float y = event. getY (); www.2cto.com

Switch (event. getAction ()){
Case MotionEvent. ACTION_DOWN:
Touch_start (x, y );
Invalidate ();
Break;
Case MotionEvent. ACTION_MOVE:
Touch_move (x, y );
Invalidate ();
Break;
Case MotionEvent. ACTION_UP:
Touch_up ();
Invalidate ();
Break;
}
Return true;
}

@ Override
Public boolean onTouchEvent (MotionEvent event ){
Float x = event. getX ();
Float y = event. getY ();
 
Switch (event. getAction ()){
Case MotionEvent. ACTION_DOWN:
Touch_start (x, y );
Invalidate ();
Break;
Case MotionEvent. ACTION_MOVE:
Touch_move (x, y );
Invalidate ();
Break;
Case MotionEvent. ACTION_UP:
Touch_up ();
Invalidate ();
Break;
}
Return true;
} The Path class can use moveTo, quadTo, and lineTo to record the Path of the finger on the screen.

In this example, we also use MaskFilter, which can perform some Transformation (edge effect) on the Alaph-Channel of the image. Android contains the following maskfilters:

BlurMaskFilter specifies a blur style and radius to process the Paint edge.
EmbossMaskFilter specifies the direction of the light source and the ambient light intensity to add relief effects.
Among them, EmbossMaskFilter emboss = new EmbossMaskFilter (direction, light, specular, blur );

Direction defines the direction of the light source, light defines the brightness of the light, specular defines the level of the light, and blur.

To enable erasure, use the Clear mode of PorterDuff. For details, refer to Android ApiDemos example resolution (59): Graphics-> ColorFilters.

[Java]
MPaint. setXfermode (new porterduduxfermode (
PorterDuff. Mode. CLEAR ));

MPaint. setXfermode (new porterduduxfermode (
PorterDuff. Mode. CLEAR); ColorPickerDialog is the color selection dialog box. The color selection result is obtained by implementing the ColorPickerDialog. OnColorChangedListener callback function:

[Java] view plaincopyprint?
Public class FingerPaint extends GraphicsActivity
Implements ColorPickerDialog. OnColorChangedListener {

....
Public void colorChanged (int color ){
MPaint. setColor (color );
}


New ColorPickerDialog (this, this, mPaint. getColor (). show ();

...
}

Public class FingerPaint extends GraphicsActivity
Implements ColorPickerDialog. OnColorChangedListener {
 
....
Public void colorChanged (int color ){
MPaint. setColor (color );
}
 
 
New ColorPickerDialog (this, this, mPaint. getColor (). show ();
 
...
}
 

 

The following figure shows the running effect on the mobile phone:

 

 
Author: mapdigit
 

 

 

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.