Code Monkey home page Code Monkey logo

Comments (1)

longshishui avatar longshishui commented on July 17, 2024

Here is my solution for your reference. FYI.

1. Declare a global variable.
private int minSize = 0;
2. Create a method to calculate the height of the arc.
private int getArcHeight(int min) {
    int angle = 180 - mSweepAngle / 2;
    double arc = Math.PI * 2 / 360 * (angle);
    int arcHeight = (int) ((Math.cos(arc) + 1) * (min / 2));
    return arcHeight;
}
3. Override onMeasure() and give a min height.
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

    int height = getDefaultSize(getSuggestedMinimumHeight(),
            heightMeasureSpec);
    final int width = getDefaultSize(getSuggestedMinimumWidth(),
            widthMeasureSpec);
    final int min = Math.min(width, height);
    float top = 0;
    float left = 0;
    float right = 0;
    float bottom = 0;
    int arcDiameter = 0;
    minSize = min;
    arcDiameter = min - getPaddingLeft();

    mTranslateX = (int) (width * 0.5f);
    mTranslateY = (int) (min * 0.5f);

    mArcRadius = arcDiameter / 2;
    top = min / 2 - (arcDiameter / 2);
    left = width / 2 - (arcDiameter / 2);
    right = (left + arcDiameter);
    bottom = (top + arcDiameter);
    mArcRect.set(left, top, right, bottom);
    int newHeight = (int) (getArcHeight(minSize) + top);

    int arcStart = (int) mProgressSweep + mStartAngle + mRotation + 90;
    mThumbXPos = (int) (mArcRadius * Math.cos(Math.toRadians(arcStart)));
    mThumbYPos = (int) (mArcRadius * Math.sin(Math.toRadians(arcStart)));

    setTouchInSide(mTouchInside);
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);

    // Set view height
    setMeasuredDimension(width, newHeight);
}
4. Override onDraw() and adjust canvas size.
@Override
protected void onDraw(Canvas canvas) {
    if (!mClockwise) {
        canvas.scale(-1, 1, mArcRect.centerX(), mArcRect.centerY());
    }

    // Draw the arcs
    final int arcStart = mStartAngle + mAngleOffset + mRotation;
    final int arcSweep = mSweepAngle;

    // Erase the area 2
    double newHeight = getArcHeight(minSize) + getPaddingLeft() / 2;
    canvas.clipRect(0, 0, getMeasuredWidth(), (int) (newHeight), Region.Op.INTERSECT);
    // Set a conspicuous background, just for test.
    canvas.drawColor(Color.RED);
    canvas.drawArc(mArcRect, arcStart, arcSweep, false, mArcPaint);
    canvas.drawArc(mArcRect, arcStart, mProgressSweep, false,
            mProgressPaint);
    if (mEnabled) {
        // Draw the thumb nail
        canvas.translate(mTranslateX - mThumbXPos, mTranslateY - mThumbYPos);
        mThumb.draw(canvas);
    }
}

This is a demo screenshot.

The red rectangle represents the view's bounds.

S20615-13355729

from seekarc.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.