【问题标题】:How to show image for 30 seconds then show another one after 30 seconds for multiple images?如何显示图像 30 秒,然后在 30 秒后显示另一张图像以显示多张图像?
【发布时间】:2020-05-22 18:36:48
【问题描述】:

我只能在 2 张图片之间进行更改,但我想更改 2 张以上的图片。我正在创建一个健身应用程序,其中每个练习时长 30 秒,30 秒后,下一个练习开始。每次锻炼都有大约 5 个练习,但我只能在 2 个图像之间切换。目前,在计时器达到 30 秒后,它将切换到下一张图像,但计时器停在那里。我听说我需要使用数组和循环,但我不知道如何......有人可以帮助我吗?非常感谢您的帮助,因为我的提交将在 5 天内提交 >

private void startTimer(){
    countDownTimer = new CountDownTimer(30000, 1000) {
        @Override
        public void onTick(long millisUntilFinished) {
            mTimeLeftInMillis = millisUntilFinished;
            updateCountDowntText();
        }

        @Override
        public void onFinish() {
            exercise_image.setImageResource(R.drawable.lunges);
            exercise_name.setText("Alternate Lunges");
        }
    }.start();
    mTimerRunning = true;
}

private void updateCountDowntText(){
    int seconds = (int) (mTimeLeftInMillis / 1000) % 30;

    String timeLeft = String.format(Locale.getDefault(), "%02d", seconds);
    timerValue.setText(timeLeft);
}

【问题讨论】:

  • 做一个练习课。然后创建所有练习的列表并遍历它。
  • erm 我不太确定怎么做,这就是我问的原因>
  • 创建列表并遍历它。目前 30 秒后它会变为另一张图片,但计时器停在那里它不会从 30 秒开始倒计时
  • 如果你有一个练习类,我假设它有实例变量练习名称和图像?所以在一个 for 循环中。只需为锻炼列表中的每个锻炼设置锻炼名称和图像并倒计时。

标签: java android-studio


【解决方案1】:

如果您的图像在 R.drawables 中。然后只需将 int 引用存储在您的数组中,然后加载如下所示,

  private int[] yourarray = {
R.drawable.star_00, //star_00 is image name here
R.drawable.star_01,
R.drawable.star_02,

};

然后,当您想要加载图像并绘制到图像视图时,您可以从 Activity 中执行类似的操作。

Drawable d = getResources().getDrawable(yourarray[n]);

然后在 ImageView 上调用 setImageDrawable。所以:

yourImageView.setImageDrawable(d);

【讨论】: