问题描述
我有一个蜡烛火焰作为单独的图像意见。当用户点击了火焰,它的绘制资源设置为透明的意义火焰烧断了。
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.
我也想展示烟雾火焰被吹掉。对于我有黑烟约21图像可在快速连续显示,使用户将其视为烟雾。这里是我的code,当火焰被窃听它挂(R.drawable.smoke_22是火焰的透明图像和R.drawable.candle_1是黄色的火焰图像,标志是跟踪如果火焰正在吹走或不。标志= FLASE意味着火焰仍在照明):
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
推荐答案
您可以简单地换你在一个while循环语句,由布尔掀起:
每当你想要的绘制动画刚才设置的布尔为true。
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);
}
其他你可以创建一个 AnimationDrawable
对象:(可能是更容易,更防错路线)
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!
这篇关于显示ImageView的图像连续序列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!