我尝试绘制一些饼图,但结果不同:
这是在Android 4.3上(正确的变体)
在Android 4.1上接下来(不正确的变种):
我的代码如下:
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
// int saveFlag = Canvas.MATRIX_SAVE_FLAG | Canvas.CLIP_SAVE_FLAG | Canvas.HAS_ALPHA_LAYER_SAVE_FLAG
// | Canvas.FULL_COLOR_LAYER_SAVE_FLAG | Canvas.CLIP_TO_LAYER_SAVE_FLAG;
// int sc = canvas.saveLayer(0, 0, getWidth(), getHeight(), null, saveFlag);
// canvas.translate(0, 0);
float temp = 0;
float values[] = {700, 500, 300, 444, 222};
values = calculateData(values);
float[] value_degree;
value_degree = new float[values.length];
for (int i = 0; i < values.length; i++) {
value_degree[i] = values[i];
}
int height = getHeight();
int width = getWidth();
Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mPaint.setStrokeWidth(20f);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setColor(getResources().getColor(R.color.circle_background));
int radius = (int) ((height / 2) + (Math.pow((double) width, 2) / (8 * height))) / 2;
float left = width / 2 - radius;
float top = height / 2 - radius;
float right = width / 2 + radius;
float bottom = height / 2 + radius;
RectF rectF = new RectF(left, top, right, bottom);
canvas.drawOval(rectF, mPaint);
rectF.set(left + 11f, top + 11f, right - 11f, bottom - 11f);
mPaint.setColor(Color.GRAY);
mPaint.setStyle(Paint.Style.FILL);
Random r;
for (int i = 0; i < value_degree.length; i++) {
if (i == 0) {
r = new Random();
int color = Color.argb(100, r.nextInt(256), r.nextInt(256),
r.nextInt(256));
mPaint.setColor(color);
canvas.drawArc(rectF, 270, value_degree[i], true, mPaint);
} else {
if (i == 1) {
temp += value_degree[i - 1] + 270 - 360;
} else {
temp += value_degree[i - 1];
}
r = new Random();
int color = Color.argb(255, r.nextInt(256), r.nextInt(256),
r.nextInt(256));
mPaint.setColor(color);
canvas.drawArc(rectF, temp, value_degree[i], true, mPaint);
}
}
mPaint.setColor(getResources().getColor(R.color.circle_background_small));
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeWidth(20f);
canvas.drawCircle(rectF.centerX(), rectF.centerY(), radius / 1.8f, mPaint);
// mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
mPaint.setColor(getResources().getColor(android.R.color.transparent));
mPaint.setAlpha(1);
mPaint.setStyle(Paint.Style.FILL);
canvas.drawCircle(rectF.centerX(), rectF.centerY(), radius / 1.9f, mPaint);
}
我做错了什么?
的此致
附:中心应该是带有活动背景的透明圆圈,为此我使用PorterDuffXfermode并恢复图层。但现在是评论。
答案 0 :(得分:1)
在画布上绘图时,指定的大小以像素为单位,而不是与密度无关的像素(dp或dip)。为了缩放值,您应该指定所需的大小,并将其乘以屏幕的密度。
mPaint.setStrokeWidth(20f * getResources().getDisplayMetrics().density);
(尽管您可能希望先在某处缓存密度)。
这将根据屏幕的密度缩放笔画宽度,因此宽度应该都匹配。