Joe Maher
Joe Maher

Reputation: 5460

Android - Stop drawable background from exceeding view

So at the moment i have a drawable object that i create which is essentially a circle, my only problem is is that i cannot stop it from exceeding its parent container, and by exceeding i mean going outside its parent view so that the edges of it are no longer visible.

My drawable:

public class ProgressArc extends Drawable {

private int color;
private float progress;
private int strokeWidth;

public ProgressArc(int color, float progress, int strokeWidth) {

    this.progress = progress * (float)3.6;
    this.color = color;
    this.strokeWidth = strokeWidth;
}

@Override
public void draw(Canvas canvas) {
    canvas.save();

    Rect bounds = getBounds();
    Paint paint = new Paint();
    RectF oval = new RectF();

    paint.setColor(this.color);
    paint.setStrokeCap(Paint.Cap.ROUND);
    paint.setStyle(Paint.Style.STROKE);
    paint.setStrokeWidth(strokeWidth);
    paint.setAntiAlias(true);

    oval.set(bounds);
    canvas.drawArc(oval, 90, this.progress, false, paint);
}

@Override
public void setAlpha(int alpha) {
    // Has no effect
}

@Override
public void setColorFilter(ColorFilter cf) {
    // Has no effect
}

@Override
public int getOpacity() {
    // Not Implemented
    return 0;
}

}

XML:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/progress_container"
    android:layout_width="150dp"
    android:layout_height="150dp">

       <View
           android:id="@+id/bill_progress_partial_bg"
           android:scaleType="fitXY"
           android:layout_width="fill_parent"
           android:layout_height="fill_parent"
           android:gravity="center_vertical" />

</FrameLayout>

How i set the background:

progressViewPartialArc.setBackground(new ProgressArc(color, getPercent(), 12));

Screenshot of what i mean:

screeny

that circle should be a perfect circle but i just cant work out why it wont?

Upvotes: 0

Views: 115

Answers (1)

Stefan Haustein
Stefan Haustein

Reputation: 18793

It looks like you need to take the stroke width into account for the bounds:

oval.set(bounds);
oval.inset(strokeWidth / 2, strokeWidth / 2);

Upvotes: 1

Related Questions