我想使用for循环,字符串数组和可绘制文件夹中的png图像更改图像视图的图像。一个图像应该休息5秒,然后下一个应该出现。这里是我使用的代码,并且它不能正常工作。图像没有变化。它每个循环等待5秒,但图像不变。最后一个图像被加载。请别人帮我.. 。想要使用ImageView和For循环制作幻灯片
private String [] array1 = {"card2c","card2d","card2h","card2s",
"card3c","card3d","card3h","card3s",
"card4c","card4d","card4h","card4s",
"card5c","card5d","card5h","card5s",
"card6c","card6d","card6h","card6s",
"card7c","card7d","card7h","card7s",
"card8c","card8d","card8h","card8s",
"card9c","card9d","card9h","card9s",
"card10c","card10d","card10h","card10s",
"cardjc","cardjd","cardjh","cardjs",
"cardqc","cardqd","cardqh","cardqs",
"cardkc","cardkd","cardkh","cardks",
"cardac","cardad","cardah","cardas",};
for(int j=0;j<52;j++)
{
int resID_temp1 = getResources().getIdentifier(array1[j] , "drawable", getPackageName());
Drawable image_temp1 = getResources().getDrawable(resID_temp1);
Player1.setImageDrawable(image_temp1); // Player1 is the ImageView
try {
Thread.sleep(5000);
}
catch(InterruptedException ex)
{
Thread.currentThread().interrupt();
}
}
试过定时器
(for(int j=0;j<52;j++)
{
timer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
//switch image here
int resID_temp1 = getResources().getIdentifier(array1[j] , "drawable", getPackageName());
Drawable image_temp1 = getResources().getDrawable(resID_temp1);
Player1.setImageDrawable(image_temp1);
}
}, 0, 5000);
对(INT J = 0;Ĵ<52; J ++){ timer.scheduleAtFixedRate(新的TimerTask(){ \t @覆盖 \t公共无效的run(){ \t //切换图像此处 \t INT resID_temp1 = getResources()则getIdentifier(数组1 [j]时, “可拉伸”,getPackageName()); \t绘制对象image_temp1 = getResources()getDrawable(resID_temp1); \t Player1.setImageDrawable(image_temp1); \t} } ,0,5000); – SNT93
我试过计时器,我已经更新了我的问题。但它不允许我在计时器中使用非最终变量.. – SNT93
我会尽快在我回家的时候发表评论... – JohnyTex