Notepad for Android Project (9)-----eraser, brush size, and brush color for artboard features

Source: Internet
Author: User

The previous section has implemented the drawing in the artboard, delete, restore into the empty function, as well as the eraser, set the brush size and brush color is not implemented, this section will be implemented one by one.

Look first:

In, the first shows the ability to set the color of the brush, the second shows the color of the set brush size, and the third shows the function of the eraser, carefully found, this section will set the icon color to blue, and added the leftmost button (in fact, Is the addition of an item in 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 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);       }        
       Initialize canvas public        void Initcanvas () {                setpaintstyle ();            Mbitmappaint = new Paint (paint.dither_flag);                    Canvas size             Mbitmap = Bitmap.createbitmap (Bitmapwidth, Bitmapheight,                 Bitmap.Config.RGB_565);            Mcanvas = new Canvas (mbitmap);  All Mcanvas paintings are preserved in the Mbitmap                        Mcanvas.drawcolor (color.white);            MPath = new Path ();            Mbitmappaint = new Paint (Paint.dither_flag);                    }


To set the brush style:

        Set brush style public        void Selectpaintstyle (int which) {        setpaintstyle ();                When the eraser is selected, set the color to white        if (which = = 1) {        mpaint.setcolor (color.white);        Mpaint.setstrokewidth (a);        CurrentColor = Color.White;        }                }

To set the brush size:

<strong abp= "918" >         </strong>//Select brush size <strong abp= "919" > Public        void selectpaintsize (int which) {        setpaintstyle ();        int size =integer.parseint (this.getresources (). Getstringarray (R.array.paintsize) [which]);         currentsize = size;        Mpaint.setstrokewidth (currentsize);        } </strong>

To set the brush color:

        Set Brush color <strong abp= "924" > Public        void Selectpaintcolor (int which) {        setpaintstyle ();        CurrentColor = Paintcolor[which];        Mpaint.setcolor (CurrentColor);        }        </strong>

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 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 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_paint_color_index, new Dialoginterface.onclicklistener () {@Overridepublic void OnClick (dialoginterface dialog, int which) {Select_paint_ Color_index = Which;paintview.selectpaintcolor (which);d Ialog.dismiss ();}); Alertdialogbuilder.setnegativebutton ("Cancel", new Dialoginterface.onclicklistener () {@Overridepublic void OnClick ( Dialoginterface dialog, int which) {Dialog.dismiss ();}});  Alertdialogbuilder.create (). Show (); }//Popup Brush Size Options dialog box public void Showpaintsizedialog (View parent) {Alertdialog.builder Alertdialogbuilder = NE W Alertdialog.builder (This,r.style.custom_dialog); Alertdialogbuilder.settitle ("Select Brush Size:"); Alertdialogbuilder.setsinglechoiceitems (R.array.paintsize, Select_paint_size_index, new DialogIntErface. Onclicklistener () {@Overridepublic void OnClick (dialoginterface dialog, int which) {select_paint_size_index = which; Paintview.selectpaintsize (which);d Ialog.dismiss ();}); Alertdialogbuilder.setnegativebutton ("Cancel", new Dialoginterface.onclicklistener () {@Overridepublic void OnClick ( Dialoginterface dialog, int which) {Dialog.dismiss ();}});  Alertdialogbuilder.create (). Show (); }//Popup Selection Brush or Eraser dialog box public void Showmoredialog (View parent) {Alertdialog.builder Alertdialogbuilder = new Aler Tdialog.builder (This,r.style.custom_dialog); Alertdialogbuilder.settitle ("Select Brush or Eraser:"); Alertdialogbuilder.setsinglechoiceitems (R.array.paintstyle, Select_paint_style_index, new Dialoginterface.onclicklistener () {@Overridepublic void OnClick (dialoginterface dialog, int which) {Select_paint_ Style_index = Which;paintview.selectpaintstyle (which);d Ialog.dismiss ();}); Alertdialogbuilder.setnegativebutton ("Cancel", new Dialoginterface.onclicklistener () {@Overridepublic void OnClick ( Dialoginterface Dialog, int which) {Dialog.dismiss ();}});  Alertdialogbuilder.create (). Show ();        }

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 moving, show the brush icon    if (this.ismoving && currentcolor! = color.white) {    //Set the icon of the brush        Bitmap pen = Bitmapfactory.decoderesource (This.getresources (),        r.drawable.pen);        Canvas.drawbitmap (pen, this.mx, This.my-pen.getheight (),        new Paint (Paint.dither_flag));    }








Notepad for Android Project (9)-----eraser, brush size, and brush color for artboard features

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.