I am trying to accomplish fireworks functionality based on the github code from Lenoids. I want to create two fireworks (one white and another red) and have them display continuously after every two seconds.
Here's what I did so far:
Created two buttons (one for each color of fireworks). The buttons have the fireworks functionality.
Call the buttons programatically in a thread to display the fireworks.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_congratulations);
new Thread(new Runnable() {
#Override
public void run() {
while(true){
try {
Thread.sleep(100);
button10.callOnClick(); //for red fireworks
button11.callOnClick(); //for white fireworks
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
}
The problem is that if I increase the value of sleep method, then the fireworks do not show up at all. How can I accomplish the two second continuous fireworks functionality?
Here's the fireworks code:
new ParticleSystem(MyClass.this, 100, R.drawable.star_pink, 800) .setSpeedRange(0.1f, 0.25f) .oneShot(view, 70);
If you want to run the firework every two seconds then use Handler instead of the thread and thread.sleep.
final Handler ha=new Handler();
ha.postDelayed(new Runnable() {
#Override
public void run() {
button10.callOnClick();
button11.callOnClick();
ha.postDelayed(this, 2000);
}
}, 2000);
Hope that helps.
Related
When I'm clicking on a button, I want to change an Image view to different picture, to wait 3 seconds, and to change it again to another picture (without clicking again).
1 click -> change picture -> wait 3 seconds -> change picture.
This is my code:
northLight.setImageResource(R.drawable.red_and_yellow);
try {
TimeUnit.MILLISECONDS.sleep(3000);
} catch (InterruptedException e) {}
northLight.setImageResource(R.drawable.green);
While I'm running the program, when I'm actually clicking the button, the program ignores the first setImage and changes it straight to the second setImage (to the green).
How can I solve this?
you can use handler for it,
northLight.setImageResource(R.drawable.red_and_yellow);
new Handler().postDelayed(new Runnable() {
public void run() {
// Actions to do after 3 seconds
northLight.setImageResource(R.drawable.green);
}
}, 3000);
You can try using a Handler to wait and change the image. When your button gets clicked, change your image and run the handler with a delay of 3 seconds.
//Call this method when your button is clicked
public void changeImage() {
northLight.setImageResource(R.drawable.red_and_yellow);
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
northLight.setImageResource(R.drawable.green);
}
}, 3000);
}
Try this code
new CountDownTimer(3000,1000){
#Override
public void onTick(long l) {
northLight.setImageResource(R.drawable.red_and_yellow);
}
#Override
public void onFinish() {
northLight.setImageResource(R.drawable.green);
}
}.start();
I think my problem is very basic but i am new to android so just need this solution.
I have 20 buttons and all of them are animating(translation and scaling) on the screen. I want to create a new thread and do animations there. Those buttons have images on them,code is working fine in UI thread but sometimes i am getting ANR message and exception that application may be doing too much work on its main thread.
Please help
Thank you
This is my onCreate in which i want to do animations inside thread:-
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
playerName=getSharedPreferences(WelcomeActivity.Player_Name,0);
gameMode= getSharedPreferences("game_mode", 0);
setContentView(R.layout.activity_dragonland);
shendron=(ImageView)findViewById(R.id.shendron);
glaedr=(ImageView)findViewById(R.id.glaedr);
saphira=(ImageView)findViewById(R.id.saphira);
brownlay=(ImageView)findViewById(R.id.brownlay);
chrysophylax=(ImageView)findViewById(R.id.chrysophylax);
tempest=(ImageView)findViewById(R.id.tempest);
thrisorn=(ImageView)findViewById(R.id.thrisorn);
ruth=(ImageView)findViewById(R.id.ruth);
grifoka=(ImageView)findViewById(R.id.grifoka);
horrid=(ImageView)findViewById(R.id.horrid);
brownmean=(ImageView)findViewById(R.id.brownmean);
firnen=(ImageView)findViewById(R.id.firnen);
rhaegal=(ImageView)findViewById(R.id.rhaegal);
mnementh=(ImageView)findViewById(R.id.mnementh);
gorep=(ImageView)findViewById(R.id.gorep);
rubela=(ImageView)findViewById(R.id.rubela);
hotrika=(ImageView)findViewById(R.id.hotrika);
drako=(ImageView)findViewById(R.id.drako);
cadui=(ImageView)findViewById(R.id.cadui);
balerion=(ImageView)findViewById(R.id.balerion);
dragon_zoom = AnimationUtils.loadAnimation(getApplicationContext(),R.anim.dragon_zoom);
dragon_zoom.setStartOffset(1500);
down_right = AnimationUtils.loadAnimation(getApplicationContext(),R.anim.down_right);
up_right = AnimationUtils.loadAnimation(getApplicationContext(),R.anim.up_right);
seq_down= AnimationUtils.loadAnimation(getApplicationContext(),R.anim.sequential_down);
seq_up= AnimationUtils.loadAnimation(getApplicationContext(),R.anim.sequential_up);
seq_right= AnimationUtils.loadAnimation(getApplicationContext(),R.anim.sequntial_right);
seq_left= AnimationUtils.loadAnimation(getApplicationContext(),R.anim.sequential_left);
shendron.startAnimation(dragon_zoom);
glaedr.startAnimation(seq_down);
saphira.startAnimation(down_right);
brownlay.startAnimation(seq_up);
chrysophylax.startAnimation(seq_right);
tempest.startAnimation(up_right);
thrisorn.startAnimation(dragon_zoom);
ruth.startAnimation(down_right);
grifoka.startAnimation(down_right);
horrid.startAnimation(dragon_zoom);
brownmean.startAnimation(dragon_zoom);
firnen.startAnimation(dragon_zoom);
rhaegal.startAnimation(seq_right);
mnementh.startAnimation(up_right);
gorep.startAnimation(seq_left);
rubela.startAnimation(seq_left);
hotrika.startAnimation(seq_left);
drako.startAnimation(seq_left);
cadui.startAnimation(dragon_zoom);
balerion.startAnimation(up_right);
}
you can use something like this:
Thread thread = new Thread()
{
#Override
public void run() {
try {
Animation ranim = (Animation) AnimationUtils.loadAnimation(getBaseContext(),
R.anim.rotation);
buttonRotate.setAnimation(ranim);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
thread.start();
After pushing a button, i want to close an activity. But, I’d like to wait some seconds before closing it, because users have to read a short message displayed on that activity.
I tried using Thread inside the onClick event
try{
Thread.sleep(2000);
finish();
}
catch(Exception e){}
But, when I push the button, the entire objects are freeze (for example, the button stay pushed).
Then I used a simple Timer
timer.schedule(task(), 2000);
And it seems to work well. Is it correct to use a Timer in this situation, or should I use a Thread or something else?
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
Activity.this.finish();
}
}, 2000) ;
The easiest way is probably to use a Handler
private Handler h = new Handler();
...
h.postDelayed(new Runnable() {
#Override
public void run() {
finish();
}
}, 2000);
i'm making a 15 puzzle (http://en.wikipedia.org/wiki/Fifteen_puzzle) game, and i have an activity for user to select the background image, and then i pass that image to a new activity in order to scale and crop it.
Now i want to show the solution to the user first for 3 secs then it would shuffle, i'm using code like this:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//...skip the code that gets the image and scales it
start();
}
then in my start() funcion:
public void start() {
//the createPuzzle function would create all the Views(tiles)
//and add them to the root LinearLayout using addView() function
createPuzzle(game.getBoardConfig(), dimension);
//i was trying to sleep here
shuffle();
}
i used:
try {
Thread.sleep(3000);
} catch (InterruptedException e) {}
or:
SystemClock.sleep(3000);
but neither of them worked properly, they all paused the the thread right after i selected the image and when it was paused i can't see the new activity and the tiles i created. When the thread resumes, it was already showing the shuffled puzzle.
I've been looking at the documentation for quite a while but still can't figure out what's wrong with my code, thanks for helping!
Don't make the UI thread sleep because that will lock up the entire UI thread which is a no-no. Instead, use a Handler to post a runnable with a delay...like
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
shuffle();
}
}, 3000);
I have created a custom control panel for a video player. Now I want to give a effect like default MediaController where the panel becomes visible when the screen is touched and it becomes invisible again after the last touch time. I can use this type of code for that.
Thread thread = new Thread() {
#Override
public void run() {
try {
Thread.sleep(60000);
} catch (InterruptedException e) {
}
runOnUiThread(new Runnable() {
#Override
public void run() {
// make the panel invisible
}
});
}
};
I can start the thread when the screen is touched and make it invisible after 60 seconds. But in my case, if the user touches the screen again in between this 60 seconds, the panel should vanish after 60 seconds from the last touch. How to consider this case also?
I would recommend using a combination of Runnables and a Handler. You can do Handler calls using postDelayed() to do something after, say, 60 seconds.
Here's an example:
private Handler mHandler = new Handler();
mHandler.post(showControls); // Call this to show the controls
private Runnable showControls = new Runnable() {
public void run() {
// Code to show controls
mHandler.removeCallbacks(showControls);
mHandler.postDelayed(hideControls, 60000);
}
};
private Runnable hideControls = new Runnable() {
public void run() {
// Code to hide the controls
}
};
Simply delete/cancel current timer.
Btw, you should not do it by Thread, but by posting message to a Handler. Such future timer task doesn't need another thread.