user3552603
user3552603

Reputation:

Why drawing in android sometimes shows different

I am sorry I did not know how to formulate the question but you will understand from the pictures. Feel free to edit the title.

When I run the program usually shows this:

But sometimes I can see this:

Why is that happening? I am not posting code because I dont do anything dynamicaly I use always the same method with the same values so it is not important but if you need just tell me.

Any idea?

EDIT:

CODE:

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

}

Upvotes: 1

Views: 83

Answers (1)

user3552603
user3552603

Reputation:

Moving this part of code to onWindowFocusChanged() method helped:

// 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));

Upvotes: 1

Related Questions