我正在开发一个android工作室项目,试图让图像每半小时更换一次, 我跟着一个例子,但没有运气;它没有抛出任何错误,它只是不起作用,这就是我所拥有的 我尝试了什么:
public class MainActivity extends AppCompatActivity {
long startTime = 0;
Handler feed = new Handler();
Runnable runTimer = new Runnable()
{
@Override
public void run() {
Calendar now = Calendar.getInstance();
TextView output = (TextView) findViewById(R.id.output);
ImageView roto = (ImageView) findViewById(R.id.earthView);
Globals g = (Globals) getApplication();
int vision = 0;
int visionb = 0;
int hour = now.get(Calendar.HOUR);
int minute = now.get(Calendar.MINUTE);
String[] epos = g.getData56();
if (minute >= 30) {
visionb = 1;
}
if (minute < 30) {
visionb = 0;
}
long millis = System.currentTimeMillis() - startTime;
int seconds = (int) (millis / 1000);
int minutes = seconds / 60;
seconds = seconds % 60;
output.setText(String.format("%d:%02d", minutes, seconds));
vision = ((hour * 2) + visionb);
int resID = getResources().getIdentifier(epos[vision], "drawable", getPackageName());
if (roto != null) {
roto.setImageResource(resID);
feed.postDelayed(this, 500); // tried moving this around and changing the value from 0 - 3000
}
startTime = System.currentTimeMillis();
feed.postDelayed(runTimer, 500); // tried moving this around and changing the value from 0 - 3000
}
};
为了测试我的代码,我把它放在onUserInteraction方法中,它完全正常:
@Override
public void onUserInteraction() {
super.onUserInteraction();
final Globals g = (Globals) getApplication();
Calendar now = Calendar.getInstance();
ImageView roto = (ImageView) findViewById(R.id.earthView);
int vision = 0;
int visionb = 0;
int hour = now.get(Calendar.HOUR);
int minute = now.get(Calendar.MINUTE);
String[] epos = g.getData56();
if (minute >= 30) {
visionb = 1;
}
if (minute < 30) {
visionb = 0;
}
vision = ((hour * 2) + visionb);
int resID = getResources().getIdentifier(epos[vision], "drawable", getPackageName());
if (roto != null) {
roto.setImageResource(resID);
}
}
所以我认为我做错了什么,任何人都知道我做错了什么或错过了什么?
感谢帮助Landice这是工作结果,给我一个时钟和每半小时 这一天改变了我的形象(共有48张图片),再次感谢您的帮助!
long startTime = 0;
{
final Handler feed = new Handler();
final Runnable runTimer = new Runnable() {
@Override
public void run() {
Calendar now = Calendar.getInstance();
TextView output = (TextView) findViewById(R.id.output);
ImageView roto = (ImageView) findViewById(R.id.earthView);
Globals g = (Globals) getApplication();
int vision = 0;
int visionb = 0;
int second = now.get(Calendar.SECOND);
int hour = now.get(Calendar.HOUR);
int minute = now.get(Calendar.MINUTE);
String[] epos = g.getData56();
if (minute >= 30) {visionb = 1;}
if (minute < 30) {visionb = 0;}
output.setText(String.format(hour + ":" + minute + ":" + second));
feed.postDelayed(this, 0);
if (minute == 30 || minute == 0) {vision = ((hour * 2) + visionb);
int resID = getResources().getIdentifier(epos[vision], "drawable", getPackageName());
if (roto != null) {
roto.setImageResource(resID);
}
}
startTime = System.currentTimeMillis();
}
};
feed.postDelayed(runTimer,1000);
}
答案 0 :(得分:2)
我想你忘了安排第一篇文章。 这是使用Handler和Runnable来安排定期事件的示例。 该行计划第一次运行。
handler.post(runTimer);
run()内的这一行计划运行时的后续事件:
handler.postDelayed(this, 1000);
要注意1000意味着一秒钟。因此30分钟将是60 * 1000 * 30,在您的情况下为1800000。
示例:
public class MainActivity extends AppCompatActivity {
TextView mTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTextView = (TextView) findViewById(R.id.textview);
final Handler handler = new Handler();
Runnable runTimer = new Runnable()
{
@Override
public void run() {
mTextView.setText("Current Time: " + (new Date()).toString());
handler.postDelayed(this, 1000);
}
};
handler.post(runTimer);
}
}
此外,您可能希望通过以下方式删除计划的延迟事件来停止循环:
handler.removeCallbacks(runTimer);
答案 1 :(得分:1)
您可以尝试以下答案:How to change images in imageview every n number of seconds。
int[] imageArray = { R.drawable.image1, R.drawable.image2, R.drawable.image3, R.drawable.image4, R.drawable.image5 };
int position=0;
Timer mTimer = new Timer();