该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
各位大神,我想实现一个布局里面的3张图片自动切换浏览的功能,每次只显示1张,就是第一次显示第一张图片,隔10秒,显示第二张图片,再隔10秒,显示第三张,再10秒,又是第一张,这样。
package com.example.sports;
import java.util.Timer;
import java.util.TimerTask;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.ViewSwitcher.ViewFactory;
public class MainActivity extends Activity implements ViewFactory {
public Timer Timer;
public static ImageSwitcher advertisement;
public static int[] image={R.drawable.image1,R.drawable.image2,R.drawable.image3};
public static int index=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
advertisement=(ImageSwitcher) findViewById(R.id.advertisement);
advertisement.setFactory(this);
advertisement.setImageResource(image[0]);
Timer timer = new Timer();
timer.schedule(new TimerTaskTest(), 5000);
}
@Override
public View makeView() {
return new ImageView(this);
}
}
package com.example.sports;
import java.util.TimerTask;
public class TimerTaskTest extends TimerTask {
@Override
public void run() {
MainActivity.index++;
MainActivity.advertisement.setImageResource(MainActivity.image[MainActivity.index]);
}
}
这样只能显示第一张,之后就报错,timer好像有点问题。