[英]setImageRessource doesn't work in a timer android studio
I'm a beginner... Why did my "setImageRessource" doesn't work?我是初学者……为什么我的“setImageRessource”不起作用? The image "fiole" have to change at 10% and at 20% of the duration of the timer but on my phone, the image never change...图像“fiole”必须在计时器持续时间的 10% 和 20% 处更改,但在我的手机上,图像永远不会改变......
However, the text of the textViex is changed when the time equals the quarter of the duration.但是,当时间等于持续时间的四分之一时,textViex 的文本会发生变化。
public class XActivity extends AppCompatActivity {
int duree;
String duration;
int durationInt;
protected TextView tempsRestant;
protected ImageView fiole;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_X);
this.duree = 0;
this.duration = getIntent().getStringExtra("DURATION");
this.durationInt = Integer.parseInt(duration)*60;
this.tempsRestant=findViewById(R.id.tempsRestant);
this.fiole=findViewById(R.id.fiole);
final Timer modecTimer = new Timer();
modecTimer.schedule(new TimerTask() {
@Override
public void run() {
duree += 1;
if (duree==(durationInt/100)*10){
fiole.setImageResource(R.drawable.fiole10p);
}
if (duree==(durationInt/100)*20){
fiole.setImageResource(R.drawable.fiole20p);
}
if (duree==durationInt/4){
tempsRestant.setText("some text");
}
}
}
}, 1000, 1000);
}
Your setImageResource didn't work, because it needs to be run on UIThread您的 setImageResource 不起作用,因为它需要在 UIThread 上运行
modecTimer.schedule(new TimerTask() {
@Override
public void run() {
runOnUiThread(() -> {
duree += 1;
if (duree==(durationInt/100)*10){
fiole.setImageResource(R.drawable.fiole10p);
}
if (duree==(durationInt/100)*20){
fiole.setImageResource(R.drawable.fiole20p);
}
if (duree==durationInt/4){
tempsRestant.setText("some text");
}
});
}
}, 1000, 1000);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.