网址:http://android.jobbole.com/81083/
ZoomOutPageTransformer
当在相邻界面滑动时,这个page transformer使页面收缩并褪色。当页面越靠近中心,它将渐渐还原到正常大小并且图像渐入。
Depth Page Transformer
这个page transformer使用默认动画的屏幕左滑动画。但是为右滑使用一种“潜藏”效果的动画。潜藏动画将page淡出,并且线性缩小它。
mPager.setPageTransformer(true, new ZoomOutPageTransformer());
ZoomOutPageTransformer .java 类
package
com
.
zhy
.
view
;
import
android
.
annotation
.
SuppressLint
;
import
android
.
support
.
v4
.
view
.
ViewPager
;
import
android
.
util
.
Log
;
import
android
.
view
.
View
;
public
class
ZoomOutPageTransformer
implements
ViewPager
.
PageTransformer
{
private
static
final
float
MIN_SCALE
=
0.85f
;//该值是滑动时候显示的大小(滑动中)
private
static
final
float
MIN_ALPHA
=
0.5f
;
@SuppressLint
(
"NewApi"
)
public
void
transformPage
(
View
view
,
float
position
)
{
int
pageWidth
=
view
.
getWidth
(
)
;
int
pageHeight
=
view
.
getHeight
(
)
;
Log
.
e
(
"TAG"
,
view
+
" , "
+
position
+
""
)
;
if
(
position
<
-
1
)
{
// [-Infinity,-1)
// This page is way off-screen to the left.
view
.
setAlpha
(
0
)
;
}
else
if
(
position
<=
1
)
//a页滑动至b页 ; a页从 0.0 -1 ;b页从1 ~ 0.0
{
// [-1,1]
// Modify the default slide transition to shrink the page as well
float
scaleFactor
=
Math
.
max
(
MIN_SCALE
,
1
-
Math
.
abs
(
position
)
)
;
float
vertMargin
=
pageHeight
*
(
1
-
scaleFactor
)
/
2
;
float
horzMargin
=
pageWidth
*
(
1
-
scaleFactor
)
/
2
;
if
(
position
<
0
)
{
view
.
setTranslationX
(
horzMargin
-
vertMargin
/
2
)
;
}
else
{
view
.
setTranslationX
(
-
horzMargin
+
vertMargin
/
2
)
;
}
// Scale the page down (between MIN_SCALE and 1)
view
.
setScaleX
(
scaleFactor
)
;
view
.
setScaleY
(
scaleFactor
)
;
// Fade the page relative to its size.
view
.
setAlpha
(
MIN_ALPHA
+
(
scaleFactor
-
MIN_SCALE
)
/
(
1
-
MIN_SCALE
)
*
(
1
-
MIN_ALPHA
)
)
;
}
else
{
// (1,+Infinity]
// This page is way off-screen to the right.
view
.
setAlpha
(
0
)
;
}
}
}