代码:
double percentage = 0.7711627906976745;
mSweepAngle = (float)(360*(percentage/(double)100));
我已经google了一下,但没有找到答案,afaik我正确地输入它,但它仍然评估为0.0。我甚至尝试在计算过程中将360
,percentage
和100
强制转换为float,但它仍然不起作用...
EDIT1: 完整的代码:
package se.kiendys.petrus.da171a.uppg3.budgetapp;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Paint.Style;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
public class PieChartView extends View {
private Paint piechartItem1Paint;
private Paint piechartItem2Paint;
private Paint piechartBorderPaint;
private Paint infoBorderPaint;
private Paint infoBackgroundPaint;
private Paint infoTextPaint;
private RectF oval;
private int mWidth;
private int mHeight;
private int mMarginLeft;
private int mMarginTop;
private float mStartAngle;
private float mSweepAngle;
private double percentage;
public PieChartView(Context context) {
super(context);
init();
}
public PieChartView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public void init() {
piechartItem1Paint = new Paint(); // the piechart items color
piechartItem2Paint = new Paint(); // the piechart items color
piechartBorderPaint = new Paint(); // the piechart border color
infoTextPaint = new Paint(); // the info text color
infoBackgroundPaint = new Paint(); // the info background color
infoBorderPaint = new Paint(); // the info border color
mWidth = 200; // circle width
mHeight = 200; // circle height
mMarginLeft = 10; // circle and info margin
mMarginTop = 10; // circle and info margin
mStartAngle = 0; // the starting angle of the arc, begins at the rightmost part of the circle (an angle of 0 degrees correspond to the geometric angle of 0 degrees, or 3 o'clock on a watch)
// and through incrementation moves clockwise, the units are degrees
oval = new RectF(mMarginLeft, mMarginTop, mWidth, mHeight); // sets the shape and size (boundaries) of the drawn circle
piechartItem1Paint.setAntiAlias(true);
piechartItem1Paint.setStyle(Style.FILL);
piechartItem1Paint.setStrokeWidth(0.5f);
piechartItem2Paint.setAntiAlias(true);
piechartItem2Paint.setStyle(Style.FILL);
piechartItem2Paint.setStrokeWidth(0.5f);
piechartItem1Paint.setColor(0xCCFEFEFE); // blue color
piechartItem2Paint.setColor(0xCC343434); // green color
// double temp = (360*((double)percentage/(double)100));
// Log.d(BudgetConstants.DEBUG_TAG, "temp: "+temp);
// mSweepAngle = (float)temp;
// Log.d(BudgetConstants.DEBUG_TAG, "init mSweepAngle: "+mSweepAngle);
// mSweepAngle = (float)(360*(percentage/(double)100));
// mSweepAngle = (float)(percentage/100.0*360);
Log.d(BudgetConstants.DEBUG_TAG, "init mSweepAngle: "+mSweepAngle);
}
public void setDistributionPercentage(double percentage) {
this.percentage = percentage;
Log.d(BudgetConstants.DEBUG_TAG, "percentage: "+percentage);
}
protected void onDraw(Canvas canvas) {
Log.d(BudgetConstants.DEBUG_TAG, "onDraw fired!");
canvas.drawArc(oval, mStartAngle, mSweepAngle, true, piechartItem1Paint);
Log.d(BudgetConstants.DEBUG_TAG, "startAngle1: "+mStartAngle+", sweepAngle1: "+mSweepAngle);
canvas.drawArc(oval, mSweepAngle, (360-mSweepAngle), true, piechartItem2Paint);
Log.d(BudgetConstants.DEBUG_TAG, "startAngle2: "+mSweepAngle+", sweepAngle2: "+(360-mSweepAngle));
}
}
请注意,百分比已成功从另一项活动传递,目前正在通过0.7711627906976745.
答案 0 :(得分:2)
您正在从构造函数中调用init
,并且正在进行此计算的init
。因此,无论您使用什么机制来设置percentage
,它都不会发生,直到计算完成后才会发生。
我认真地建议学习使用调试器。单步执行代码会立即告诉您发生了什么。