0

I am using onDraw within customView. I am plotting two line. Red and Green.

     @Override
  public void onDraw(Canvas canvas) 
   {

        super.onDraw(canvas); 

             canvas.DrawLine(0, 0, 700, 200, Green);
             canvas.DrawLine(0,0, 700, 250, Red);
     }

On the button Click , I want to make Red Line InVisible and next click visible. I can,t redraw again.The line I have plotted , I have to make it visible and Invisible

Any Idea?

Kara
  • 6,115
  • 16
  • 50
  • 57
user2710860
  • 19
  • 1
  • 6
  • check this : http://stackoverflow.com/questions/15378493/how-to-partially-redraw-custom-surfaceview-using-separate-thread-without-losing – dreamcoder Aug 29 '13 at 11:41

1 Answers1

0

put a boolean in your custom view and make lines invisible/visible depending on that. Set the boolean in your onClick listener like so:

public class MyView extends View {

    boolean isVisible;

    //constructor etc.

    public boolean isVisible() {
        return isVisible;
    }

    public void setVisible(boolean visible) {
        isVisible = visible;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        super.onDraw(canvas);

        canvas.drawLine(0, 0, 700, 200, Green);

        if (isVisible)
            canvas.drawLine(0, 0, 700, 250, Red);

    }
}    

and the onClickListener would be:

button.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {

                myView.setVisible(!myView.isVisible());
                myView.invalidate();

            }
        });
Anup Cowkur
  • 20,443
  • 6
  • 51
  • 84
  • Here Why we are calling onDraw two times? and As I have mentioned I can't redraw to make it visible. Please suggest me If I have misunderstood.Is it reDrawing to make the line visible and Invisible? – user2710860 Aug 29 '13 at 12:12
  • Every time you have to change a view, you have to invalidate and redraw it. You can redraw parts of the view or the whole view, but there is no way to change a view without redrawing it. In your case, since the view is not complex, you can invalidate the whole view without any significant performance cost. If your view had a lot of other views etc in it, then you could have invalidated only the parts that have changed. – Anup Cowkur Aug 29 '13 at 12:17
  • Yes Correct!! But This is not the actual case. I have given a simple eg to understand . Here once i will the drawline the line after that I will loose the points. So I can't redraw. Another way I can use two Custom View. But I want to access second Custom View canvas(i.e. ReDLine canvas) on First Custom View (Green Line). Is there any way to access , by creating objects or something else? – user2710860 Aug 29 '13 at 12:23
  • I would suggest you store the points in some object and re-use them when you need to redraw again. That would be the simplest and cleanest approach. – Anup Cowkur Aug 29 '13 at 12:25
  • Then I have total 4616 bytes of data to redraw. :-(. and it not about redraw. it is about complexity. – user2710860 Aug 29 '13 at 12:29