The eraser brush size and brush color of the Android artboard feature

Source: Internet
Author: User

The first display sets the color of the brush, the second shows the color of the brush size, and the third shows the eraser function, which sets the icon color to blue, and adds the leftmost button (in fact, adds an item to the GridView).

Here are the main ideas for the eraser, setting the brush size, setting the brush color, respectively:

1. Eraser function:

Rationale: The eraser is a touch on the screen with a brush that is consistent with the color of the canvas, and the eraser function is simply achieved.

1) Initialize the brush and set the color of the brush to white (this is actually set to the color of the canvas).

2) Set the size of the brush to the appropriate size.

3) Use one variable to remember the color of the eraser and use it to reuse the eraser after other actions.

2. 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.

3. 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 is as follows:

Private Bitmap  Mbitmap; Private int currentcolor = color.red;         Private int 5 ; Private int 1;
//Set Brush Styles       Public voidSetpaintstyle () {Mpaint=NewPaint (); Mpaint.setantialias (true); Mpaint.setdither (true);          Mpaint.setstyle (Paint.Style.STROKE);          Mpaint.setstrokejoin (Paint.Join.ROUND);          Mpaint.setstrokecap (Paint.Cap.ROUND);          Mpaint.setstrokewidth (currentsize); if(Currentstyle = =1) Mpaint.setcolor (CurrentColor); Else{mpaint.setcolor (color.white); }      }        
//Initializing the canvas  Public voidInitcanvas () {Setpaintstyle (); Mbitmappaint=NewPaint (Paint.dither_flag); //Canvas SizeMbitmap =Bitmap.createbitmap (Bitmapwidth, Bitmapheight, Bitmap.Config.RGB_565); Mcanvas=NewCanvas (MBITMAP);//all Mcanvas paintings are preserved in the Mbitmap.Mcanvas.drawcolor (Color.White); MPath=NewPath (); Mbitmappaint=NewPaint (Paint.dither_flag); }

To set the brush style:

//Set Brush Styles   Public voidSelectpaintstyle (intwhich) {            if(Which = =0) {Currentstyle=1;      Setpaintstyle (); }      //When the eraser is selected, set the color to white      if(Which = =1) {Currentstyle=2;          Setpaintstyle (); Mpaint.setstrokewidth ( -); }  }

To set the brush size:

// Select brush size   Public void selectpaintsize (int  which) {            int size =integer.parseint ( this . Getresources (). Getstringarray (R.array.paintsize) [which]);        = size;      Setpaintstyle ();  }

To set the brush color:

// Set Brush color   Public void selectpaintcolor (int  which) {            = Paintcolor[which];      Setpaintstyle ();  }

Of course, these methods are implemented in custom view, which is Paintview, and the next step is to invoke the custom view method by clicking the bottom button to implement the corresponding function.

//Select a brush style              Case 0: Showmoredialog (view);  Break; //Brush Size              Case 1: Showpaintsizedialog (view);  Break; //Brush Color              Case 2: Showpaintcolordialog (view);  Break;
      //pop-Up Brush Color Options dialog Box     Public voidShowpaintcolordialog (View parent) {Alertdialog.builder Alertdialogbuilder=NewAlertdialog.builder ( This, R.style.custom_dialog); Alertdialogbuilder.settitle ("Select a brush color:"); Alertdialogbuilder.setsinglechoiceitems (R.array.paintcolor, Select_paint_color_index,NewDialoginterface.onclicklistener () {@Override Public voidOnClick (Dialoginterface Dialog,intwhich) {Select_paint_color_index=which;        Paintview.selectpaintcolor (which);    Dialog.dismiss (); }}); Alertdialogbuilder.setnegativebutton ("Cancel",NewDialoginterface.onclicklistener () {@Override Public voidOnClick (Dialoginterface Dialog,intwhich)    {Dialog.dismiss ();    }}); Alertdialogbuilder.create (). Show (); }              //pop-Up Brush Size Options dialog box     Public voidShowpaintsizedialog (View parent) {Alertdialog.builder Alertdialogbuilder=NewAlertdialog.builder ( This, R.style.custom_dialog); Alertdialogbuilder.settitle ("Select a brush size:"); Alertdialogbuilder.setsinglechoiceitems (R.array.paintsize, Select_paint_size_index,NewDialoginterface.onclicklistener () {@Override Public voidOnClick (Dialoginterface Dialog,intwhich) {Select_paint_size_index=which;        Paintview.selectpaintsize (which);    Dialog.dismiss (); }}); Alertdialogbuilder.setnegativebutton ("Cancel",NewDialoginterface.onclicklistener () {@Override Public voidOnClick (Dialoginterface Dialog,intwhich)    {Dialog.dismiss ();    }}); Alertdialogbuilder.create (). Show (); }            //Pop-up dialog box to select a brush or eraser     Public voidShowmoredialog (View parent) {Alertdialog.builder Alertdialogbuilder=NewAlertdialog.builder ( This, R.style.custom_dialog); Alertdialogbuilder.settitle ("Select a brush or eraser:"); Alertdialogbuilder.setsinglechoiceitems (R.array.paintstyle, Select_paint_style_index,NewDialoginterface.onclicklistener () {@Override Public voidOnClick (Dialoginterface Dialog,intwhich) {Select_paint_style_index=which;        Paintview.selectpaintstyle (which);    Dialog.dismiss (); }}); Alertdialogbuilder.setnegativebutton ("Cancel",NewDialoginterface.onclicklistener () {@Override Public voidOnClick (Dialoginterface Dialog,intwhich)    {Dialog.dismiss ();    }}); Alertdialogbuilder.create (). Show (); }     

Here the arrays.xml is used, as follows:

<?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>Ten</item> <item> the</item> <item> -</item> <item> -</item> <item> -</item> </string-array> <string-array name="Paintcolor"> <item>RED</item> <item>BLUE</item> <item>BLACK</item> <item>GREEN</item> <item>YELLOW</item> <item>CYAN</item> <ite M>ltgray</item> </string-array> </resources>

At this point, all the functions of the artboard have been implemented.

In fact, there is a more interesting function, is to set a pencil icon for the brush, the main principle is that in the custom view of the OnDraw method, the pencil picture is loaded in, and set the picture to move along the path.

In the OnDraw method in the Custom view, add:

// When you move, the brush icon       is displayed if (this. ismoving && currentcolor! = color.white)           {// Set the icon               for the brush Bitmap pen = bitmapfactory.decoderesource (this. Getresources (),                           r.drawable.pen);                 This this. MY- pen.getheight (),                           new  Paint (Paint.dither_flag));       }    

The eraser brush size and brush color of the Android artboard feature

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.