I have a candle and flame as separate image views. When user taps on the flame, its drawable resource is set to transparent meaning the flame has blown off.
I also want to show smoke as the flame is blown off. For that I have some 21 images of black smoke which can be shown in quick sequence so that users sees it as smoke. Here is my code which hangs when the flame is tapped (R.drawable.smoke_22 is the transparent image for flame and R.drawable.candle_1 is the yellow flame image, flag is to track if flame is currently blown off or not. flag=flase means that flame is still lighting):
int[] smokeImages = { R.drawable.smoke_1, R.drawable.smoke_2,
R.drawable.smoke_3, R.drawable.smoke_4, R.drawable.smoke_5,
R.drawable.smoke_6, R.drawable.smoke_7, R.drawable.smoke_8,
R.drawable.smoke_9, R.drawable.smoke_10, R.drawable.smoke_11,
R.drawable.smoke_12, R.drawable.smoke_13, R.drawable.smoke_14,
R.drawable.smoke_15, R.drawable.smoke_16, R.drawable.smoke_17,
R.drawable.smoke_18, R.drawable.smoke_19, R.drawable.smoke_20,
R.drawable.smoke_21 };
this.flamImageView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (flag) {
flamImageView.setImageDrawable(getResources().getDrawable(
R.drawable.smoke_22));
flag = false;
startTimer();
} else {
flamImageView.setImageDrawable(getResources().getDrawable(
R.drawable.candle_1));
flag = true;
smokemageView.setImageResource(R.drawable.smoke_22);
}
}
});
}
private void startTimer() {
for (int i = 0; i < 21; i++) {
try {
Thread.sleep(250);
smokemageView.setImageResource(smokeImages[i]);
} catch (InterruptedException localInterruptedException) {
}
}
smokemageView.setImageResource(R.drawable.smoke_22);
}
The flame does go off and comes back on repetitive touch but I don't see proper smoke animation
You could simply wrap your for statement in a while loop, set off by a boolean: just set the boolean to true whenever you want the drawable to animate.
private void startTimer() {
while (timing) {
for (int i = 0; i < 21; i++) {
try {
Thread.sleep(250);
smokemageView.setImageResource(smokeImages[i]);
} catch (InterruptedException localInterruptedException) {
}
}
}
smokemageView.setImageResource(R.drawable.smoke_22);
}
else you could create an AnimationDrawable
object: (probably the easier, more error-proof route)
AnimationDrawable anim = new AnimationDrawable();
anim.addFrame(
getResources().getDrawable(R.drawable.smoke_1),
250);
anim.addFrame(
getResources().getDrawable(R.drawable.smoke_2),
250);
anim.addFrame(
getResources().getDrawable(R.drawable.smoke_3),
250);
anim.addFrame(
getResources().getDrawable(R.drawable.smoke_4),
250);
//......So on, so forth until you have a satisfying animation sequence
//set ImageView to AnimatedDrawable
smokemageView.setImageDrawable(anim);
//if you want the animation to loop, set false
anim.setOneShot(false);
anim.start();
I sure hope this helps, Happy Coding!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With