gpt4 book ai didi

java - Android - 阻止可绘制背景超出 View

转载 作者:行者123 更新时间:2023-12-01 11:07:50 25 4
gpt4 key购买 nike

所以目前我有一个我创建的可绘制对象,它本质上是一个圆圈,我唯一的问题是我无法阻止它超出其父容器,并且超出我的意思是超出其父 View ,以便它的边缘不再可见。

我的绘图:

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));

我的意思的屏幕截图:

screeny

那个圆应该是一个完美的圆,但我就是不明白为什么它不会?

最佳答案

看起来您需要考虑边界的笔划宽度:

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

关于java - Android - 阻止可绘制背景超出 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32735417/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com