Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redraw View Android

Tags:

android

view

I now have a View that is added programically after the onCreate (Depending on some other variables). Everything works as it should and it draws part of a circle.

But my question is how do i redraw it later on ? I need to change the angle in the circle after some data is fetched.

Code for the WindRose :

    public class WindRose extends View {

    public WindRose(Context context) {
        super(context);


    }

    @Override

    protected void onDraw(Canvas canvas) {


        super.onDraw(canvas);
        canvasTest = canvas;


        float height = (float) getHeight();
        float width = (float) getWidth();

        float radius;

        if (width > height) {
            radius = height / 2;

        } else {
            radius = width / 2;
        }

        // radius = (height )/ 2;


        Path path = new Path();
        path.addCircle(width, height, radius, Path.Direction.CCW);

        // / 2

        Resources resources = getResources();
        int color = resources.getColor(R.color.green_back);



        paint.setColor(color);
        paint.setStrokeWidth(5);

        paint.setStyle(Paint.Style.FILL);
        float center_x, center_y;
        center_x = width / 2;
        center_y = height / 2;



        //Formulas :
        //SD = Start Degree
        //ED = End Degree

        //If cakepiece passes 0 (East)
        //SD, 360-(SD+ED)

        //Else :
        //SD, (ED-SD)

        oval.set(center_x - radius, center_y - radius, center_x + radius, center_y + radius);

        if (End > Start) {
            canvas.drawArc(oval, Start, (End - Start), true, paint);

        } else if (End < Start) {
            canvas.drawArc(oval, Start, ((360 - Start) + End), true, paint);
        }


    }


}

If i update the Start and End variables nothing happens. i have also tried to call invalidate on the class but also there i dont get any redrawing.

Invalidate that i've tried :

WindRose windrose = new WindRose(this);
windrose.invalidate();

Was called from the main class which WindRose is part of.

How should i do this correctly ?

like image 800
Dukes Avatar asked Mar 06 '14 18:03

Dukes


1 Answers

Maybe calling the invalidate() method of the view will help.

You can read more here(http://developer.android.com/reference/android/view/View.html), but: "To force a view to draw, call invalidate()."

Note, also that you can invalidate only parts of a view

like image 61
peshkira Avatar answered Sep 22 '22 18:09

peshkira