今天讓咱們瞭解一下,Android3.0以後ViewPager切換時候的一個動畫。Google給咱們展現了兩個動畫例子:DepthPageTransformer和ZoomOutPageTransformer,代碼以下:java
public class DepthPageTransformer implements PageTransformer { private static float MIN_SCALE = 0.75f; @SuppressLint("NewApi") @Override public void transformPage(View view, float position) { int pageWidth = view.getWidth(); if (position < -1) { // [-Infinity,-1)//This page is way off-screen to the left. view.setAlpha(0); } else if (position <= 0) { // [-1,0]Use //the default slide transition when moving to the left page view.setAlpha(1); view.setTranslationX(0); view.setScaleX(1); view.setScaleY(1); } else if (position <= 1) { // (0,1]// Fade the page out. view.setAlpha(1 - position); // Counteract the default slide transition view.setTranslationX(pageWidth * -position); // Scale the page down (between MIN_SCALE and 1) float scaleFactor = MIN_SCALE + (1 - MIN_SCALE) * (1 - Math.abs(position)); view.setScaleX(scaleFactor); view.setScaleY(scaleFactor); } else { // (1,+Infinity] // This page is way off-screen to the right. view.setAlpha(0); } } }
public class ZoomOutPageTransformer implements PageTransformer { private static float MIN_SCALE = 0.85f; private static float MIN_ALPHA = 0.5f; @Override public void transformPage(View view, float position) { int pageWidth = view.getWidth(); int pageHeight = view.getHeight(); if (position < -1) { // [-Infinity,-1) // This page is way off-screen to the left. view.setAlpha(0); } else if (position <= 1) { // [-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); } } }
咱們在使用的時候只需mPager.setPageTransformer(true, new DepthPageTransformer())便可,下面讓咱們看看transformPage方法怎麼實現動畫的!!!ide
transformPage方法有兩個參數,第一個view固然就是應用動畫的那個控件,第二個是一個float類型的值,不是咱們日常見到的position位置,而是當前滑動狀態的表示,相對於當前position的position。它有三個臨界值-1 0 1,0表明當前屏幕顯示的view的position,1表明當前view的下一個view所在的position,-1表明當前view的前一個view所在的position。不知道我講清楚沒有?動畫
那麼請看下面的圖片:orm
下面讓咱們看看當前view左滑、右滑時各個view positon的變化狀況:blog
好了,上面的表格你們能夠經過打log的形式驗證一下。知道了各個位置的view的position的變化狀況,下面讓咱們看看動畫吧。這裏咱們不作炫酷的動畫,只作透明度的動畫,方便你們理解transformPage方法。圖片
前提不知道你們了不瞭解alpha這個屬性,它的取值範圍在0和1之間,0表明徹底透明,1表明徹底不透明。廢話很少說看動畫:get
public class ViewpagerTransformAnim implements ViewPager.PageTransformer { @Override public void transformPage(View page, float position) { float alpha = 0.0f; if (0.0f <= position && position <= 1.0f) { alpha = 1.0f - position; } else if (-1.0f <= position && position < 0.0f) { alpha = position + 1.0f; } page.setAlpha(alpha); } }
那麼咱們就對上面的代碼簡單的解釋一下:無論當前view左滑仍是右滑,當我滑動的時候,根據以前的表格咱們知道上面的兩個判斷語句都會走到,這是由於你滑動的時候,確定是兩個view作動畫的切換(不算兩個邊界,邊界的話只會走一個),因爲alpha取值在0和1之間,position取值在-1和1之間。因此有了條件語句中的代碼。看下效果吧:it
很簡單的一個動畫,但願對你們有所幫助!!!io