Android Custom View Learning (i)

Source: Internet
Author: User

Android Custom View Learning (i)

Canvas Common methods:

                                   Photo source

 Public classBaseviewdrawextendsView {PrivatePaint mPaint1; PrivatePaint MPaint2; PrivatePaint MPaint3;  PublicBaseviewdraw (Context context) {Super(context); }     PublicBaseviewdraw (Context context, AttributeSet attrs) {Super(context, attrs); Initpaint ();//Initializing brushes    }     PublicBaseviewdraw (context context, AttributeSet attrs,intdefstyleattr) {        Super(context, attrs, defstyleattr); }    Private voidInitpaint () {mPaint1=NewPaint (); Mpaint1.setcolor (Color.Blue);//Set Brush ColorMpaint1.setstrokewidth (10f);//Set Brush widthMpaint1.setstyle (Paint.Style.FILL);//Set Brush fill modeMPaint2=NewPaint ();        Mpaint2.setcolor (color.red);        Mpaint2.setstrokewidth (20f);        Mpaint2.setstyle (Paint.Style.FILL_AND_STROKE); MPaint3=NewPaint ();        Mpaint3.setcolor (Color.Black);        Mpaint3.setstrokewidth (15f);    Mpaint3.setstyle (Paint.Style.STROKE); } @Overrideprotected voidOnDraw (canvas canvas) {Super. OnDraw (canvas); /*** Draw Color * Learn more about canvas to official documents:https://developer.android.com/reference/android/graphics/Canvas.html         */Canvas.drawcolor (Color.gray); /*** Draw points*/Canvas.drawpoint ((MPAINT1);//draw a point----200,200 respectively represents the coordinates on the x, Y axis that you want tocanvas.drawpoints (New float[]{//Draw multiple points300, 300,                        300, 400,                        300, 500}, MPaint1); /*** Draw Lines*/Canvas.drawline (Ten, ten, $, mPaint1);//10, 10 for starting point, 200,500 for end PointCanvas.drawlines (New float[]{//draw multiple line segments20, 20, 600, 20,                50, 200, 50, 600}, MPaint1); /*** Draw rectangles, three ways * Learn more about rect go to official documents:https://developer.android.com/reference/android/graphics/Rect.html* Learn more about RECTF to official documents:http://developer.android.com/reference/android/graphics/RectF.html         */        //First KindCanvas.drawrect (500, 100, 800, 400, MPaint1); //The second Kind        FinalRect rect =NewRect (500, 500, 800, 800);        Canvas.drawrect (rect, mPaint1); //Third Kind        FinalRECTF RECTF =NewRECTF (500, 900, 800, 1200);        Canvas.drawrect (RECTF, mPaint1); /*** Draw rounded rectangles*/        //Method One        FinalRECTF rectF1 =NewRECTF (100, 900, 400, 1200); //10,30 represents the radius of the arc, respectivelyCanvas.drawroundrect (rectF1, 10, 30, MPaint2); //Method Two        if(Build.VERSION.SDK_INT >=Build.version_codes. LOLLIPOP) {//this notation only supports api21+Canvas.drawroundrect (100, 900, 400, 1200, 10, 30, MPaint2); }        /*** Draw Ellipse * 350 is half of the length of the rectangular x-axis * 150 is half the length of the rectangular y-axis*/        FinalRECTF rectF2 =NewRECTF (100, 500, 800, 800); Canvas.drawroundrect (rectF2,350, 150, MPaint3); /*** Draw Circle * 350 is half of the length of the rectangular x-axis * 350 is half the length of the rectangular y-axis * When the length and width of the rectangle are equal, the circle*/        //method One: Draw ellipse technique with rectangle        FinalRECTF rectF3 =NewRECTF (100, 500, 800, 1200); Canvas.drawroundrect (RectF3,350, 350, MPaint3); //method Two: 550,800 for the center position, 400 for the circle radiusCanvas.drawcircle (550, 800, 400, MPaint3); }}

GitHub Address: Https://github.com/SiberiaDante/DrawView

Android Custom View learning (i)

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.