Android Custom View Knowledge points

Source: Internet
Author: User

Learn and summarize according to the knowledge points provided by Hencoder.

Three points:
    1. Layout
    2. Draw
    3. Touch Feedback
Draw
    • Custom Draw: Implement the drawing process by yourself
    • Common drawing methods OnDraw (canvas canvas)
    • Draw key:
      1. Canvas: Drawing Tools
        • Drawing class methods
        • Drawcircle (): Draw a circle
        • DrawRect (): Draw Square
        • DrawPath (): Draw route
        • DrawText (): Drawing text
        • Drawbitmap (): Painting bitmap
        • Drawing Accessibility Tools
        • Crop the drawing range
        • Geometric transformations for drawing content
      2. Paint: Original paint, Android middle finger to strengthen the pigment, can be set in various styles, such as hollow or solid or thickness, etc.
      3. Drawing order: Android is drawn in order. Drawing foreground (forground) and background (BackGround) In addition to the OnDraw () Drawing body part
Example: Paint Common settings:
    • Mpaint.setcolor (color.red): Set color
    • Mpaint.setstyle (Style.stroke): Set drawing mode
    • Mpaint.setstrokewidth (20): Set line switch
    • Mpaint.setantialias (TRUE): Set anti-aliasing
Cancan drawing a class method drawing color
* canvas.drawColor(Color.RED);* canvas.drawColor(Color.parseColor("#00ff00"));* canvas.drawColor(Color.parseColor("#8800ff00"));* canvas.drawRGB(255, 0, 0);* canvas.drawARGB(100, 255, 0, 0);
Draw a Circle
1. 实心圆:         mPaint = new Paint();        canvas.drawCircle(100, 100, 50, mPaint);2. 空心圆          mPaint.setStyle(Style.STROKE);        canvas.drawCircle(200, 200, 50, mPaint);3. 蓝色实心圆        mPaint.setStyle(Style.FILL);        mPaint.setColor(Color.BLUE);        canvas.drawCircle(200, 400, 50, mPaint);4. 线宽为20的空心圆        mPaint.setStyle(Style.STROKE);        mPaint.setStrokeWidth(20);        canvas.drawCircle(400, 200, 50, mPaint);
Draw Ellipse
    canvas.drawOval(300, 100, 700, 300, mPaint);
Draw a rectangle
    canvas.drawRect(250, 500, 700, 800, mPaint);    //    Rect tem1 = new Rect(250, 500, 700, 800);    canvas.drawRect(tem1, mPaint);    //    RectF tem2 = new RectF(250, 500, 700, 800);    canvas.drawRect(tem2, mPaint);
Draw rounded rectangles
    RectF t = new RectF(150, 200, 500, 350);    canvas.drawRoundRect(t, 50, 50, mPaint);    //    canvas.drawRoundRect(150, 200, 500, 350, 50, 50, mPaint);
Draw arcs or fans
    • DrawArc (float left, float top, float right, float bottom, float startangle,float sweepAngle, Boolean usecenter, Paint pain T
      * Left,top,right,bottom: Ellipse where arc is located
      * StartAngle: The starting angle of the arc (the positive direction of the x-axis, that is, the position of the right, 0 degrees). Clockwise is positive angle, counterclockwise is negative angle)
      * SweepAngle: Arc across the angle
      * Usecenter: Indicates a very connected center, 连接圆心是扇形不连接圆心是弧形

    • Drawing arcs

      canvas.drawArc(200, 600, 800, 1000, 0, 47, false, mPaint);
    • Draw sector

      canvas.drawArc(200, 600, 800, 1000, 0, 135, true, mPaint);
    • Example:

      mPaint.setAntiAlias(true);mPaint.setStyle(Style.FILL);canvas.drawArc(200, 100, 800, 500, -110, 100, true, mPaint); // 绘制扇形canvas.drawArc(200, 100, 800, 500, 20, 140, false, mPaint); // 绘制弧形mPaint.setStyle(Paint.Style.STROKE); // 画线模式canvas.drawArc(200, 100, 800, 500, 180, 60, false, mPaint); // 绘制不封口的弧形
Draw a custom graphic
  • DrawPath (path path, paint paint): Drawing graphics by describing paths
  • Path: The object that describes the path
  • Paint

  • Path
  • Direct Path Description
    1. addxxx ()--Add a child shape
    • addcircle (float x, float y, float radius, Direction dir), This method, like the Canvas's drawcircle (), draws a circular
      • description: Add circle
      • parameter x and parameter y: OK center
      • parameter radius: Determines the radius of the circle
      • dir: Draw the direction of the circular path, enum type, CW (clockwise) clockwise/CCW (counter-clockwise) counterclockwise
    • addarc (): Drawa with Canvas RC (), draw an arc or sector
    • addoval (): Like the canvas's DrawOval (), draw the ellipse
    • addrect (): Like the canvas's DrawRect (), Draw the rectangle
    • Addroundrect (): Draw rounded rectangles
    1. xxxto () As with Canvas's Drawroundrect ()--Draw lines (lines or curves)
    • LineTo (): Draw a line, draw a line from the current position to the target, X, y
    • Rlineto (): As with the LineTo () method, the parameter is relative to the current position
    • MoveTo (): Move to the target location
    • Rmoveto ()
    • Cubicto (): Draw three Bezier curves
    • Rcubicto ()
    • quadto (): Draw two Bezier curves
    • rquadto ()
    • ArcTo: Drawing arcs
    1. Close (): Encloses the current sub-shape, as with lineTo (starting coordinates)
  • Auxiliary settings or calculations
    • Setfilltype (): Set fill mode
    • Even_odd: Odd-even principle, cross-fill mode, for any point in the plane, a ray in any direction, the number of times the ray and the graph intersect (intersection is calculated, tangent does not count) if it is odd, then this point is considered inside the graph, is to be painted area; This point is considered to be outside of the graph and is not a painted area.
    • Winding (default): Non-0 surround number principle, full fill mode
    • Inverse_even_odd
    • Inverse_winding
Draw Bitmap
    • Drawbitmap ()
Draw text
    • DrawText ()

Android Custom View Knowledge points

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.