开发者

Trying to flash the color of the background of one of my android apps

开发者 https://www.devze.com 2023-03-12 10:25 出处:网络
I\'m trying to have my app go between 2 background colors and then return back to the original background color this is the code that i tried thinking it would change the background then pause for a q

I'm trying to have my app go between 2 background colors and then return back to the original background color this is the code that i tried thinking it would change the background then pause for a quarter of a second and change the color again. Is there someway to do this. I am fairly new to programming and I am still learning this is what I thought would work but I'm open to any suggestions.

Bg.setBackgroundColor(getResources().getColor(R.color.C1));
try{Thread.sleep(250);}
catch(InterruptedException e){}
Bg.setBackgroundColor(getResources().getColor(R.color.C2));
try{Thread.sleep(250);}
catch(InterruptedException e){}
Bg.setBackgroundColor(getResources().getColor(R.color.C1));
try{Thread.sleep(250);}
catch(InterruptedException e){}
Bg.setBackgroundColor(getResources()开发者_Go百科.getColor(R.color.C2));
try{Thread.sleep(250);}
catch(InterruptedException e){}
Bg.setBackgroundColor(getResources().getColor(BG[C-1]));


If you have to use this approach, then I would suggest doing this in a different thread than the UI thread, because I assume this will make the UI thread sleep for 250 millis, and it will thus be unresponsive. I don't even if this is even allowed and it is possible that the UI thread will throw an exception.

That said, if you are not going to do anything in the catch clause of IntereuptedException than why not use SystemClock.sleep.

Finally, I suggest forgetting this approach and use animations instead, check Animation Resources


Set a solid colored background (whatever you want, drawable for example), and use an animation to effect it. You can blink it on and off with whatever parameters you wish including speed, fade, repeat count, and so on ... All declaratively defined in a res/anim .xml file.

A tutorial on Android animations is beyond the scope of what I want to write here, but just Google for it. There are examples of almost anything you want.

You can find the Android API demos related to animation here, http://developer.android.com/resources/samples/ApiDemos/res/anim/index.html

0

精彩评论

暂无评论...
验证码 换一张
取 消