Android custom Controls--Company Chop Production

Source: Internet
Author: User
Tags chop getcolor

Do not use the picture, do a simple seal resources, with custom control to implement.


Analysis:

1, draw two concentric circles, the outer circle is painted in red, the inner circle is painted with the background color of the parent control.

2,Drawtextonpath to draw text.

3, inside the circle with Path.lineto () to draw a pentagram.


Code:

Attrs.xml

<attr name= "Seal_text" format= "string"/> <attr    name= "seal_textsize" format= "Dimension"/>    < attr name= "Seal_textcolor" format= "color"/> <declare-styleable name=    "Customer_seal" >        <attr Name= "Seal_text"/>        <attr name= "seal_textsize"/> <attr        name= "Seal_textcolor"/>    </ Declare-styleable>

To customize the constructor of the view Customerseal:

Public Customerseal (context context, AttributeSet attrs, int defstyleattr) {Super (context, attrs, defstyleattr); Gets the system default background color TypedArray array = Context.gettheme (). Obtainstyledattributes (New int[] {Android . R.attr.colorbackground, Android.        R.attr.textcolorprimary,});        Mbackgroundcolor =array.getcolor (0, 0XFF00FF);        TypedArray a = Context.gettheme (). Obtainstyledattributes (Attrs, R.styleable.customer_seal, defstyleattr, 0);        int n = a.getindexcount ();            for (int i=0;i<n;i++) {int attr = A.getindex (i);                    Switch (attr) {Case r.styleable.customer_seal_seal_text:mtitle = a.getstring (attr);                Break Case R.styleable.customer_seal_seal_textcolor://default Black Mtextcolor = A.getcolor (attr,                    Color.Black);                Break      Case R.styleable.customer_seal_seal_textsize:              Mtextsize = A.getdimensionpixelsize (attr, (int) typedvalue.applydimension (typedvalue.complex_unit_sp,                    Getresources () getdisplaymetrics ());            Break        }} a.recycle ();        Mpaint = new Paint ();    Mpaint.settextsize (mtextsize); }

Override the Ondtraw () method:

@Override protected void OnDraw (canvas canvas) {//Super.ondraw (canvas);        Get canvas width High mwidth = GetWidth ();        Mheight = GetHeight (); Mpaint.setstrokewidth (5);//Set Brush width Mpaint.setantialias (true);        Specifies whether antialiasing is used and, if used, slows down the drawing speed Mpaint.setcolor (color.red);        Mpaint.setstyle (Paint.Style.STROKE);//drawing style, set to fill//Draw circle Path Circlepath = new Path ();  Circlepath.addcircle (MWIDTH/2, MHEIGHT/2, Path.Direction.CW);        Center x coordinate, y coordinate, radius, direction canvas.drawpath (Circlepath, mpaint); Mpaint.setstrokewidth ((float) 0.0);//Set Brush width mpaint.setcolor (mbackgroundcolor);//Set brush width//Draw a circle Path t        Extcirclepath = new Path ();  Textcirclepath.addcircle (MWIDTH/2, MHEIGHT/2, Path.Direction.CW);        Center x coordinate, y coordinate, radius, direction canvas.drawpath (Textcirclepath, mpaint);        Mpaint.setstrokewidth (0);//Set Brush width mpaint.setcolor (mtextcolor);        Mpaint.setstyle (Paint.Style.FILL); Write Canvas.drawtextonPath (Mtitle,textcirclepath,250,-35,mpaint);        (0,0), (MWIDTH/2-radius,mheight/2-radius) int radius = 80;  int StartX = Mwidth/2-radius; Beginning x position int starty = Mheight/2-radius;        Start Y position//Draw Pentagram path PATH = new Path ();        float Radian = Degree2radian (degree); float radius_in = (float) (RADIUS * Math.sin (RADIAN/2)/Math.Cos (radian));        The radius of the middle Pentagon Path.moveto ((float) (RADIUS * Math.Cos (RADIAN/2) +startx), starty); Path.lineto (float) (RADIUS * Math.Cos (RADIAN/2) + radius_in * Math.sin (radian) + StartX), (float) (Radius-radius * Ma        Th.sin (RADIAN/2) +starty)); Path.lineto ((float) (RADIUS * MATH.COS (RADIAN/2) * 2 + StartX), (float) (Radius-radius * Math.sin (RADIAN/2) + Starty        )); Path.lineto ((float) (RADIUS * Math.Cos (RADIAN/2) + radius_in * Math.Cos (RADIAN/2) +startx), (float) (RADIUS + radius_in        * Math.sin (RADIAN/2) +starty)); Path.lineto ((float) (RADIUS * Math.Cos (RADIAN/2) + radius * MaTh.sin (Radian) +startx), (float) (radius + radius * Math.Cos (radian) +starty));        Path.lineto ((float) (RADIUS * Math.Cos (RADIAN/2) +startx), (float) (radius + radius_in+starty)); Path.lineto ((float) (RADIUS * Math.Cos (RADIAN/2)-radius * Math.sin (radian) +startx), (float) (radius + radius * math.co        S (radian) +starty));  Path.lineto ((float) (RADIUS * Math.Cos (RADIAN/2)-radius_in * Math.Cos (RADIAN/2) +startx), (float) (RADIUS + radius_in        * Math.sin (RADIAN/2) +starty));        Path.lineto (StartX, (float) (Radius-radius * Math.sin (RADIAN/2) +starty)); Path.lineto (float) (RADIUS * Math.Cos (RADIAN/2)-radius_in * Math.sin (radian) +startx), (float) (Radius-radius * Mat        H.sin (RADIAN/2) +starty));        Path.close ();    Canvas.drawpath (path, mpaint); }

The method of angle turning arc

/** *     angle to radians     * @param degree     * @return * */    private float Degree2radian (float degree) {        return ( FLOAT) (Math.PI * degree/180);    }


Source


Android custom Controls--Company Chop Production

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.