Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

minimize the height when use just half circle (forexample) #49

Open
alkiswanee opened this issue Aug 28, 2016 · 1 comment
Open

minimize the height when use just half circle (forexample) #49

alkiswanee opened this issue Aug 28, 2016 · 1 comment

Comments

@alkiswanee
Copy link

my application just need 120deg from the circle , but the script need the full circle diameter as a layaoutframe height as in the shape :
1
total height of arc area = (min (height,width)-padding/2) >> arc diameter ..
but height 2 is not scrollable in (scroll view) .. and i dont need this area because my arc is not a full circle ..
how i can erase this (2) area .. or make it scrollable in framelayout ??

@longshishui
Copy link

longshishui commented Jun 15, 2022

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants