我有一个运行的线程从class1调用draw方法,它从class2调用另一个draw方法(下面发布的class2的draw方法)。
class2的draw方法运行一些代码并绘制圆圈,并调用一个名为drawtext的方法。在drawText方法中,它在canvas.drawText之前调用方法调用计算,但是其余的drawText方法(canvas.drawText的代码)不会被执行。
这怎么可能?我想当调用方法时,只是在不中断“父方法”的情况下调用它。
谢谢!
public void draw(Canvas canvas) {
paint = new Paint();
double HEIGHT = customView.HEIGHT;
double WIDTH = customView.WIDTH;
double margin = customView.margin;
r = WIDTH / 2 - margin;
for (int i = 0; i < 10; i++) {
double randX = getRN();
double randY = getRN();
if (Math.hypot(randX - r, randY - r) > r) {
paint.setColor(Color.WHITE);
} else {
paint.setColor(Color.BLACK);
count++;
}
//System.out.println(randX + " " + randY + " " + WIDTH + " " + HEIGHT);
//System.out.println((randX + margin) + " " + (HEIGHT / 2 - WIDTH / 2 + randY));
canvas.drawCircle((float) (randX + margin), (float) (randY + HEIGHT / 2 - r), 10, paint);
total++;
}
drawText(canvas);
}
public void drawText(Canvas canvas) {
paint.setColor(Color.BLACK);
paint.setTextSize(50);
paint.setTypeface(Typeface.create(Typeface.DEFAULT, Typeface.BOLD));
calculate();
canvas.drawText("Valid / Total: " + count + " / " + total + " (" + ratioPercentage + ")", 75, 91, paint);
/*BigDecimal countBig = new BigDecimal(count);
BigDecimal totalBig = new BigDecimal(total);
BigDecimal piCalculated = (countBig.divide(totalBig)).multiply(BigDecimal.valueOf(4));*/
canvas.drawText("Calculated \u03C0 : " + piCalculated, 75, 151, paint);
canvas.drawText("\"Actual\" \u03c0 : 3.1415926535" , 75, 211, paint);
//canvas.drawText("Error: " + ((piCalculated.divide(pi)).subtract(BigDecimal.valueOf(1))).multiply(BigDecimal.valueOf(100)) + " %",75, 210, paint);
canvas.drawText("Error: " + errorPercentage, 75, 271, paint);
}
public void calculate() {
ratio = (double)count / total;
ratioPercentage = df.format(ratio);
piCalculated = df1.format(ratio * 4);
error = Double.parseDouble(piCalculated) / 3.1415926535 - 1;
errorPercentage = df2.format(error);
best = mainActivity.getBest();
if (total > 5000 && Math.abs(error) < best) {
System.out.println("Evaluate: best update");
mainActivity.updateBest(error);
}
}