#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2004
    Location
    Denmark, Copenhagen
    Posts
    122
    Rep Power
    11

    Calling setBackgroundDrawable() in a view class makes a infinite call loop to draw.


    Hi Guys.

    I'm relativly new the Android development, all though quite experienced with Java. I'm trying to make a background gradient, and so far succesfully using the following code in my onDraw method in my view class:

    Code:
    protected void onDraw(Canvas canvas)
    	{
    		
    		Log.v("HelloWorld", "Drawing gradiant");
    		int colors[] = new int[4];
    		colors[0] = Color.GREEN;
    		colors[1] = Color.rgb(128, 255, 128);
    		colors[2] = Color.rgb(200, 255, 200);
    		colors[3] = Color.WHITE;
    		GradientDrawable gradiantBackColor = new GradientDrawable(Orientation.TL_BR, colors);
    	
    		setBackgroundDrawable(gradiantBackColor);
    		setBackgroundColor(Color.GREEN);
    	}
    This makes a nice gradient green color from top to bottom. But i usually look through my LogCat to see if everything is called as it should, and i noticed that the onDraw method is being called ALOT, and it seems that the program is stuck in a refresh GFX loop. This also happens when i use the setBackgroundColor(color).

    If I use the canvas.drawPaint() method, to make my background just green, the onDraw method is only called once. What gives?

    I have tried to find an explanation to this loop behavior but so far come up short. Any have an explanation to this. I'm sort of certain i'm doing something wrong.

    Btw, I'm debugging on an actual device: HTC Desire.

    Here is a short copy paste of my log cat:
    01-27 11:59:49.666: VERBOSE/HelloWorld(12398): Drawing gradiant
    01-27 11:59:49.686: VERBOSE/HelloWorld(12398): Drawing gradiant
    01-27 11:59:49.706: VERBOSE/HelloWorld(12398): Drawing gradiant
    01-27 11:59:49.716: VERBOSE/HelloWorld(12398): Drawing gradiant
    01-27 11:59:49.736: VERBOSE/HelloWorld(12398): Drawing gradiant
    01-27 11:59:49.756: VERBOSE/HelloWorld(12398): Drawing gradiant
    01-27 11:59:49.766: VERBOSE/HelloWorld(12398): Drawing gradiant
    01-27 11:59:49.786: VERBOSE/HelloWorld(12398): Drawing gradiant
    01-27 11:59:49.796: VERBOSE/HelloWorld(12398): Drawing gradiant
    01-27 11:59:49.816: VERBOSE/HelloWorld(12398): Drawing gradiant
    01-27 11:59:49.826: VERBOSE/HelloWorld(12398): Drawing gradiant
    01-27 11:59:49.846: VERBOSE/HelloWorld(12398): Drawing gradiant
    01-27 11:59:49.866: VERBOSE/HelloWorld(12398): Drawing gradiant
    01-27 11:59:49.876: VERBOSE/HelloWorld(12398): Drawing gradiant
    01-27 11:59:49.896: VERBOSE/HelloWorld(12398): Drawing gradiant
    01-27 11:59:49.916: VERBOSE/HelloWorld(12398): Drawing gradiant
    01-27 11:59:49.926: VERBOSE/HelloWorld(12398): Drawing gradiant
    01-27 11:59:49.946: VERBOSE/HelloWorld(12398): Drawing gradiant
    01-27 11:59:49.966: VERBOSE/HelloWorld(12398): Drawing gradiant
    01-27 11:59:49.976: VERBOSE/HelloWorld(12398): Drawing gradiant
    01-27 11:59:49.996: VERBOSE/HelloWorld(12398): Drawing gradiant
    01-27 11:59:50.016: VERBOSE/HelloWorld(12398): Drawing gradiant
    01-27 11:59:50.026: VERBOSE/HelloWorld(12398): Drawing gradiant
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2004
    Location
    Denmark, Copenhagen
    Posts
    122
    Rep Power
    11
    I found out what went wrong.

    setBackgroundDrawable sets the UI state to invalid. This forces the activity to redraw, which means to called the ondraw method, that then calls setBackgroundDrawable, which then again sets the UI to invalid.

    setting the setBackgroundDrawable the activity's on create fixed this problem.

IMN logo majestic logo threadwatch logo seochat tools logo