Android 重新绘制画布,用于更新,然后在画布上设置文本不起作用
我有画饼图的画布。将单选按钮更改为“不同意”位置后,饼图必须显示不同的值。我是和听众一起做这件事的(建立在广播组上)。它正在工作,但为了更新画布(不是添加和过度绘制,我必须先清除它并用新值重新绘制),我使用以下方法:Android 重新绘制画布,用于更新,然后在画布上设置文本不起作用,android,canvas,Android,Canvas,我有画饼图的画布。将单选按钮更改为“不同意”位置后,饼图必须显示不同的值。我是和听众一起做这件事的(建立在广播组上)。它正在工作,但为了更新画布(不是添加和过度绘制,我必须先清除它并用新值重新绘制),我使用以下方法: canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR); 在图表下面,我想写下百分比的数字。它在没有上面那条线的情况下工作。但在我执行该行之后,只有piechart被重新绘制(使用新值),但不显示带有百分比的文本(即使
canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
在图表下面,我想写下百分比的数字。它在没有上面那条线的情况下工作。但在我执行该行之后,只有piechart被重新绘制(使用新值),但不显示带有百分比的文本(即使我再次将文本设置为该百分比):
计算百分比并将其写入的方法:
public void setupPercentageValueToGraph() {
float[] degrees = calculateData(values);
// get percentage number from values
float percentage = (degrees[0] / (degrees[0] + degrees[1])) * 100;
// setup color of the number shown red/green
if (percentage >= 50) {
percentageTV.setTextColor(Color.parseColor("#47B243"));
} else {
percentageTV.setTextColor(Color.parseColor("#DB262A"));
}
// set the text
percentageTV.setText((int) percentage + "%");
}
将piechart设置为linearLayout的方法:
public void setupPieChart() {
float[] degrees = calculateData(values);
graphLayout.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
graphLayout.addView(new MyGraphView(this, degrees, size));
}
类,该类正在绘制使用清除的图表:
public class MyGraphView extends View {
public static final int PADDING = 4;
private Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
private float[] value_degree;
int strokeWidth;
private int[] COLORS = { Color.parseColor("#47B243"),
Color.parseColor("#DB262A") };
// size of bigger half circle
RectF rectf = new RectF(2, 2, 62, 62);
// size of smaller half circle
RectF rectf2 = new RectF(9, 9, 55, 55);
// size of the smallest half circle
RectF rectf3 = new RectF(16, 16, 48, 48);
int temp = 0;
public MyGraphView(Context context, float[] values, int size) {
super(context);
// setting up size of pie chart dynamically
int difference = size / 9;
rectf.set(PADDING, PADDING, size + PADDING, size + PADDING);
rectf2.set(difference + PADDING, difference + PADDING, size
- difference + PADDING, size - difference + PADDING);
rectf3.set(difference * 2 + PADDING, difference * 2 + PADDING, size
- difference * 2 + PADDING, size - difference * 2 +
PADDING);
// setting up brush size
strokeWidth = size / 15;
// assign degrees of agree and disagree to array
value_degree = new float[values.length];
for (int i = 0; i < values.length; i++) {
value_degree[i] = values[i];
}
}
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
//canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
for (int i = 0; i < value_degree.length; i++) {
// set type of "brush"
paint.setStrokeWidth(strokeWidth);
paint.setStyle(Paint.Style.STROKE);
// set shadow
paint.setShadowLayer(2, 1, 1, Color.BLACK);
// setLayerType(LAYER_TYPE_SOFTWARE, paint);
// agree
if (i == 0) {
final Path path = new Path();
paint.setColor(COLORS[i]);
// draw 3 paths to show 3 curves
path.addArc(rectf, 180, value_degree[i] - 4);
path.addArc(rectf2, 180, value_degree[i] - 5);
path.addArc(rectf3, 180, value_degree[i] - 6);
// draw the path
canvas.drawPath(path, paint);
// disagree
} else {
temp += (int) value_degree[i - 1];
paint.setColor(COLORS[i]);
final Path path = new Path();
path.addArc(rectf, temp + 180 + 4, value_degree[i] - 4);
path.addArc(rectf2, temp + 180 + 5, value_degree[i] - 5);
path.addArc(rectf3, temp + 180 + 6, value_degree[i] - 6);
// draw the path
canvas.drawPath(path, paint);
}
}
}
}
公共类MyGraphView扩展视图{
公共静态最终整数填充=4;
私有油漆=新油漆(油漆.防油漆别名标志);
私有浮动[]值_度;
内冲程宽度;
private int[]COLORS={Color.parseColor(#47B243”),
parseColor(#DB262A”)};
//大半圆的尺寸
RectF RectF=新的RectF(2,2,62,62);
//小半圆的尺寸
RectF rectf2=新的RectF(9,9,55,55);
//最小半圆的大小
RectF rectf3=新的RectF(16,16,48,48);
内部温度=0;
公共MyGraphView(上下文上下文、浮点[]值、整数大小){
超级(上下文);
//动态设置饼图的大小
内部差异=大小/9;
rectf.set(填充,填充,大小+填充,大小+填充);
设置(差异+填充,差异+填充,大小
-差异+填充,大小-差异+填充);
rectf3.set(差分*2+填充,差分*2+填充,大小
-差异*2+填充,大小-差异*2+
填充物);
//设置笔刷大小
冲程宽度=尺寸/15;
//为数组指定同意和不同意的程度
value_degree=新浮点[values.length];
对于(int i=0;i
你知道为什么不写文本吗?或者在radiobutton被更改后如何更新piechart?最后我找到了一种方法。要在绘制图形时在同一画布上绘制文本,请执行以下操作:
// draw the number of percent under piechart
// setup brush
Paint paint2 = new Paint(Paint.ANTI_ALIAS_FLAG);
// set shadow
paint2.setShadowLayer(3, 2, 2, Color.parseColor("#404040"));
// fill it not stroke
paint2.setStyle(Style.FILL);
// set text family + make it bold
paint2.setTypeface(Typeface.create(Typeface.DEFAULT, Typeface.BOLD));
// convert dp to pixels and setup the size of text
int MY_DIP_VALUE = 23; // 5dp
int pixel = (int) TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP, MY_DIP_VALUE,
getResources()
.getDisplayMetrics());
// setup color of the number shown red/green
if (percent >= 50) {
paint2.setColor(COLORS[0]);
} else {
paint2.setColor(COLORS[1]);
}
// draw the text
paint2.setTextSize(pixel);
float textWidth = paint2.measureText(percent + "%");
canvas.drawText(percent + "%", size / 2 - textWidth / 2 + PADDING,
size, paint2);
我已经删除了TextView并做了一些小改动,但关键是要绘制它。仍然不明白为什么我不能在上面写文章,但必须画画,但它正在工作
// draw the number of percent under piechart
// setup brush
Paint paint2 = new Paint(Paint.ANTI_ALIAS_FLAG);
// set shadow
paint2.setShadowLayer(3, 2, 2, Color.parseColor("#404040"));
// fill it not stroke
paint2.setStyle(Style.FILL);
// set text family + make it bold
paint2.setTypeface(Typeface.create(Typeface.DEFAULT, Typeface.BOLD));
// convert dp to pixels and setup the size of text
int MY_DIP_VALUE = 23; // 5dp
int pixel = (int) TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP, MY_DIP_VALUE,
getResources()
.getDisplayMetrics());
// setup color of the number shown red/green
if (percent >= 50) {
paint2.setColor(COLORS[0]);
} else {
paint2.setColor(COLORS[1]);
}
// draw the text
paint2.setTextSize(pixel);
float textWidth = paint2.measureText(percent + "%");
canvas.drawText(percent + "%", size / 2 - textWidth / 2 + PADDING,
size, paint2);