Notepad for Android Project-----Setting the brush size and brush color of the handwriting function

Source: Internet
Author: User

The previous section realizes all the functions of the artboard, including setting the brush size, setting the brush color, eraser and other functions, while handwriting, you can also add the ability to set the size and color of the handwriting, this section of the trend to achieve handwriting adjustment handwriting size and adjust the color of the handwriting.

First look at the picture:

In fact, the handwriting and artboard of these functions are similar, direct reuse of the previous code on the line, the principle is as follows:

1. The ability to set the brush size:

1) Initialize the brush.

2) Set the size of the brush to the size you choose.

3) Use one variable to remember the size of the current brush, and to keep the brush size set before other actions.

2. The ability to set the brush color:

1) Initialize the brush.

2) Set the color of the brush to the selected color.

3) Use one variable to remember the color of the current brush, and to keep the brush color set before other actions.

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

Private Paint mpaint;private int currentcolor = color.red;private int currentsize = 5;
   Set 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);    }

To set the brush size:

         Sets the size of the brush public void selecthandwritetsize (int which) {int size =integer.parseint (this.getresources (). Getstringarray ( R.array.paintsize) [which]);     currentsize = size;    Setpaintstyle ();}

To set the brush color:

    Set brush color public    void Selecthandwritecolor (int which) {        currentcolor = Paintcolor[which];    Setpaintstyle ();    }


After adding these methods in the custom view, the next step is to invoke these methods in the activity, as in the previous section (add the following code to Handwriteactivity.java):

Brush size Case  0:  showpaintsizedialog (view);  break;  Color Case  1:  showpaintcolordialog (view);  Break

By clicking on these two buttons, a popup dialog box is used to select the size and color of the brush

Showpaintsizedialog (view), a dialog box with the selection brush size appears, as follows:

  Pop-Up Brush Size Options dialog box public  void Showpaintsizedialog (View parent) {        Alertdialog.builder alertdialogbuilder = new Alertdialog.builder (This,r.style.custom_dialog); Alertdialogbuilder.settitle ("Select 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);d Ialog.dismiss ();}); Alertdialogbuilder.setnegativebutton ("Cancel", new Dialoginterface.onclicklistener () {@Overridepublic void OnClick ( Dialoginterface dialog, int which) {Dialog.dismiss ();}}); Alertdialogbuilder.create (). Show ();  


Showpaintcolordialog: Pop-up color selection dialog box, as follows:

Pop-Up Brush Color Options dialog box public  void Showpaintcolordialog (View parent) {        Alertdialog.builder alertdialogbuilder = new Alertdialog.builder (This,r.style.custom_dialog); Alertdialogbuilder.settitle ("Select brush 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);d Ialog.dismiss ();}); Alertdialogbuilder.setnegativebutton ("Cancel", new Dialoginterface.onclicklistener () {@Overridepublic void OnClick ( Dialoginterface dialog, int which) {Dialog.dismiss ();}}); Alertdialogbuilder.create (). Show ();  

It also uses the arrays.xml mentioned in the previous section.

Arrays.xml

<?xml version= "1.0" encoding= "Utf-8"?><resources> < String-array name= "Paintstyle" > <item> @string/track_line</item> <item> @string/eraser< ;/item> </string-array> <string-array name= "Paintsize" > <item>5</item> < item>10</item> <item>15</item> <item>20</item> <item>25</it em> <item>30</item> </string-array> <string-array name= "Paintcolor" > <i tem>red</item> <item>BLUE</item> <item>BLACK</item> <item>gree n</item> <item>YELLOW</item> <item>CYAN</item> <item>ltgray</i Tem> </string-array> </resources> 

At this point, the handwriting function, also has been basically complete, then left the handwriting of the word saved to a file, and return to save the file path stored in the database, follow-up continue to improve.






Notepad for Android Project-----Setting the brush size and brush color of the handwriting function

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.