本文主要是介绍ViewPager切换动画PageTransformer的使用,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
Android从3.0开始添加了属性动画后,诸多难以实现的动画都可以轻松解决了,v4包下的ViewPager控件当然也不例外,相对于非常平庸的默认切换动画,Google官方给我们展示了两个动画例子:DepthPageTransformer和ZoomOutPageTransformer,比较炫。
实际上我们也可以通过实现ViewPager.PageTransformer接口来做出完全不同的切换动画效果。实现PageTransformer接口就只要实现一个方法:transformPage(View view,float position);该方法的两个参数尤为重要,理解它是我们自定义ViewPager动画的重点。
transformPage(View view,float position);
- 第一个参数view顾名思义就是我们需要滑动的那个View。
- 第二个参数position,这个float类型的参数并不是我们平常所理解的位置,而是表示滑动的View的一种状态,比如:当滑动到正全屏时,position是0,而向左滑动,使得右边刚好有一部被进入屏幕时,position是1,如果前一页和下一页基本各在屏幕占一半时,前一页的position是-0.5,后一页的posiotn是0.5,所以根据position的值我们就可以自行设置需要的alpha,ScaleX/Y,translationX/Y等参数值。
下面,我们就来看看Google官方提供的两种动画效果的源码及对应的动画,理解了原理自定义动画可以仿照源码了:
DepthPageTransformer:
- import android.annotation.SuppressLint;
- import android.support.v4.view.ViewPager.PageTransformer;
- import android.view.View;
-
- 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) {
-
- view.setAlpha(0);
- } else if (position <= 0) {
-
-
- view.setAlpha(1);
- view.setTranslationX(0);
- view.setScaleX(1);
- view.setScaleY(1);
- } else if (position <= 1) {
-
- view.setAlpha(1 - position);
-
- view.setTranslationX(pageWidth * -position);
-
- float scaleFactor = MIN_SCALE + (1 - MIN_SCALE)
- * (1 - Math.abs(position));
- view.setScaleX(scaleFactor);
- view.setScaleY(scaleFactor);
- } else {
-
- view.setAlpha(0);
-
- }
- }
-
- }
效果为:
ZoomOutPageTransformer:
- import android.support.v4.view.ViewPager.PageTransformer;
- import android.view.View;
-
- 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) {
-
- view.setAlpha(0);
- } else if (position <= 1) {
-
-
- 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);
- }
-
- view.setScaleX(scaleFactor);
- view.setScaleY(scaleFactor);
-
- view.setAlpha(MIN_ALPHA + (scaleFactor - MIN_SCALE)
- / (1 - MIN_SCALE) * (1 - MIN_ALPHA));
- } else {
-
- view.setAlpha(0);
- }
- }
- }
效果为:
下面我们来看看怎么使用ViewPager,让它加入动画。
其实就是通过mViewPager.setPageTransformer(true, new DepthPageTransformer());来设置。
布局:
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- tools:context=".MainActivity">
- <android.support.v4.view.ViewPager
- android:id="@+id/viewpager"
- android:layout_width="match_parent"
- android:layout_height="match_parent" />
- </RelativeLayout>
代码:
- public class MainActivity extends ActionBarActivity {
- private int[] imageId = new int[]{R.mipmap.bg,R.mipmap.one, R.mipmap.two, R.mipmap.three};
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- ViewPager mViewPager = (ViewPager) findViewById(R.id.viewpager);
- mViewPager.setAdapter(new ViewPagerAdapter(this,imageId));
-
- mViewPager.setPageTransformer(true,new DepthPageTransformer());
- }
-
- class ViewPagerAdapter extends PagerAdapter {
- private ArrayList<ImageView> mImages ;
- private int[] imagesId;
- private Context context;
- public ViewPagerAdapter(Context context,int[] resourceId){
- this.context = context;
- this.imagesId = resourceId;
- mImages = new ArrayList<>();
- }
- @Override
- public Object instantiateItem(ViewGroup container, int position) {
- ImageView imageView = new ImageView(context);
- imageView.setImageResource(imagesId[position]);
- imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
- container.addView(imageView);
- mImages.add(imageView);
- return imageView;
- }
-
- @Override
- public void destroyItem(ViewGroup container, int position, Object object) {
-
- container.removeView(mImages.get(position));
- }
-
- @Override
- public int getCount() {
- return imagesId.length;
- }
-
- @Override
- public boolean isViewFromObject(View view, Object object) {
- return view==object;
- }
- }
- }
另外说明一点:
关于ViewPager的适配器,有两种实现:
- class ViewPagerAdapter extends PagerAdapter {
- private ArrayList<ImageView> mImages ;
- private int[] imagesId;
- private Context context;
- public ViewPagerAdapter(Context context,int[] resourceId){
- this.context = context;
- this.imagesId = resourceId;
- mImages = new ArrayList<>();
- }
- @Override
- public Object instantiateItem(ViewGroup container, int position) {
- ImageView imageView = new ImageView(context);
- imageView.setImageResource(imagesId[position]);
- imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
- container.addView(imageView);
- mImages.add(imageView);
- return imageView;
- }
-
- @Override
- public void destroyItem(ViewGroup container, int position, Object object) {
-
- container.removeView(mImages.get(position));
- }
-
- @Override
- public int getCount() {
- return imagesId.length;
- }
-
- @Override
- public boolean isViewFromObject(View view, Object object) {
- return view==object;
- }
- 继承至FragmentPagerAdapter:即
- class FragmentViewPagerAdapter extends FragmentPagerAdapter{
- private final List<Fragment> mFragments = new ArrayList<>();
- public FragmentViewPagerAdapter(FragmentManager fm) {
- super(fm);
- }
- public void addFragment(Fragment fragment) {
- mFragments.add(fragment);
- }
- @Override
- public Fragment getItem(int position) {
-
- return mFragments.get(position);
- }
-
- @Override
- public int getCount() {
-
- return mFragments.size();
- }
- }
好了,就说到这了。。。
源码地址:http://download.csdn.net/detail/u010687392/8913511
这篇关于ViewPager切换动画PageTransformer的使用的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!