android3.0自带ViewPager动画

原创 2016年05月30日 11:10:02

Create ViewPager transitions: a PagerTransformer example

The ViewPager is a great UI element, providing a smooth swipe animation for switching between pages. But what if a different scrolling effect is required? Fear not, the support library provides a useful solution called the PagerTransformer. It was introduced in revision 11, and is supported from API level 11 (Honeycomb) or greater.

Usage is pretty straightforward, just attach a PageTransformer to the ViewPager:

viewpager.setPageTransformer(false, new ViewPager.PageTransformer() {
    @Override
    public void transformPage(View page, float position) {
        // do transformation here
        }
});

The transformPage() method has a View and a position parameter. The former represents the current view or fragment, while the latter contains its position. Scrolling events are triggered by both the starting page and the target page, the corresponding _transformPage() _calls occur simultaneously. A value of zero means the current page is in the center, 1 means a full page offset to the right side, -1 means a full page offset to the left side. Quoted from the developer site: > The position parameter indicates where a given page is located relative to the center of the screen. It is a dynamic property that changes as the user scrolls through the pages. When a page fills the screen, its position value is 0. When a page is drawn just off the right side of the screen, its position value is 1. If the user scrolls halfway between pages one and two, page one has a position of -0.5 and page two has a position of 0.5. It is a good idea to normalize the position, so you don’t have to bother with the numbers being negative or positive. It’s pretty easy, just do this in every call:

final float normalizedposition = Math.abs(Math.abs(position) - 1);

Now you have a variable that goes from 0 to 1 (and the other way, respectively) if the user scrolls the ViewPager. What you do with it is up to your imagination; here are some basic examples. First, we’ll fade the pages in and out:

@Override
public void transformPage(View page, float position) {
    final float normalizedposition = Math.abs(Math.abs(position) - 1);
    page.setAlpha(normalizedposition);
}

These lines perform a scaling effect from and to 50%:

@Override
public void transformPage(View page, float position) {
final float normalizedposition = Math.abs(Math.abs(position) - 1);
    page.setScaleX(normalizedposition / 2 + 0.5f);
    page.setScaleY(normalizedposition / 2 + 0.5f);
}

The last example rotates the pages around their Z axis by 30 degrees; you don’t need to normalize for this one. The effect is similar to the cover flow UI pattern:

@Override
public void transformPage(View page, float position) {
    page.setRotationY(position * -30);
}

Of course rotating the views or fragments around the axes is also possible, but the effect might be a bit overwhelming for the user, so I don’t recommend it. Anyway, it can be done with the other setRotation()methods.

These are just some basic examples, you can do much neater stuff with the PagerTransformer. See this linkat the official dev site for two good looking effects. You can also combine it with a PagerTitleStrip or aPagerTabStrip.

版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

Android 实现个性的ViewPager切换动画 实战PageTransformer(兼容Android3.0以下)

Android 实现个性的ViewPager切换动画 实战PageTransformer(兼容Android3.0以下) 分类: 【android 进阶之路】2014-10-30 09:...
  • anlun
  • anlun
  • 2014-11-18 23:34
  • 329

Android 实现个性的ViewPager切换动画 实战PageTransformer(兼容Android3.0以下)

转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/40411921,本文出自:【张鸿洋的博客】 1、概述 之前写过一篇博文...

【Android】实现个性的ViewPager切换动画 实战PageTransformer(兼容Android3.0以下)

转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/40411921,本文出自:【张鸿洋的博客】 1、概述 之前写过一篇博文...

Android 实现个性的ViewPager切换动画 实战PageTransformer(兼容Android3.0以下)

转自:http://blog.csdn.net/lmj623565791/article/details/40411921,本文出自:【张鸿洋的博客】 1、概述 之前写过一篇博文:Andr...

Android 实现个性的ViewPager切换动画 实战PageTransformer(兼容Android3.0以下)

Android 实现个性的ViewPager切换动画 实战PageTransformer(兼容Android3.0以下)

Android 实现个性的ViewPager切换动画 实战PageTransformer(兼容Android3.0以下)

转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/40411921,本文出自:【张鸿洋的博客】 1、概述 之前写过一篇博文...

Android 实现个性的ViewPager切换动画 实战PageTransformer(兼容Android3.0以下)

本文出自: 【张鸿洋的博客】http://blog.csdn.net/lmj623565791/article/details/40411921/ 1、概述 之前写过一篇...

Android 实现个性的ViewPager切换动画 实战PageTransformer(兼容Android3.0以下)

转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/40411921,本文出自:【张鸿洋的博客】 1、概述 之前写过一篇博文...

Android 实现个性的ViewPager切换动画 实战PageTransformer(兼容Android3.0以下)

1、概述 之前写过一篇博文:Android 自定义 ViewPager 打造千变万化的图片切换效果。有兄弟提出,ViewPager自带了一个setPageTransformer用于设置切换动画~...

Android 实现个性的ViewPager切换动画 实战PageTransformer(兼容Android3.0以下)

转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/40411921,本文出自:【张鸿洋的博客】 1、概述 之前写过一篇博文:A...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)