L1ghtk3ira
L1ghtk3ira

Reputation: 3199

Android canvas create screen flash for few seconds

I am trying to accomplish having the android canvas flash from blue to red. I would like to control the flashing to be adjustable. For example change colors every 100 milliseconds for 3 seconds.

I have tried using Timer and Threads how ever the application seems to crash. I tried with and without invalidate(). I am assuming my logic is off or that i do not fully understand how its executing. Possibly it happens to fast I cant see and crashes or creates too many threads that crash the app.

Pseudo code

// canvas change color to blue
// wait 100 milliseconds
// canvas change color to red
// wait 100 milliseconds
// canvas change color to blue
// execute for 3 seconds

I would put code but since I tried multiple solutions each having the same crash or hanging effect I am assuming they won't be that helpful.

I know I can either create a rectangle shape drawable to fill the screen or just color the whole canvas which is probably the better approach.

I have two paint objects created and if I fill the canvas regularly they both work of course.

Paint paint2 = new Paint();      
paint2.setColor(Color.RED); 
paint2.setStyle(Style.FILL); 
canvas.drawPaint(paint2); 

Paint paint1 = new Paint();      
paint1.setColor(Color.BLUE); 
paint1.setStyle(Style.FILL); 
canvas.drawPaint(paint1); 

Just to sum up: I would like the canvas to flash between blue and red every 100 milliseconds for 3 seconds. But would also like both the delay (100 milliseconds) and the duration (3 seconds) to be adjustable so I can increase or decrease them.

I have tried using:

Upvotes: 1

Views: 137

Answers (0)

Related Questions