Android ParallaxViewPager:ViewPager背景视差Parallax移动

51 篇文章 0 订阅

Android ParallaxViewPager:ViewPager背景视差Parallax移动

附录的相关文章,实现了一种是当ViewPager左右滑动时候,背景伴随左右滑动,附录的那一篇文章中介绍的BackgroundViewPager从一定意义上讲是把ViewPager的背景图片n等均分,每一个ViewPager页面均分得到1/n宽度的背景图片内容。

而本文要介绍的Android ParallaxViewPager不同于BackgroundViewPager,Android ParallaxViewPager实现一种在Android ViewPager页面左右翻动时候特殊的视察移动的视觉效果,如下面的动态图所示:


测试的主MainActivity.java:

[java]  view plain copy
  1. package zhangphil.viewpager;  
  2.   
  3. import android.graphics.Color;  
  4. import android.os.Bundle;  
  5. import android.support.v4.app.Fragment;  
  6. import android.support.v4.app.FragmentActivity;  
  7. import android.support.v4.app.FragmentPagerAdapter;  
  8. import android.view.Gravity;  
  9. import android.view.LayoutInflater;  
  10. import android.view.View;  
  11. import android.view.ViewGroup;  
  12. import android.widget.TextView;  
  13.   
  14. public class MainActivity extends FragmentActivity {  
  15.   
  16.     @Override  
  17.     protected void onCreate(Bundle savedInstanceState) {  
  18.         super.onCreate(savedInstanceState);  
  19.         setContentView(R.layout.activity_main);  
  20.   
  21.         ParallaxViewPager parallaxViewPager = ((ParallaxViewPager) findViewById(R.id.viewpager));  
  22.         parallaxViewPager.setOverlapPercentage(0.75f);  
  23.   
  24.         parallaxViewPager.setAdapter(new FragmentPagerAdapter(this.getSupportFragmentManager()) {  
  25.   
  26.             @Override  
  27.             public Fragment getItem(final int pos) {  
  28.   
  29.                 // 测试的Fragment  
  30.                 Fragment f = new Fragment() {  
  31.                     @Override  
  32.                     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {  
  33.                         View v = inflater.inflate(android.R.layout.simple_list_item_1, null);  
  34.                         TextView text = (TextView) v.findViewById(android.R.id.text1);  
  35.                         text.setText("Zhang Phil " + pos);  
  36.                         text.setGravity(Gravity.CENTER);  
  37.                         text.setTextColor(Color.RED);  
  38.                         text.setTextSize(50f);  
  39.                         return v;  
  40.                     }  
  41.                 };  
  42.   
  43.                 return f;  
  44.             }  
  45.   
  46.             // 假设有5个页面  
  47.             @Override  
  48.             public int getCount() {  
  49.                 return 5;  
  50.             }  
  51.         });  
  52.     }  
  53. }  


把ParallaxViewPager当作一个和Android标准的ViewPager一样使用、写进布局文件。
布局文件:

[html]  view plain copy
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     tools:context="zhangphil.viewpager.MainActivity" >  
  6.   
  7.     <zhangphil.viewpager.ParallaxViewPager  
  8.         android:id="@+id/viewpager"  
  9.         android:layout_width="match_parent"  
  10.         android:layout_height="match_parent"  
  11.         android:background="@drawable/background" />  
  12.   
  13. </RelativeLayout>  


注意要衬一张较宽的图片作为ParallaxViewPager的背景,因为通常ViewPager是加载多个页面的,这样左右翻动可以有充足的宽度跟随ViewPager左右滑动。

本例的背景图片background.jpg:


核心的ParallaxViewPager.java:

[java]  view plain copy
  1. package zhangphil.viewpager;  
  2.   
  3. import android.annotation.SuppressLint;  
  4. import android.content.Context;  
  5. import android.graphics.Bitmap;  
  6. import android.graphics.BitmapFactory;  
  7. import android.graphics.Canvas;  
  8. import android.graphics.Rect;  
  9. import android.graphics.drawable.BitmapDrawable;  
  10. import android.graphics.drawable.Drawable;  
  11. import android.support.v4.view.ViewPager;  
  12. import android.util.AttributeSet;  
  13. import android.util.Log;  
  14.   
  15. @SuppressLint("NewApi")  
  16. public class ParallaxViewPager extends ViewPager {  
  17.   
  18.     public static final int FIT_WIDTH = 0;  
  19.     public static final int FIT_HEIGHT = 1;  
  20.     public static final float OVERLAP_FULL = 1f;  
  21.     public static final float OVERLAP_HALF = 0.5f;  
  22.     public static final float OVERLAP_QUARTER = 0.25f;  
  23.     private static final float CORRECTION_PERCENTAGE = 0.01f;  
  24.     public Bitmap bitmap;  
  25.     private Rect source, destination;  
  26.     private int scaleType;  
  27.     private int chunkWidth;  
  28.     private int projectedWidth;  
  29.     private float overlap;  
  30.     private OnPageChangeListener secondOnPageChangeListener;  
  31.   
  32.     public ParallaxViewPager(Context context) {  
  33.         super(context);  
  34.         init();  
  35.     }  
  36.   
  37.     public ParallaxViewPager(Context context, AttributeSet attrs) {  
  38.         super(context, attrs);  
  39.         init();  
  40.     }  
  41.   
  42.     private void init() {  
  43.         source = new Rect();  
  44.         destination = new Rect();  
  45.         scaleType = FIT_HEIGHT;  
  46.         overlap = OVERLAP_HALF;  
  47.   
  48.         setOnPageChangeListener(new OnPageChangeListener() {  
  49.             @Override  
  50.             public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {  
  51.                 if (bitmap != null) {  
  52.                     source.left = (int) Math.floor((position + positionOffset - CORRECTION_PERCENTAGE) * chunkWidth);  
  53.                     source.right = (int) Math  
  54.                             .ceil((position + positionOffset + CORRECTION_PERCENTAGE) * chunkWidth + projectedWidth);  
  55.                     destination.left = (int) Math  
  56.                             .floor((position + positionOffset - CORRECTION_PERCENTAGE) * getWidth());  
  57.                     destination.right = (int) Math  
  58.                             .ceil((position + positionOffset + 1 + CORRECTION_PERCENTAGE) * getWidth());  
  59.                     invalidate();  
  60.                 }  
  61.   
  62.                 if (secondOnPageChangeListener != null) {  
  63.                     secondOnPageChangeListener.onPageScrolled(position, positionOffset, positionOffsetPixels);  
  64.                 }  
  65.             }  
  66.   
  67.             @Override  
  68.             public void onPageSelected(int position) {  
  69.                 if (secondOnPageChangeListener != null) {  
  70.                     secondOnPageChangeListener.onPageSelected(position);  
  71.                 }  
  72.             }  
  73.   
  74.             @Override  
  75.             public void onPageScrollStateChanged(int state) {  
  76.                 if (secondOnPageChangeListener != null) {  
  77.                     secondOnPageChangeListener.onPageScrollStateChanged(state);  
  78.                 }  
  79.             }  
  80.         });  
  81.     }  
  82.   
  83.     @Override  
  84.     protected void onSizeChanged(int w, int h, int oldw, int oldh) {  
  85.         super.onSizeChanged(w, h, oldw, oldh);  
  86.         destination.top = 0;  
  87.         destination.bottom = h;  
  88.         if (getAdapter() != null && bitmap != null)  
  89.             calculateParallaxParameters();  
  90.     }  
  91.   
  92.     private void calculateParallaxParameters() {  
  93.         if (bitmap.getWidth() < getWidth() && bitmap.getWidth() < bitmap.getHeight() && scaleType == FIT_HEIGHT) {  
  94.             Log.w(ParallaxViewPager.class.getName(),  
  95.                     "Invalid bitmap bounds for the current device, parallax effect will not work.");  
  96.         }  
  97.   
  98.         final float ratio = (float) getHeight() / bitmap.getHeight();  
  99.         if (ratio != 1) {  
  100.             switch (scaleType) {  
  101.             case FIT_WIDTH:  
  102.                 source.top = (int) ((bitmap.getHeight() - bitmap.getHeight() / ratio) / 2);  
  103.                 source.bottom = bitmap.getHeight() - source.top;  
  104.                 chunkWidth = (int) Math.ceil((float) bitmap.getWidth() / (float) getAdapter().getCount());  
  105.                 projectedWidth = chunkWidth;  
  106.                 break;  
  107.             case FIT_HEIGHT:  
  108.             default:  
  109.                 source.top = 0;  
  110.                 source.bottom = bitmap.getHeight();  
  111.                 projectedWidth = (int) Math.ceil(getWidth() / ratio);  
  112.                 chunkWidth = (int) Math  
  113.                         .ceil((bitmap.getWidth() - projectedWidth) / (float) getAdapter().getCount() * overlap);  
  114.                 break;  
  115.             }  
  116.         }  
  117.     }  
  118.   
  119.     /** 
  120.      * Sets the background from a resource file. 
  121.      * 
  122.      * @param resid 
  123.      */  
  124.     @Override  
  125.     public void setBackgroundResource(int resid) {  
  126.         bitmap = BitmapFactory.decodeResource(getResources(), resid);  
  127.     }  
  128.   
  129.     /** 
  130.      * Sets the background from a Drawable. 
  131.      * 
  132.      * @param background 
  133.      */  
  134.     @Override  
  135.     public void setBackground(Drawable background) {  
  136.         bitmap = ((BitmapDrawable) background).getBitmap();  
  137.     }  
  138.   
  139.     /** 
  140.      * Deprecated. Sets the background from a Drawable. 
  141.      * 
  142.      * @param background 
  143.      */  
  144.     @Override  
  145.     public void setBackgroundDrawable(Drawable background) {  
  146.         bitmap = ((BitmapDrawable) background).getBitmap();  
  147.     }  
  148.   
  149.     /** 
  150.      * Sets the background from a bitmap. 
  151.      * 
  152.      * @param bitmap 
  153.      * @return The ParallaxViewPager object itself. 
  154.      */  
  155.     public ParallaxViewPager setBackground(Bitmap bitmap) {  
  156.         this.bitmap = bitmap;  
  157.         return this;  
  158.     }  
  159.   
  160.     /** 
  161.      * Sets how the view should scale the background. The available choices are: 
  162.      * <ul> 
  163.      * <li>FIT_HEIGHT - the height of the image is resized to matched the height 
  164.      * of the View, also stretching the width to keep the aspect ratio. The 
  165.      * non-visible part of the bitmap is divided into equal parts, each of them 
  166.      * sliding in at the proper position.</li> 
  167.      * <li>FIT_WIDTH - the width of the background image is divided into equal 
  168.      * chunks, each taking up the whole width of the screen.</li> 
  169.      * </ul> 
  170.      * 
  171.      * @param scaleType 
  172.      * @return 
  173.      */  
  174.     public ParallaxViewPager setScaleType(final int scaleType) {  
  175.         if (scaleType != FIT_WIDTH && scaleType != FIT_HEIGHT)  
  176.             throw new IllegalArgumentException("Illegal argument: scaleType must be FIT_WIDTH or FIT_HEIGHT");  
  177.         this.scaleType = scaleType;  
  178.         return this;  
  179.     }  
  180.   
  181.     /** 
  182.      * Sets the amount of overlapping with the setOverlapPercentage(final float 
  183.      * percentage) method. This is a number between 0 and 1, the smaller it is, 
  184.      * the slower is the background scrolling. 
  185.      * 
  186.      * @param percentage 
  187.      * @return The ParallaxViewPager object itself. 
  188.      */  
  189.     public ParallaxViewPager setOverlapPercentage(final float percentage) {  
  190.         if (percentage <= 0 || percentage >= 1)  
  191.             throw new IllegalArgumentException("Illegal argument: percentage must be between 0 and 1");  
  192.         overlap = percentage;  
  193.         return this;  
  194.     }  
  195.   
  196.     /** 
  197.      * Recalculates the parameters of the parallax effect, useful after changes 
  198.      * in runtime. 
  199.      * 
  200.      * @return The ParallaxViewPager object itself. 
  201.      */  
  202.     public ParallaxViewPager invalidateParallaxParameters() {  
  203.         calculateParallaxParameters();  
  204.         return this;  
  205.     }  
  206.   
  207.     @Override  
  208.     protected void onDraw(Canvas canvas) {  
  209.         if (bitmap != null)  
  210.             canvas.drawBitmap(bitmap, source, destination, null);  
  211.     }  
  212.   
  213.     public void addOnPageChangeListener(OnPageChangeListener listener) {  
  214.         secondOnPageChangeListener = listener;  
  215.     }  
  216. }  


代码运行结果如上面的动态图所示。


Android ParallaxViewPager在github上的主页是:https://github.com/andraskindler/parallaxviewpager


附录相关文章:

《Android BackgroundViewPager:类似桌面背景壁纸随手指滑动》链接地址:http://blog.csdn.net/rokends/article/details/50373389

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
parallaxviewpager是一个具有视差滚动效果的ViewPager。所谓视差滚动(Parallax Scrolling)是指让多层背景以不同的速度移动,形成立体的运动效果,带来非常出色的视觉体验。parallaxviewpager通过设置一个背景图片,使原有的ViewPager页面与这个背景图片间实现视差效果。这就要求你的背景图片的高宽比要大于屏幕的高宽比。效果非常不错。项目地址:https://github.com/andraskindler/parallaxviewpager 效果图:如何使用:parallaxviewpager是扩展了ViewPager,所有基本使用二者是相同的。在xml中布局中创建ParallaxViewPager<com.andraskindler.parallaxviewpager.ParallaxViewPager          android:id="@ id/viewPager"          android:layout_width="match_parent"          android:layout_height="match_parent"/>2. 创建了ParallaxViewPager之后,可以使用下面的方法来设置背景,或者也可以xml设置:1) setBackgroundResource(int resid)2) setBackground(Drawable background) or setBackgroundDrawable(Drawable background)3) setBackground(Bitmap bitmap)这就好了,你现在可以使用ParallaxViewPager的全部功能了。你可以修改背景的滚动效果来优化用户体验。你也可以使用setScaleType(final int scaleType)方法来配置视图的图像缩放方式。这个方法只能和FIT_HEIGHT搭配使用,从下面的参数中进行选择:FIT_HEIGHT表示缩放图像的高度以便适配视图的高度,同时缩放图像的宽度以便保持宽高比。bitmap的不可见部分被划分成相同的区域,每个区域插入到合适的位置。FIT_HEIGHT是默认值。FIT_WIDTH表示背景图像的宽度被划分成相同的块,每一块占满整个屏幕的宽度。这个模式不适用于视差效果,因为背景和视图的滚动速度一样。你也可以使用setOverlapPercentage(final float percentage) 方法来设置重叠的程度。重叠程度值介于0到1之间,这个值越小背景就滚动地越慢,默认值是50%。ParallaxViewPageviewPager = (ParallaxViewPager)this.findViewById(R.id.viewPager); viewPager.setBackgroundResource(R.drawable.bg); viewPager.setOverlapPercentage(0.75f); PageAdapter adapter = new PageAdapter(getSupportFragmentManager()); viewPager.setAdapter(adapter);项目作者并没有提供demo,完整实例可点击上面的"下载源码"获得。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值