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