Notepad (10) for android projects ----- set the paint brush size and color with the handwriting Function

Source: Internet
Author: User

Notepad (10) for android projects ----- set the paint brush size and color with the handwriting Function

In the previous section, all the functions of the canvas are implemented, including setting the paint brush size, setting the paint brush color, and eraser. You can also add the handwriting size and color setting functions, this section allows you to adjust handwriting size and color.

First look at the figure:

In fact, the functions of handwriting and drawing board are similar. Simply reuse the previous code. The principle is as follows:

1. Set the paint brush size:

1) initialize the paint brush.

2) set the paint brush size to the selected size.

3) Use a variable to remember the size of the current paint brush. It is used to keep the size of the previously set paint brush after other operations.

2. Set the paint brush color:

1) initialize the paint brush.

2) set the paint brush color to the selected color.

3) Use a variable to remember the color of the current paint brush. It is used to maintain the color of the previously set paint brush after other operations.

The main code (added in TouchView) is as follows:

private Paint mPaint;private int currentColor = Color.RED;private int currentSize = 5;
// Set the Paint brush style public void setPaintStyle () {mPaint = new Paint (); mPaint. setAntiAlias (true); mPaint. setDither (true); mPaint. setStyle (Paint. style. STROKE); mPaint. setStrokeJoin (Paint. join. ROUND); mPaint. setStrokeCap (Paint. cap. ROUND); mPaint. setStrokeWidth (currentSize); mPaint. setColor (currentColor );}

Set the paint brush size:

// Set the size of the paint brush public void selectHandWritetSize (int which) {int size = Integer. parseInt (this. getResources (). getStringArray (R. array. paintsize) [which]); currentSize = size; setPaintStyle ();}

Set the paint color:

// Set the paint brush color public void selectHandWriteColor (int which) {currentColor = paintColor [which]; setPaintStyle ();}


After these methods are added to the custom View, these methods are called in the Activity, as shown in the preceding section (Add the following code in HandWriteActivity. java ):

// Paint brush size case 0: showPaintSizeDialog (view); break; // color case 1: showPaintColorDialog (view); break;

Click these two buttons to bring up a dialog box to select the size and color of the paint brush.

ShowPaintSizeDialog (view). The select brush size dialog box is displayed, as shown below:

// Public void showPaintSizeDialog (View parent) {AlertDialog. builder alertDialogBuilder = new AlertDialog. builder (this, R. style. custom_dialog); alertDialogBuilder. setTitle ("select the paint brush size:"); alertDialogBuilder. setSingleChoiceItems (R. array. paintsize, select_handwrite_size_index, new DialogInterface. onClickListener () {@ Overridepublic void onClick (DialogInterface dialog, int which) {select_handwrite_size_index = which; touchView. selectHandWritetSize (which); dialog. dismiss () ;}}); alertDialogBuilder. setNegativeButton ("cancel", new DialogInterface. onClickListener () {@ Overridepublic void onClick (DialogInterface dialog, int which) {dialog. dismiss () ;}}); alertDialogBuilder. create (). show ();}


ShowPaintColorDialog (view): The color selection dialog box is displayed, as follows:

// Public void showPaintColorDialog (View parent) {AlertDialog. builder alertDialogBuilder = new AlertDialog. builder (this, R. style. custom_dialog); alertDialogBuilder. setTitle ("select paint color:"); alertDialogBuilder. setSingleChoiceItems (R. array. paintcolor, select_handwrite_color_index, new DialogInterface. onClickListener () {@ Overridepublic void onClick (DialogInterface dialog, int which) {select_handwrite_color_index = which; touchView. selectHandWriteColor (which); dialog. dismiss () ;}}); alertDialogBuilder. setNegativeButton ("cancel", new DialogInterface. onClickListener () {@ Overridepublic void onClick (DialogInterface dialog, int which) {dialog. dismiss () ;}}); alertDialogBuilder. create (). show ();}

The arrays. xml mentioned in the previous section is also used.

Arrays. xml

 
     
          
   
    @string/track_line
           
   
    @string/Eraser
       
      
          
   
    5
           
   
    10
           
   
    15
           
   
    20
           
   
    25
           
   
    30
       
       
          
   
    RED
           
   
    BLUE
           
   
    BLACK
           
   
    GREEN
           
   
    YELLOW
           
   
    CYAN
           
   
    LTGRAY
       
      
 

At this point, the hand-writing function has been basically completed, and the next step is to save the hand-writing words into a file, and return the path to save the file to the database, which will continue to be improved in the future.






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.