我现在正在实现视图寻呼机,我想在特定的时间段内不断地自动更改图像,可能是每 5 毫秒一次.
I am now implementing the view pager and i want to continuously change the image one by one automatically for a particular periods of time may be once in 5 ms .
我还必须手动允许用户滑动图像(它工作正常)
I also have to manually allow the user to swipe the images (it is working properly)
但图像不会自动改变,当它到达最后一张图像时,它应该自动到达第一张图像
But the image is not automatically changing and when it reaches the last image it should come to the first image automatically
final Handler handler = new Handler();
final Runnable Update = new Runnable() {
public void run() {
if (currentPage == strImages.length-1) {
currentPage = 0;
}
intro_images.setCurrentItem(currentPage++, true);
}
};
timer = new Timer(); // This will create a new Thread
timer .schedule(new TimerTask() { // task to be scheduled
@Override
public void run() {
handler.post(Update);
}
}, 500, 3000);
我使用了上面的代码,但它不能正常工作
I used above code but it does not work properly
试试这个添加 addOnPageChangeListener
到你的 viewPager
像这样
try this add addOnPageChangeListener
to your viewPager
like this
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
if (position == strImages.length-1) {
currentPage = 0;
}
intro_images.setCurrentItem(currentPage++, true);
}
@Override
public void onPageSelected(int position) {
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
或者对于自动更改页面,试试这个创建一个像这样的时间任务
Timer timer;
timer = new Timer();
timer.scheduleAtFixedRate(new RemindTask(), 0, 3000); // delay*/
private class RemindTask extends TimerTask {
int current = viewPager.getCurrentItem();
@Override
public void run() {
runOnUiThread(new Runnable() {
public void run() {
if (current < strImages.size()) {
viewPager.setCurrentItem(current);
current += 1;
} else {
current = 0;
viewPager.setCurrentItem(current);
}
}
});
}
}
这篇关于如何在视图寻呼机中自动更改图像,当它到达最后一张图像时,它应该自动到达第一张图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!