这这是我的第一个问题,在stackoverflow.com所以原谅自己,如果我做的东西是错误的。
This is my first question here at stackoverflow.com so excuse myself if i'm doing stuff wrong.
我想创建一个圈子基本上就像一个进度条。现在我想通过设置一些code中的比例。
I want to create a circle which basically is like a progress bar. Now I'd like to set the percentage through some code.
我想实现的是:的https://m.xsw88.com/allimgs/daicuo/20230904/279.png
我的问题:
在无法获得与两种颜色的圆形工作(一直在寻找论坛小时,已经找到解决方案,与我相似的问题,但我就是无法实现这些解决方案,为我的应用程序,我已经读了很多关于canvas.drawArc(...),但似乎无法找出如何使用它)。 这怎么可能把一个帆布成一个布局? (我有一个XML布局和画布应该在特定的布局中可以得出在不改变布局的其余部分)。感谢。
这只是一个提示。这仅仅是画两个圆弧在同一RECT一个观点:从角度0一弧跨越到360第二个(上面第一个),范围从0到要看百分比的角度
This is only a hint. It is simply a view that draw two arcs in the same rect: First arc spans from angle 0 to 360. The second one (above the first) spans from 0 to an angle that depends on the percentage.
public class PercentView extends View {
public PercentView (Context context) {
super(context);
init();
}
public PercentView (Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public PercentView (Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
private void init() {
paint = new Paint();
paint.setColor(getContext().getResources().getColor(R.color.lightblue));
paint.setAntiAlias(true);
paint.setStyle(Paint.Style.FILL);
bgpaint = new Paint();
bgpaint.setColor(getContext().getResources().getColor(R.color.darkblue));
bgpaint.setAntiAlias(true);
bgpaint.setStyle(Paint.Style.FILL);
rect = new RectF();
}
Paint paint;
Paint bgpaint;
RectF rect;
float percentage = 0;
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//draw background circle anyway
int left = 0;
int width = getWidth();
int top = 0;
rect.set(left, top, left+width, top + width);
canvas.drawArc(rect, -90, 360, true, bgpaint);
if(percentage!=0) {
canvas.drawArc(rect, -90, (360*percentage), true, paint);
}
}
public void setPercentage(float percentage) {
this.percentage = percentage / 100;
invalidate();
}
}
添加到您的布局:
Add to your layout:
<bla.bla.PercentView
android:id="@+id/percentview"
android:layout_width="100dp"
android:layout_height="100dp" />