标签:
前面我们学习了怎么了ViewPager添加动画效果,今天我来说下怎么自定义动画效果
首先我们理解系统给我们的那两个类。我这里贴上去一个
1 public class ZoomOutPageTransformer implements PageTransformer { 2 private static final float MIN_SCALE = 0.85f; 3 private static final float MIN_ALPHA = 0.5f; 4 5 public void transformPage(View view, float position) { 6 int pageWidth = view.getWidth(); 7 int pageHeight = view.getHeight(); 8 9 if (position < -1) { // [-Infinity,-1) 10 // This page is way off-screen to the left. 11 view.setAlpha(0); 12 13 } else if (position <= 1) { // [-1,1] 14 // Modify the default slide transition to shrink the page as well 15 float scaleFactor = Math.max(MIN_SCALE, 1 - Math.abs(position)); 16 float vertMargin = pageHeight * (1 - scaleFactor) / 2; 17 float horzMargin = pageWidth * (1 - scaleFactor) / 2; 18 if (position < 0) { 19 view.setTranslationX(horzMargin - vertMargin / 2); 20 } else { 21 view.setTranslationX(-horzMargin + vertMargin / 2); 22 } 23 24 // Scale the page down (between MIN_SCALE and 1) 25 view.setScaleX(scaleFactor); 26 view.setScaleY(scaleFactor); 27 28 // Fade the page relative to its size. 29 view.setAlpha(MIN_ALPHA + 30 (scaleFactor - MIN_SCALE) / 31 (1 - MIN_SCALE) * (1 - MIN_ALPHA)); 32 33 } else { // (1,+Infinity] 34 // This page is way off-screen to the right. 35 view.setAlpha(0); 36 } 37 } 38 39 }
我们会发现transformPage方法中有两个参数view和position。我来解释下这两个参数的意思:view表示的是当前操作的view(view在出去的view和进来的view不断切换),position表示的是当前的view的位置。首先说下不同的view有不同position,出去的view的position位于[-1,0]之间,进来的view的position位于[0,1]之间。其它的view分别位于[-Infinity,-1)和(1,+Infinity],这些的view都是不可见的。
我们通过position的不同范围,可以判断当前的操作的是哪个view明白了这一点,对于我们自定义不同的动画效果有很大的帮助。
改写的基本思想:在源代码的基础上,写自己的喜欢的动画。
代码
1 package com.example.android_viewpager; 2 3 import android.support.v4.view.ViewPager.PageTransformer; 4 import android.view.View; 5 6 public class MyTransformer implements PageTransformer{ 7 public void transformPage(View view, float position) { 8 float angle = 30; 9 if (position < -1) { // [-Infinity,-1) 10 // This page is way off-screen to the left. 11 view.setAlpha(0); 12 13 } else if (position <= 1) { // [-1,1] 14 if (position < 0) //操作的是出去view 15 { 16 angle *= position; 17 //这两个是设置旋转中心 18 view.setPivotX(view.getWidth() / 2); 19 view.setPivotY(view.getMeasuredHeight());//view的底部 20 view.setRotation(angle); 21 angle = 50; 22 } else //操作的进来的view 23 { 24 angle *= position; 25 view.setPivotX(view.getWidth() / 2); 26 view.setPivotY(view.getMeasuredHeight());//view的底部 27 view.setRotation(angle); 28 angle = 50; 29 } 30 31 } else { // (1,+Infinity] 32 // This page is way off-screen to the right. 33 view.setAlpha(0); 34 } 35 } 36 37 38 }
标签:
原文地址:http://www.cnblogs.com/Stay-Hungry-Stay-Foolish/p/5901879.html