这是我制作ViewPager
的代码。我想在更改页面之间制作动画。所以我写这段代码:
ViewPagerAdapter adapter;
adapter = new ViewPagerAdapter(FistActiivty.this,imges);
vp.setAdapter(adapter);
pageSwitcher(2);
try {
Field mScroller;
mScroller = ViewPager.class.getDeclaredField("mScroller");
mScroller.setAccessible(true);
Interpolator sInterpolator = new AccelerateInterpolator();
FixedSpeedScroller scroller = new FixedSpeedScroller(vp.getContext(), sInterpolator);
// scroller.setFixedDuration(5000);
mScroller.set(vp, scroller);
} catch (Exception e) {
Log.v("this",e.getMessage());
}
public class FixedSpeedScroller extends Scroller {
private int mDuration = 2000;
public FixedSpeedScroller(Context context) {
super(context);
}
public FixedSpeedScroller(Context context, Interpolator interpolator) {
super(context, interpolator);
}
public FixedSpeedScroller(Context context, Interpolator interpolator, boolean flywheel) {
super(context, interpolator, flywheel);
}
@Override
public void startScroll(int startX, int startY, int dx, int dy, int duration) {
super.startScroll(startX, startY, dx, dy, mDuration);
}
@Override
public void startScroll(int startX, int startY, int dx, int dy) {
// Ignore received duration, use fixed one instead
super.startScroll(startX, startY, dx, dy, mDuration);
}
}
public void pageSwitcher(int seconds) {
timer = new Timer(); // At this line a new Thread will be created
timer.scheduleAtFixedRate(new RemindTask(), 0, seconds * 1000);
}
class RemindTask extends TimerTask {
@Override
public void run() {
runOnUiThread(new Runnable() {
public void run() {
Log.v("this","change pager " +page);
if (page > imges.length) {
vp.setCurrentItem(0);
page=1;
} else {
vp.setCurrentItem(page++);
}
}
});
}
}
class ViewPagerAdapter extends PagerAdapter {
Context context;
String[] img;
LayoutInflater inflater;
ImageLoader imageLoader = ImageLoader.getInstance();
DisplayImageOptions options;
private ImageLoadingListener imageListener;
public ViewPagerAdapter(Context context, String[] img) {
this.context = FistActiivty.this;
this.img = img;
}
@Override
public int getCount() {
return img.length;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == ((RelativeLayout) object);
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
ImageView imgflag;
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemView = inflater.inflate(R.layout.viewpager_item, container,false);
imgflag = (ImageView) itemView.findViewById(R.id.flag);
imageLoader.displayImage(img[position], imgflag,options, imageListener);
((ViewPager) container).addView(itemView);
return itemView;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
// Remove viewpager_item.xml from ViewPager
((ViewPager) container).removeView((RelativeLayout) object);
}
}
此代码的结果是这样的。它每2秒更改viewpager
并且它工作正常,但它不会在更改页面之间生成动画,但是当我手动更改寻呼机时,它会为其设置动画。
如果要更改页面,如何强制它进行动画处理?
感谢。
答案 0 :(得分:0)
如果这就是你的意思,请尝试使用smoothscroll选项 在TimerTask中更改为此内容。
if (page > imges.length) {
vp.setCurrentItem(0, true);
page=1;
} else {
vp.setCurrentItem(page++, true);
}
答案 1 :(得分:0)
我认为您应该使用 PageTransformer 在页面更改时应用动画。
请访问this链接...