Android-based custom View learning (1), androidview

Source: Internet
Author: User

Android-based custom View learning (1), androidview

Custom View learning for Android (1)

Common Canvas methods:

Image Source

 

/*** Created by SiberiaDante on. */public class BaseViewDraw extends View {private Paint mPaint1; private Paint mPaint2; private Paint mPaint3; private Paint mPaint4; public BaseViewDraw (Context context) {super (context );} public BaseViewDraw (Context context, AttributeSet attrs) {super (context, attrs); initPaint (); // initialization paint} public BaseViewDraw (Context context, AttributeSet attrs, int defStyleAttr) {super (context, attrs, defStyleAttr);} private void initPaint () {mPaint1 = new Paint (); mPaint1.setColor (Color. BLUE); // set the Paint brush color mPaint1.setStrokeWidth (10f); // set the Paint width mPaint1.setStyle (Paint. style. FILL); // set the Paint brush FILL mode mPaint2 = new Paint (); mPaint2.setColor (Color. RED); mPaint2.setStrokeWidth (20f); mPaint2.setStyle (Paint. style. FILL_AND_STROKE); mPaint3 = new Paint (); mPaint3.setColor (Color. BLACK); mPaint3.setStrokeWidth (15f); mPaint3.setStyle (Paint. style. STROKE); mPaint4 = new Paint (); mPaint4.setColor (Color. GREEN); mPaint4.setStrokeWidth (5f); mPaint4.setStyle (Paint. style. STROKE) ;}@ Override protected void onDraw (Canvas canvas) {super. onDraw (canvas);/*** paint color ** learn more about canvas go to official documents: https://developer.android.com/reference/android/graphics/Canvas.html */canvas. drawColor (Color. GRAY);/*** painting point */canvas. drawPoint (200,200, mPaint1); // draw a vertex ---- 200,200 representing the coordinates canvas on the x and Y axes respectively. drawPoints (new float [] {// draw multiple points 300,300,300,400,300,500}, mPaint1);/*** draw lines */canvas. drawLine (10, 10,200,500, mPaint1); // 10, 10 indicates the start point, and 200,500 indicates the end canvas. drawLines (new float [] {// draw multiple line segments 20, 20,600, 20, 50,200, 50,600}, mPaint1);/*** draw a rectangle, three ways to write * learn more Rect go to official documents: https://developer.android.com/reference/android/graphics/Rect.html * learn more RectF go to official documents: http://developer.android.com/reference/android/graphics/RectF.html * // first canvas. drawRect (500,100,800,400, mPaint1); // The Second final Rect rect = new Rect (500,500,800,800); canvas. drawRect (rect, mPaint1); // The third final RectF rectF = new RectF (500,900,800,120 0); canvas. drawRect (rectF, mPaint1);/*** draw a rounded rectangle * // method 1 final RectF rectF1 = new RectF (100,900,400,120 0 ); // 10, 30 respectively represent the radius of the arc canvas. drawRoundRect (rectF1, 10, 30, mPaint2); // method 2 if (Build. VERSION. SDK_INT> = Build. VERSION_CODES.LOLLIPOP) {// This method only supports API21 + canvas. drawRoundRect (100,900,400,120 0, 10, 30, mPaint2 );} /*** draw an ellipse * 350 is half of the length of the X axis of the rectangle * 150 is half of the length of the Y axis of the rectangle */final RectF rectF2 = new RectF (100,500,800,800); canvas. drawRoundRect (rectF2, 350,150, mPaint3);/*** circle * 350 is half of the length of the X axis of the rectangle * 350 is half of the length of the Y axis of the rectangle * When the length and width of the rectangle are equal, that is, circle * // Method 1: Use a rectangle to draw an elliptic technique final RectF rectF3 = new RectF (100,500,800,120 0); canvas. drawRoundRect (rectF3, 350,350, mPaint3); // Method 2: 550,800 represents the center of the circle, and 400 represents the radius of the canvas. drawCircle (550,800,400, mPaint3);/*** draw an arc *, indicating the start and end radians of the radians respectively. The clockwise direction is positive, and the counter-clockwise in mathematics is positive * The third Boolean parameter: true indicates that the center is used, and false indicates that the center is not used, that is, an arc (not filled) */final RectF rectF4 = new RectF (400,400,800,120 0); canvas. drawArc (rectF4, 0, 90, true, mPaint4); final RectF rectF5 = new RectF (100,400,400,120 0); canvas. drawArc (rectF5, 0, 90, false, mPaint4 );}}

 

Github address: https://github.com/SiberiaDante/DrawView

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.