gpt4 book ai didi

android - 使用 Canvas 在弧内绘制文本

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:07:02 33 4
gpt4 key购买 nike

我正在一个 android 应用程序中工作以绘制一个圆圈并将它们平均划分并将文本绑定(bind)到圆圈的划分部分内(如图片)。我画了一个圆圈并将它们平均分割,但我想在分割的部分内绑定(bind)文本。请查看我的代码并给出解决方案。提前致谢。

enter image description here

public class MainActivity extends Activity {
/** Called when the activity is first created. */
float values[] = { 130, 130, 130, 130, 130 };

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LinearLayout linear = (LinearLayout) findViewById(R.id.linearlay);
values = calculateData(values);
linear.addView(new MyGraphview(this, values));
}

private float[] calculateData(float[] data) {

float total = 0;
for (int i = 0; i < data.length; i++) {
total += data[i];
}
for (int i = 0; i < data.length; i++) {
data[i] = 360 * (data[i] / total);
}
return data;

}

public class MyGraphview extends View {

private Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
private float[] value_degree;
private int[] COLORS = { Color.YELLOW, Color.GREEN, Color.WHITE,
Color.CYAN, Color.RED };
RectF rectf = new RectF(10, 10, 300, 300);
Rect rect = new Rect(10, 10, 300, 300);
int temp = 0;
String rotatedtext;
Path path;

public MyGraphview(Context context, float[] values) {
super(context);
path = new Path();
value_degree = new float[values.length];
for (int i = 0; i < values.length; i++) {
value_degree[i] = values[i];
}

paint.setTextSize(16);
rotatedtext = "Rotated :)";
}

@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);

for (int i = 0; i < value_degree.length; i++) {
if (i == 0) {

paint.setColor(COLORS[i]);
canvas.drawArc(rectf, 0, value_degree[i], true, paint);

} else {

temp += (int) value_degree[i - 1];
paint.setColor(COLORS[i]);
canvas.drawArc(rectf, temp, value_degree[i], true, paint); //

}
}

}

}
}

最佳答案

试试这个:

private String[] STRINGS = { "Yellow", "GREEN", "WHITE", "CYAN", "RED" }; // Array of strings, just for the sample

@Override
protected void onDraw(Canvas canvas)
{
super.onDraw(canvas);

temp = 0;

int centerX = (rect.left + rect.right) / 2;
int centerY = (rect.top + rect.bottom) / 2;
int radius = (rect.right - rect.left) / 2;

radius *= 0.5; // 1 will put the text in the border, 0 will put the text in the center. Play with this to set the distance of your text.

for (int i = 0; i < data.length; i++)
{
if (i > 0)
temp += (int) data[i - 1]; // rewrote your code here a bit, to avoid duplicate code.

paint.setColor(COLORS[i]);
canvas.drawArc(rectf, temp, data[i], true, paint);

paint.setColor(Color.BLACK); // set this to the text color.
float medianAngle = (temp + (data[i] / 2f)) * (float)Math.PI / 180f; // this angle will place the text in the center of the arc.
canvas.drawText(STRINGS[i], (float)(centerX + (radius * Math.cos(medianAngle))), (float)(centerY + (radius * Math.sin(medianAngle))), paint);
}
}

此外,为了获得更好的效果,请确保在绘制任何文本之前在绘图中设置 Align.CENTER 属性:

paint.setTextSize(16);
paint.setTextAlign(Align.CENTER);

希望这有帮助:)

关于android - 使用 Canvas 在弧内绘制文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15739009/

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