Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ondraw for a custom view is looping infinitely android

Tags:

android

ondraw

OnDraw function for my custom View is being called infinitely and is looping !! What could be possible reason??

Here is my custom view:-

public class Balls extends View{

    private static final String TAG = "BallsView";

    private int mMode = READY;
    public static final int PAUSE = 0;
    public static final int READY = 1;
    public static final int RUNNING = 2;
    public static final int LOSE = 3;

    private final Paint mPaint = new Paint();
    private double mUx = 0.1;
    private double  mUy = 2;
    private double mVy;
    private double mVx;
    private double mSx;
    private double mSy;
    private double mRange;
    private float mX1;
    private float mY1;
    private int mX2;
    private int mY2;
    private int mDx;
    private int mDy;
    Time t;
    float mAngle;
    private final double mGravity = -9.8;

    private long mLastTime;

    private double mT;

    private Canvas mCanvas = null;



    public Balls(Context context, AttributeSet attrs, int defStyle){
        super(context, attrs, defStyle);
        setFocusable(true);
        setWillNotDraw(false);
        mPaint.setStyle(Paint.Style.FILL_AND_STROKE); 
        mPaint.setStrokeWidth(10); 
        mPaint.setAntiAlias(true); 
        mPaint.setStrokeCap(Cap.ROUND); 
        //mPaint.setColor(0xff00ffff);
        mPaint.setARGB(255, 0, 255, 0);

        mLastTime = System.currentTimeMillis();


    }
    public Balls(Context context, AttributeSet attrs) {
        super(context, attrs);
         setFocusable(true);
         setWillNotDraw(false);
          mPaint.setStyle(Paint.Style.FILL_AND_STROKE); 
          mPaint.setStrokeWidth(10); 
          mPaint.setAntiAlias(true); 
          mPaint.setStrokeCap(Cap.ROUND); 
          mPaint.setColor(0xff00ffff);

    }

     @Override
    public void onDraw(Canvas canvas) {
         Log.w(this.getClass().getName(),"onDraw of Balls called");
      super.onDraw(canvas);
      mCanvas = canvas;

      if(mCanvas!= null)
          Log.w(this.getClass().getName(),"Canvas is not null");

    }

This view is inflated as follows in another activity:-

mBalls = (Balls) findViewById(R.id.balls);

This view is placed in xml file inside a relative view and the relative view is the child of horizontal scroll view.

like image 774
Ruchira Avatar asked Oct 12 '11 03:10

Ruchira


3 Answers

There isn't an infinite loop here. What is going on is the OS is redrawing your activity as fast as possible. When your activity gets redrawn it redraws all of its children views. As your code has very little computation, from what I can see here, it is running very fast and is probably redrawing at a rate >30 FPS. Your log message makes it appear as if there is an infinite loop when there isn't. In fact there isn't even a loop inside your onDraw method.

To illustrate what is going on try this. Add a protected member drawCount to your Balls class and set it to 0:

protect int drawCount = 0;

Then append drawCount to the end of your onDraw log message

 public void onDraw(Canvas canvas){
       drawCount++;
       Log.w(this.getClass().getName(),"onDraw of Balls called. Total draws:" + Integer.toString(drawCount));
  ...
  }

What you should see is each log message will display a different drawCount.

If you want to get fancy and calculate the framerate of your app you could measure the time since the first draw and then divide the drawCount by how much time has passed which would give you a estimate of your activities framerate.

like image 145
slayton Avatar answered Nov 15 '22 05:11

slayton


onDraw(Canvas) called too often like in an infinite loop, is not normal. normally it should be called 1 to 3 times, if there is no following invalidate or layout changes. reasons for infinite loop maybe:
1, you called invalidate or postInvalidate some where.
2, parent or sibling layout is changing all the time.
3, View.LAYER_TYPE_SOFTWARE is used, es. setLayerType(View.LAYER_TYPE_SOFTWARE, null).
it is notable that LAYER_TYPE_SOFTWARE will cause onDraw() to be called, like in a loop.

like image 45
zdave Avatar answered Nov 15 '22 03:11

zdave


onDraw() get's call at invalidate. Invalidate() get's called when the view or it's parent feel the need to change and have to change it's state.

like image 40
weakwire Avatar answered Nov 15 '22 05:11

weakwire