简体   繁体   English

视Android版本而异

[英]Different views depending on android versions

I try to draw some Pie Chart graph but got different result: 我尝试绘制一些饼图图形,但结果不同:
This is on Android 4.3(correct variant) 这是在Android 4.3(正确的变体)上
在此处输入图片说明
On Android 4.1 get next(incorrect variant): 在Android 4.1上,获取next(不正确的变体):
在此处输入图片说明


My code below: 我的代码如下:

@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);
}


What I doing wrong? 我做错了什么?
Regards 问候
PS On center should be transparent circle with background of activity, for this I use PorterDuffXfermode and restoring layers. PS中心应该是带有活动背景的透明圆圈,为此,我使用PorterDuffXfermode和还原层。 But now it's comment on. 但是现在它在评论。

When drawing on the canvas, the sizes specified are in pixels rather than density independent pixels (dp or dip). 在画布上绘制时,指定的尺寸以像素为单位,而不是与密度无关的像素(dp或dip)。 In order to scale the values you should specify the size you want and multiply it by the screen's density. 为了缩放值,您应该指定所需的大小,然后将其乘以屏幕的密度。

mPaint.setStrokeWidth(20f * getResources().getDisplayMetrics().density);

(Although you might want to cache the density somewhere first). (尽管您可能想先将密度缓存在某处)。

This will scale the stroke width depending upon the screen's density, and thus the widths should all match. 这将根据屏幕的密度缩放笔划宽度,因此宽度应全部匹配。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM