ViewPager自带了一个setPageTransformer用于设置切换动画。
想详细了解可以看鸿洋的这篇博客。
http://blog.csdn.net/lmj623565791/article/details/40411921
我介绍一个没有滑动效果的ViewPager动画,移动时只有当前页可见度变低,下一页可见度变高,都在一页上面,就有两幅图片合一的感觉。
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
DisplayImageOptions options = new DisplayImageOptions.Builder()
.cacheInMemory().bitmapConfig(Bitmap.Config.RGB_565)
.imageScaleType(ImageScaleType.IN_SAMPLE_INT).build();
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(
this).threadPriority(Thread.MIN_PRIORITY + 3)
.denyCacheImageMultipleSizesInMemory().threadPoolSize(3)
.tasksProcessingOrder(QueueProcessingType.LIFO)
.memoryCacheSize(1048576 * 10)
.memoryCache(new WeakMemoryCache())
.defaultDisplayImageOptions(options)
// 限制图片个数
.memoryCache(new LRULimitedMemoryCache(500)).build();
// Initialize ImageLoader with configuration.
ImageLoader.getInstance().init(config);
ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
DiscoverBannerAdapter adapter = new DiscoverBannerAdapter(this);
ArrayList<String> urls = new ArrayList<>();
urls.add("http://pic4.zhimg.com/70/8c27b6d23d2d99523fb613cba09c4bbf_mt.jpg");
urls.add("http://pic2.zhimg.com/70/71b5c66532a03c777cad464529291921_mt.jpg");
urls.add("http://pic3.zhimg.com/70/b0b2f8e1d3ba14b5572c13b438ebf092_mt.jpg");
urls.add("http://pic2.zhimg.com/70/b0ad316d2ced6052b15d23b0b6bef375_mt.jpg");
adapter.setUrls(urls);
viewPager.setPageTransformer(true, new MyTransformer());
viewPager.setAdapter(adapter);
}
public class MyTransformer implements PageTransformer {
public void transformPage(View view, float position) {
int pageWidth = view.getWidth();
if (position < -1.0f) {
view.setAlpha(AutoScrollHelper.RELATIVE_UNSPECIFIED);
} else if (position <= 0.0f) {
view.setAlpha(1.0f + position);
view.setTranslationX(((float) (-pageWidth)) * position);
} else if (position <= 1.0f) {
view.setAlpha(1.0f - position);
view.setTranslationX(((float) (-pageWidth)) * position);
} else {
view.setAlpha(AutoScrollHelper.RELATIVE_UNSPECIFIED);
}
}
}
}
关键在
MyTransformer,通过setAlpha方法来改变透明度。