Android development smart car App (2)---android paint and canvas custom view

Those who have customized View should be familiar with these two properties.  Paint画笔Canvas画布. With these two conditions, we can do a lot of things.

If you want to set some properties like text thickness, size, color then use Paint.

Paint: This class holds style and color information for drawing geometry, text, and bitmaps. That is to say, we can use the styles and colors saved by Paint to draw graphics, text and bitmaps, which is the power of Paint. Next we use Paint to draw and see what styles and colors this class has.

When we want to customize the use of View, we need to 构造函数create a Paint brush object in it, not in onDraw()it, because onDraw may be refreshed frequently in the follow-up. If some initialization operations are set in this, then there is no need to do so. Pointless and waste of resources.

Let's look at the basic property settings of a Paint:

   mPaint = new Paint();                                             //使用paint前要对paint初始化
//Start setting the basic information of the brush mPaint.setAntiAlias(true); //Set the antialiasing of the brush mPaint.setColor(Color.WHITE); //Set the color of the brush mPaint.setStyle(Paint.Style.FILL); //Set the fill type of the drawn graphic, fill is internal fill, stroke is only border, and content is not filled mPaint.setStrokeWidth(mDensity * 2); //Set the width of the brush. The received parameter is the pixel unit mPaint.setTextSize(mDensity * 20); //Set the font size when drawing text
  mPaint.setStrokeCap(Paint.Cap.SQUARE);              //设置线帽,ROUND:圆形,SQUARE:方形

setStyle (Paint.Style style)

  • Paint.Style.FILL fills the interior
  • Paint.Style.FILL_AND_STROKE fill and stroke
  • Paint.Style.STROKE only stroke but no fill

If you want to draw a graphic directly then use Canvas

Canvas class: This class specifies the color of the background and the shape to be drawn

drawLine (float startX, float startY, float stopX, float stopY, Paint paint)//画直线
    //startX: start point X coordinate
    //startY: start point Y coordinate
    //stopX: end point X coordinate
    //stopY: Y coordinate of the end point
drawPoint (float x, float y, Paint paint)//画点
     
drawRect (float left, float top, float right, float bottom, Paint paint)//画矩形

drawRoundRect (RectF rect, float rx, float ry, Paint paint)//Draw a rounded rectangle
    //rx: The radius of the X axis of the rounded ellipse
    //ry: The radius of the Y axis of the rounded ellipse
drawCircle (float cx, float cy, float radius, Paint paint)//画圆形
    //cx: the coordinates of the center x point
    //cy: the coordinates of the y point of the center of the circle
    //radius: the radius of the circle

drawArc (RectF oval, float startAngle, float sweepAngle, boolean useCenter, Paint paint)//画弧
    //startAngle: the starting point, the default is that the positive direction of the x-axis of the coordinate system is 0, the positive value is clockwise, and the negative value is the other way around
    //sweepAngle: The angle that needs to be displayed from the start point to the end point. The positive value of this angle is clockwise rotation.
    //useCenter: is a boolean, if true, there is a connection from the start to the center of the circle, and from the end point to the center of the circle. Otherwise, there is no.

  

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324860759&siteId=291194637