Java Android 用 2 种颜色绘制圆圈(饼图)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19731261/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android draw circle with 2 colors (Pie chart)
提问by patsud
This is my first question here at stackoverflow.com so excuse myself if i'm doing stuff wrong.
这是我在 stackoverflow.com 上的第一个问题,所以如果我做错了,请原谅我自己。
I want to create a circle which basically is like a progress bar. Now I'd like to set the percentage through some code.
我想创建一个基本上像进度条的圆圈。现在我想通过一些代码设置百分比。
What I want to achieve is: https://raw.github.com/psud/Melde-App/master/res/drawable-hdpi/circlemiddle.png
我想要实现的是:https: //raw.github.com/psud/Melde-App/master/res/drawable-hdpi/circlemiddle.png
My problems:
我的问题:
- Can't get a circle with two colors to work (have been searching forums for hours and have found solutions to problems similar to mine but I just can't implement those solutions that into my app. I've read a lot about canvas.drawArc(...) but can't seem to find out how to use it).
- How is it possible to put a canvas into a layout? (I've got a xml layout and the canvas should be drawn inside a particular layout without changing the rest of the layout).
- 无法使用两种颜色的圆圈(已经在论坛上搜索了几个小时并找到了与我类似的问题的解决方案,但我无法将这些解决方案实施到我的应用程序中。我已经阅读了很多关于画布的内容。 drawArc(...) 但似乎无法找到如何使用它)。
- 如何将画布放入布局中?(我有一个 xml 布局,画布应该在特定布局内绘制,而不更改布局的其余部分)。
Thanks.
谢谢。
采纳答案by Sherif elKhatib
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.
这只是一个提示。它只是一个在同一个矩形中绘制两条弧线的视图:第一条弧线的跨度从角度 0 到 360。第二个(在第一个上方)从 0 度到取决于百分比的角度。
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" />
回答by Kai
You can implement a pie chart quite easily with this library (achartengine - https://code.google.com/p/achartengine/) instead of rolling your own solution.
您可以使用此库 (achartengine - https://code.google.com/p/achartengine/)轻松实现饼图,而无需滚动您自己的解决方案。