I have written following simple code to see the behavior of rendering. In this code i’m changing the color of screen.
The thing that I can’t understand is, why this code is running for ever? I didn’t use unlimited loop but this code will run for ever!
Please tell me the reason. Thanks
package kamalan.com.androidbasicstarter;
import java.util.Random;
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
public class RenderViewTest extends Activity{
class RenderView extends View{
Random rand = new Random();
public RenderView(Context context) {
super(context);
}
protected void onDraw(Canvas canvas){
canvas.drawRGB(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256));
invalidate();
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(new RenderView(this));
}
}
invalidatecauses a redraw. It’s usually (always?) a bad idea to call that fromonDraw, because it will causeonDrawto be called again immediately. Which is what you are seeing.