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) { super(context); init(); } public BasisView(Context context, @Nullable AttributeSet attrs) { super(context, attrs); init(); } public BasisView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(); } private void init() { //Initialization Brush mPaint=new Paint(); mPaint.setStyle(Paint.Style.STROKE); mPaint.setStrokeWidth(5); //Initialization area rectF = new RectF(); rectF.set(100, 10, 1000, 300); } @Override public boolean onTouchEvent(MotionEvent event) { mX = (int) event.getX(); mY = (int) event.getY(); if (event.getAction() == MotionEvent.ACTION_DOWN) { invalidate(); return true; } else if (event.getAction() == MotionEvent.ACTION_UP) { mX = -1; mY = -1; } postInvalidate(); return super.onTouchEvent(event); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); if (rectF.contains(mX, mY)) { mPaint.setColor(Color.GREEN); } else { mPaint.setColor(Color.RED); } 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
RectF.intersects(a,b);
Membership Method to Determine Intersection
a= new RectF(); a.set(100, 10, 1000, 300); a.intersect(b);
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(); paint.setColor(Color.RED); paint.setStyle(Paint.Style.STROKE); paint.setStrokeWidth(5); Path path = new Path(); path.moveTo(10,10); path.lineTo(10,100); path.lineTo(300,100); path.close(); canvas.drawPath(path,paint);
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)
Underline
void setUnderlineText(boolean underlineText)
tilt
void setTextSkewX(float skewX)
Strikeout
void setStrikeThruText(boolean strikeThruText)
Horizontal stretch
void setTextScaleX(float scaleX)
Draw
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