android - 为什么在android中绘图有时会显示不同

标签 android

很抱歉,我不知道如何提出问题,但您会从图片中理解。随意编辑标题。

当我运行程序时通常会显示:

但有时我会看到这个:

为什么会这样?我没有发布代码,因为我没有做任何动态的事情,我总是使用相同的方法和相同的值,所以这并不重要,但如果你需要,请告诉我。

有什么想法吗?

编辑:

代码:

public class MyGraphView extends View {
    private Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    private float[] value_degree;
    private int[] COLORS = { Color.GREEN, Color.RED };
    // size of bigger half circle
    RectF rectf = new RectF(10, 10, 310, 310);
    // size of smaller half circle
    RectF rectf2 = new RectF(45, 45, 275, 275);
    // size of the smallest half circle
    RectF rectf3 = new RectF(80, 80, 240, 240);

    int temp = 0;

    public MyGraphView(Context context, float[] values) {

        super(context);
        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);


        for (int i = 0; i < value_degree.length; i++) {
            // set type of "brush"
            paint.setStrokeWidth(20);
            paint.setStyle(Paint.Style.STROKE);
                    // 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);
            }

        }
    }
}

Activity :

    public class PieChart extends Activity {
    float values[] = { 10, 20 };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.pie_chart);
        // pie chart assigned to linear layout
        LinearLayout linear = (LinearLayout) findViewById(R.id.pieChartLL);
        values = calculateData(values);
        // draw the graf
        linear.addView(new MyGraphView(this, values));

    }

    // for pie chart
    private float[] calculateData(float[] data) {
        // TODO Auto-generated method stub
        float total = 0;
        for (int i = 0; i < data.length; i++) {
            total += data[i];
        }
        // 180 is the amount of degrees of the circle - setting it up to half
        // circle 360 means full circle
        for (int i = 0; i < data.length; i++) {
            data[i] = 180 * (data[i] / total);
        }

        return data;
    }

}

最佳答案

将这部分代码移动到 onWindowFocusChanged() 方法有助于:

// pie chart assigned to linear layout
    LinearLayout linear = (LinearLayout) findViewById(R.id.pieChartLL);
    values = calculateData(values);
    // draw the graf
    linear.addView(new MyGraphView(this, values));

关于android - 为什么在android中绘图有时会显示不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23229880/

相关文章:

android - 如何通过 Intent 将图像分享到 Facebook

android - 使用安卓相机

android - "Injecting the input artifact of a transform as a File has been deprecated."

android - 如何设置单个gridview单元格的背景颜色

android - 通过 PendingIntent 添加新任务到 JobScheduler

android - 将文件保存到android中的内部存储器?

android - 我在哪里可以找到 Android 的默认图标?

android - 改造 body 返回空

java - 滑动删除 Android recyclerview 不起作用

java - Android Studio 中的 Osmand