Android Custom Control Foundation Part 2

I. RectF and Rect

1. Does it contain a point

boolean contains(float x, float y)

An example can be written based on this:
Draw a rectangle to determine if your finger is in range.

public class BasisView extends View {

    private int mX, mY;
    private RectF rectF;
    private Paint mPaint;

    public BasisView(Context context) {

    public BasisView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);

    public BasisView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

    private void init() {
        //Initialization Brush
        mPaint=new Paint();
        //Initialization area
        rectF = new RectF();
        rectF.set(100, 10, 1000, 300);

    public boolean onTouchEvent(MotionEvent event) {
        mX = (int) event.getX();
        mY = (int) event.getY();
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            return true;
        } else if (event.getAction() == MotionEvent.ACTION_UP) {
            mX = -1;
            mY = -1;
        return super.onTouchEvent(event);

    protected void onDraw(Canvas canvas) {
        if (rectF.contains(mX, mY)) {
        } else {
        canvas.drawRect(rectF, mPaint);

The contain method also has two different parameters

boolean contains(RectF r)
boolean contains(float left, float top, float right, float bottom)

2. Determine whether two rectangles intersect

boolean intersect(RectF a, RectF b)

Static method for determining intersection


Membership Method to Determine Intersection

a= new RectF();
a.set(100, 10, 1000, 300);

The member method not only returns the intersection result but also assigns the intersecting part to the current object.Non-intersecting does not assign values.

3. Merge two rectangles

Merging means that the minimum upper left corner of the two rectangles is the upper left corner of the new rectangle, and the maximum lower right corner of the two rectangles is the lower right corner of the new rectangle, whether the two rectangles intersect or not.

 void union(RectF r)

2. Path

1. Drawing methods

void drawPath(Path path, Paint paint)

2. Linear Path

Drawing a straight path takes three steps

void moveTo(float x, float y)
void lineTo(float x, float y)
 void close()

Give an example:
Draw a triangle

 Paint paint = new Paint();

 Path path = new Path();

3. Arc Path

void arcTo(RectF oval, float startAngle, float sweepAngle, boolean forceMoveTo)
  • oval: a rectangle that generates an ellipse
  • startAngle: Start angle
  • sweepAngle: persistent angle
  • forceMoveTo: Whether to force the starting point of an arc to be the starting point for drawing

4.addXXX Series

Paths are generally contiguous, and parameters with add can be added directly to Path without regard to continuity.

Add Arc

void addArc(RectF oval, float startAngle, float sweepAngle)

Add Rectangle

void addRect(RectF rect, Path.Direction dir)

dir parameter:

  • Path.Direction.CCW: Counterclockwise
  • Path.Direction.CW: Clockwise

Add Rounded Rectangle

void addRoundRect(RectF rect, float rx, float ry, Path.Direction dir)

Add Round Path

void addCircle(float x, float y, float radius, Path.Direction dir)

Add Ellipse Path

void addOval(RectF oval, Path.Direction dir)

5. Path Fill Mode

  • Path.FillType.WINDING: Default value, which is displayed when two graphics intersect.
  • Path.FillType.EVEN_ODD: Take the region where paths do not intersect.
  • Path.FillType.INVERSE_WINDING: Take the outer region of the path.
  • Path.FillType.INVERSE_EVEN_ODD: Take the outer and intersecting areas of the path.

6. Reset

When we need to draw a new path, we can reset the path for reuse purposes.

Two methods are provided

void reset() 
void rewind()

rewind clears FillType and path data, but maintains the data structure.This allows for fast reuse.
reset clears path data and structure, but does not clear FillType.

3. Text

Text needs to be implemented with a brush

text alignment

 void setTextAlign(Paint.Align align)

Text size

void setTextSize(float textSize)

Bold or not

void setFakeBoldText(boolean fakeBoldText)


 void setUnderlineText(boolean underlineText)


void setTextSkewX(float skewX)


void setStrikeThruText(boolean strikeThruText)

Horizontal stretch

void setTextScaleX(float scaleX)


void drawText(String text, float x, float y, Paint paint)

Draw along a path

void drawTextOnPath(String text, Path path, float hOffset, float vOffset, Paint paint)
  • hOffset: The horizontal offset along the starting point of the path
  • vOffset: Vertical offset from the center of the path

Keywords: Android wechat

Added by lucianoes on Fri, 03 Sep 2021 20:28:36 +0300