Amira Elsayed Ismail
Amira Elsayed Ismail

Reputation: 9394

Disable Calling of onDraw after finish drawing android

I want to make the following :

  1. get canvas

  2. draw list of rectangles and bitmaps on the canvas based on for loop

  3. after drawing all items don't call onDraw until I rotate the device, because the application is too slow when I try to navigate through it

  4. My Canvas is in HorizontalScrollView

and When I try to scroll it is very slow

EDIT : I'm extending View class, so can I save the view and don't call onDraw unless I want it invalidate ??

EDIT 2 : This is my onDraw Method

@Override
    protected void onDraw(Canvas canvas) {


        super.onDraw(canvas);

        for (int i = 0 ; i < mIcons.size() ; i++) {

            prepareItem(canvas, paint, mIcons.get(i));
        }

    }

please any one can help ??

Upvotes: 2

Views: 3672

Answers (3)

Sunny Jha
Sunny Jha

Reputation: 121

For me it worked by calling return; after an if condition. Like:

if(lengthX == lengthTotal) return;

Upvotes: 0

Amira Elsayed Ismail
Amira Elsayed Ismail

Reputation: 9394

I have found a workaround to make this work, I have draw in bitmap and every time I try to call onDraw() I check if my bitmap is not null use it

Thanks All

Upvotes: 0

Martin Vandzura
Martin Vandzura

Reputation: 3127

You can't prevent onDraw from being called but you can override it and use flag which will tell whether it should execute or not. I don't see your code but I suggest you something like:

boolean callOnDraw = true;

   @Override
    protected void onDraw(Canvas canvas) {

        super.onDraw(canvas);

      if(callOnDraw){
        for (int i = 0 ; i < mIcons.size() ; i++) {

            prepareItem(canvas, paint, mIcons.get(i));
        }
        callOnDraw = false;
      }

    }

And when you want stop drawing you set flag callOnDraw = false;

Upvotes: 1

Related Questions