Code example for RectF

Methods: centerX, centerY, height, width

0
     * @param startAngle The starting angle of the arc in degrees 
     * @param angleExtent The angle extent of the arc in degrees 
     * @param useCenter If true, include the center of the oval in the arc, and close it if it is being stroked 
     */ 
    public ArcShape(float x, float y, float width, float height, float startAngle, float angleExtent, boolean useCenter) {
        this.mOval = new RectF(x, y, x + width, y + height);
        this.mStartAngle = startAngle;
        this.mAngleExtent = angleExtent;
        this.mPath = new Path();
        this.mStartPoint = new PointF();
        this.mEndPoint = new PointF();
        this.mUseCenter = useCenter;
        this.mRegion = new Region();
        update(); 
    } 
 
    /** 
     * Creates a new arc shap from specified location, size, double angles, and useCenter values. 
     * @param x The x-coordinate of upper-left corner of the arc 
     * @param y The y-coordinate of upper-left corner of the arc 
     * @param width The overall width of the full oval of which this arc is a partial section 
     * @param height The overall height of the full oval of which this arc is a partial section 
     * @param startAngle The starting angle of the arc in degrees 
     * @param angleExtent The angle extent of the arc in degrees 
     * @param useCenter If true, include the center of the oval in the arc, and close it if it is being stroked 
     */ 
    public ArcShape(double x, double y, double width, double height, double startAngle, double angleExtent, boolean useCenter) {
        this.mOval = new RectF((float)x, (float)y, (float)(x + width), (float)(y + height));
        this.mStartAngle = (float)startAngle;
        this.mAngleExtent = (float)angleExtent;
        this.mPath = new Path();
        this.mStartPoint = new PointF();
        this.mEndPoint = new PointF();
        this.mUseCenter = useCenter;
        update(); 
    } 
 
    /** 
     * Creates a new arc shape from specified location, size, float angles, and useCenter values. 
     * @param arcRect The bounding rectangle that defines the outer boundary of the full oval of which this arc is a partial section 
     * @param startAngle The starting angle of the arc in degrees 
     * @param angleExtent The angle extent of the arc in degrees 
     * @param useCenter If true, include the center of the oval in the arc, and close it if it is being stroked 
     */ 
    public ArcShape(RectShape rect, float startAngle, float angleExtent, boolean useCenter) {
        this(rect.getX(), rect.getY(), rect.getWidth(), rect.getHeight(), startAngle, angleExtent, useCenter);
    } 
     
    /** 
     * Creates a new arc shape from specified location, size, double angles, and useCenter values. 
     * @param arcRect The bounding rectangle that defines the outer boundary of the full oval of which this arc is a partial section 
     * @param startAngle The starting angle of the arc in degrees 
     * @param angleExtent The angle extent of the arc in degrees 
     * @param useCenter If true, include the center of the oval in the arc, and close it if it is being stroked 
     */ 
    public ArcShape(RectShape rect, double startAngle, double angleExtent, boolean useCenter) {
        this(rect.getX(), rect.getY(), rect.getWidth(), rect.getHeight(), (float)startAngle, (float)angleExtent, useCenter);
    } 
     
    /** 
     * Creates new ArcShape, copying from specified rounded corner arc shape. 
     * @param arcShape The specified arc shape 
     */ 
    public ArcShape(ArcShape arcShape) {
        this.mOval = arcShape.mOval;
        this.mStartAngle = arcShape.mStartAngle;
        this.mAngleExtent = arcShape.mAngleExtent;
        this.mStartPoint = new PointF(arcShape.mStartPoint.x, arcShape.mStartPoint.y);
        this.mEndPoint = new PointF(arcShape.mEndPoint.x, arcShape.mEndPoint.y);
        this.mPath = arcShape.mPath;
        this.mRegion = arcShape.mRegion;
        this.mUseCenter = arcShape.mUseCenter;
    } 
     
    public Path getPath() {
        return mPath;
    } 
 
    /** 
     * {@inheritDoc}  
     */ 
    public void draw(Canvas canvas, Paint paint) {
        paint.setStyle(Paint.Style.STROKE);
        canvas.drawArc(mOval, mStartAngle, mAngleExtent, mUseCenter, paint);
    } 
 
    /** 
     * {@inheritDoc}  
     */ 
    public void fill(Canvas canvas, Paint paint) {
        paint.setStyle(Paint.Style.FILL);
        canvas.drawArc(mOval, mStartAngle, mAngleExtent, mUseCenter, paint);
    } 
 
    /** 
     * {@inheritDoc}  
     */ 
    public void fillAndStroke(Canvas canvas, Paint paint) {
        paint.setStyle(Paint.Style.FILL_AND_STROKE);
        canvas.drawArc(mOval, mStartAngle, mAngleExtent, mUseCenter, paint);
    } 
 
    /** 
     * {@inheritDoc}  
     */ 
    public void clip(Canvas canvas) {
        canvas.clipPath(mPath);
    } 
 
    /** 
     * {@inheritDoc}  
     */ 
    public boolean contains(RectShape rect) {
        return contains(rect.getX(), rect.getY(), rect.getWidth(), rect.getHeight());
    } 
 
    /** 
     * {@inheritDoc}  
     */ 
    public boolean contains(float x, float y) {
        return mRegion.contains((int)x, (int)y);
    } 
 
    /** 
     * {@inheritDoc}  
     */ 
    public boolean contains(float x, float y, float width, float height) {
        Region tmpRegion = new Region();
        Region targetRegion = new Region((int)x, (int)y, (int)(width + x), (int)(height + y));
        tmpRegion.op(this.mRegion, targetRegion, Op.INTERSECT );
        return ! targetRegion.op(tmpRegion, Op.XOR);
    } 
 
    /** 
     * {@inheritDoc}  
     */ 
    public boolean contains(PointF point) {
        return mRegion.contains((int)point.x, (int)point.y);
    } 
 
    /** 
     * {@inheritDoc}  
     */ 
    @Deprecated 
    public RectShape getBounds() {
        return new RectShape(mRegion.getBounds());
    } 
 
    /** 
     * {@inheritDoc}  
     */ 
    public void getBounds(RectShape rect) {
        rect.setRect(mRegion.getBounds());
    } 
 
    /** 
     * {@inheritDoc}  
     */ 
    public boolean intersects(float x, float y, float width, float height) {
        Region region = new Region(mRegion);
        float tmpx, tmpy, tmpw, tmph;
 
        if (x < width + x) {
            tmpx = x;
            tmpw = width;
        } 
        else{ 
            tmpx = width + x;
            tmpw = Math.abs(width); 
        } 
        if (y < height + y) {
            tmpy = y;
            tmph = height;
        } 
        else{ 
            tmpy = height + y;
            tmph = Math.abs(height); 
        } 
        return region.op((int)tmpx, (int)tmpy, (int)(tmpx + tmpw), (int)(tmpy + tmph), Op.INTERSECT);
    } 
 
    /** 
     * {@inheritDoc}  
     */ 
    public boolean intersects(Rect rect) {
        return intersects(rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top);
    } 
 
    /** 
     * {@inheritDoc}  
     */ 
    public boolean intersects(RectShape rect) {
        return intersects(rect.getX(), rect.getY(), rect.getWidth(), rect.getHeight());
    } 
     
    /** 
     * {@inheritDoc}  
     */ 
    public void translate(float x, float y) {
        this.mOval.left += x;
        this.mOval.right += x;
        this.mOval.top += y;
        this.mOval.bottom += y;
        update(); 
    } 
     
    /** 
     * Sets arc shape from specified location, size, float angles, and useCenter values. 
     * @param arcRect The bounding rectangle that defines the outer boundary of the full oval of which this arc is a partial section 
     * @param startAngle The starting angle of the arc in degrees 
     * @param antleExtent The angle extent of the arc in degrees 
     * @param useCenter If true, include the center of the oval in the arc, and close it if it is being stroked 
     */ 
    public void setArc(RectShape arcRect, float startAngle, float antleExtent, boolean useCenter) {
        this.mOval = new RectF(arcRect.getRectF());
        this.mStartAngle = startAngle;
        this.mAngleExtent = antleExtent;
        this.mUseCenter = useCenter;
        update(); 
    } 
     
    /** 
     * Sets arc shape from specified location, size, double angles, and useCenter values. 
     * @param arcRect The bounding rectangle that defines the outer boundary of the full oval of which this arc is a partial section. 
     * @param startAngle The starting angle of the arc in degrees. 
     * @param angleExtent The angle extent of the arc in degrees. 
     * @param useCenter If true, include the center of the oval in the arc, and close it if it is being stroked. 
     */ 
    public void setArc(RectShape arcRect, double startAngle, double angleExtent, boolean useCenter) {
        this.mOval = new RectF(arcRect.getRectF());
        this.mStartAngle = (float)startAngle;
        this.mAngleExtent = (float)angleExtent;
        this.mUseCenter = useCenter;
        update(); 
    } 
 
    /** 
     * Returns this x-coordinate. 
     * @return this x-coordinate 
     */ 
    public float getX() { 
        return mOval.left;
    } 
 
    /** 
     * Returns this y-coordinate. 
     * @return this y-coordinate 
     */ 
    public float getY() { 
        return mOval.top;
    } 
 
    /** 
     * Returns this width. 
     * @return this width 
     */ 
    public float getWidth() { 
        return mOval.width();
    } 
 
    /** 
     * Returns this height. 
     * @return this height 
     */ 
    public float getHeight() { 
        return mOval.height();
    } 
 
    /** 
     * Returns minimum x-coordinate of this arc shape. 
     * @return minimum x-coordinate of this arc shape 
     */ 
    public float getMinX() { 
        return mOval.left;
    } 
     
    /** 
     * Returns maximum x-coordinate of this arc shape. 
     * @return maximum x-coordinate of this arc shape 
     */ 
    public float getMaxX() { 
        return mOval.right;
    } 
     
    /** 
     * Returns minimum y-coordinate of this arc shape. 
     * @return minimum y-coordinate of this arc shape 
     */ 
    public float getMinY() { 
        return mOval.top;
    } 
 
    /** 
     * Returns maximum y-coordinate of this arc shape. 
     * @return maximum y-coordinate of this arc shape 
     */ 
    public float getMaxY() { 
        return mOval.bottom;
    } 
 
    /** 
     * Returns center x-coordinate of this arc shape. 
     * @return center x-coordinate of this arc shape 
     */ 
    public float getCenterX() { 
        return mOval.centerX();
    } 
     
    /** 
     * Returns center y-coordinate of this arc shape. 
     * @return center y-coordinate of this arc shape 
     */ 
    public float getCenterY() { 
        return mOval.centerY();
    } 
     
    /** 
     * Returns start angle of the arc in degrees. 
     * @return The start angle of the arc in degrees 
     */ 
    public float getAngleStart() { 
        return mStartAngle;
    } 
 
    /** 
     * Sets start angle of the arc in degrees. 
     * @param degree The start angle of the arc in degrees 
     */ 
    public void setAngleStart(float degree) {
        this.mStartAngle = degree;
        update(); 
    } 
 
    /** 
     * Returns angle extent of the arc in degrees. 
     * @return The angle extent of the arc in degrees 
     */ 
    public float getAngleExtent() { 
        return mAngleExtent;
    } 
 
    /** 
     * Sets angle extent of the arc in degrees. 
     * @param degree The angle extent of the arc in degrees 
     */ 
    public void setAngleExtent(float degree) {
        this.mAngleExtent = degree;
        update(); 
    } 
 
    /** 
     * Gets coordinate of arc start point. 
     * @return The coordinate of arc start point 
     */ 
    public PointF getStartPoint() {
        return new PointF(mStartPoint.x, mStartPoint.y);
    } 
 
    /** 
     * Gets coordinate of arc end point. 
     * @return The coordinate of arc end point 
     */ 
    public PointF getEndPoint() {
        return new PointF(mEndPoint.x, mEndPoint.y);
    } 
 
    /** 
     * Returns true, if include the center of the oval in the arc, and close it if it is being stroked. 
     * @return mUseCenter If true, include the center of the oval in the arc, and close it if it is being stroked. 
     */ 
    public boolean isUseCenter() { 
        return mUseCenter;
    } 
     
    /** 
     * Sets mUseCenter of arc shape. 
     * @param useCenter If true, include the center of the oval in the arc, and close it if it is being stroked. 
     */ 
    public void setUseCenter(boolean useCenter) {
        this.mUseCenter = useCenter;
    } 
     
    /** 
     * Updates the internal data required to draw. 
     */ 
    private void update() { 
        mPath.reset();
         
        mStartPoint.x = (float)(Math.sin(Math.toRadians(mStartAngle + 90.0)) * (mOval.width() / 2.0)) + getCenterX();
        mStartPoint.y = (float)(-Math.cos(Math.toRadians(mStartAngle + 90.0)) * (mOval.height() / 2.0)) + getCenterY();
        mEndPoint.x = (float)(Math.sin(Math.toRadians(mStartAngle + mAngleExtent + 90.0)) * (mOval.width() / 2.0)) + getCenterX();
        mEndPoint.y = (float)(-Math.cos(Math.toRadians(mStartAngle + mAngleExtent + 90.0)) * (mOval.height() / 2.0)) + getCenterY();
         
        mPath.arcTo(mOval, mStartAngle, mAngleExtent);
        if (mUseCenter) {
            mPath.lineTo(mOval.centerX(), mOval.centerY());
        } 
        mPath.close();
        mRegion = new Region((int)mOval.left, (int)mOval.top, (int) mOval.right, (int) mOval.bottom);
        mRegion.setPath(mPath, mRegion);
    }