所以目前我有一个可绘制的对象,我创建的本质上是一个圆圈,我唯一的问题是我不能阻止它超过它的父容器,超过我的意思是超出它的父视图,以便它的边缘不再可见。
我的绘画:
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>
我如何设置背景:
progressViewPartialArc.setBackground(new ProgressArc(color, getPercent(), 12));
我的意思截图:
那个圆圈应该是一个完美的圆圈,但我不知道为什么它不会?
答案 0 :(得分:1)
看起来你需要考虑边界的笔划宽度:
oval.set(bounds);
oval.inset(strokeWidth / 2, strokeWidth / 2);