Chinaunix首页 | 论坛 | 博客
  • 博客访问: 418479
  • 博文数量: 121
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 1393
  • 用 户 组: 普通用户
  • 注册时间: 2014-03-11 12:17
个人简介

www.vibexie.com vibexie@qq.com

文章分类

全部博文(121)

文章存档

2015年(55)

2014年(66)

我的朋友

分类: Android平台

2015-04-04 10:40:10

ActivityMainfest.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android=""
  3.     package="cn.com.xiebiao.myviewpager" >

  4.     <application
  5.         android:allowBackup="true"
  6.         android:icon="@mipmap/ic_launcher"
  7.         android:label="@string/app_name"
  8.         android:theme="@style/Theme.AppCompat.NoActionBar" >
  9.         <activity
  10.             android:name=".MainActivity"
  11.             android:label="@string/app_name" >
  12.             <intent-filter>
  13.                 <action android:name="android.intent.action.MAIN" />

  14.                 <category android:name="android.intent.category.LAUNCHER" />
  15.             </intent-filter>
  16.         </activity>
  17.     </application>

  18. </manifest>

activity_main.xml

  1. <RelativeLayout xmlns:android=""
  2.     xmlns:tools="" android:layout_width="match_parent"
  3.     android:layout_height="match_parent" >
  4.     <android.support.v4.view.ViewPager
  5.         android:id="@+id/viewPager"
  6.         android:layout_width="match_parent"
  7.         android:layout_height="match_parent"/>
  8. </RelativeLayout>

MainActivity.java

  1. package cn.com.xiebiao.myviewpager;

  2. import android.app.Activity;
  3. import android.support.v4.view.PagerAdapter;
  4. import android.support.v4.view.ViewPager;
  5. import android.os.Bundle;
  6. import android.view.View;
  7. import android.view.ViewGroup;
  8. import android.widget.ImageView;

  9. import java.util.ArrayList;
  10. import java.util.List;

  11. /***
  12.  * 3.0以下版本使用viewpage,引入nineoldandroids.jar,1.重写官方的两个特效的属性动画方法,并且2.在setPageTransformer
  13.  * 源码中修改最低版本判断的if函数即可。
  14.  * 如果要实现自定义的viewpager特效,只需要修改官方提供的特效效果代码,即可。
  15.  */

  16. public class MainActivity extends Activity {
  17.     private ViewPager viewPager;
  18.     private int[] images={R.drawable.guide1,R.drawable.guide2,R.drawable.guide3,R.drawable.guide4};
  19.     private List<ImageView> imageViewList=new ArrayList<ImageView>();



  20.     @Override
  21.     protected void onCreate(Bundle savedInstanceState) {
  22.         super.onCreate(savedInstanceState);
  23.         setContentView(R.layout.activity_main);
  24.         viewPager = (ViewPager) this.findViewById(R.id.viewPager);
  25.         /**
  26.          * 官方文档的特效一
  27.          */
  28.         //viewPager.setPageTransformer(true, new ZoomOutPageTransformer());
  29.         /**
  30.          * 官方文档的特效二
  31.          */
  32.         viewPager.setPageTransformer(true,new DepthPageTransformer());
  33.         /**
  34.          * 正常效果
  35.          */
  36.         viewPager.setAdapter(new PagerAdapter() {
  37.             @Override
  38.             public Object instantiateItem(ViewGroup container, int position) {
  39.                 ImageView imageView = new ImageView(MainActivity.this);
  40.                 imageView.setImageResource(images[position]);
  41.                 imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
  42.                 container.addView(imageView);
  43.                 imageViewList.add(imageView);

  44.                 return imageView;
  45.             }

  46.             @Override
  47.             public int getCount() {
  48.                 return images.length;
  49.             }

  50.             @Override
  51.             public boolean isViewFromObject(View view, Object o) {
  52.                 return view == o;
  53.             }

  54.             @Override
  55.             public void destroyItem(ViewGroup container, int position, Object object) {
  56.                 container.removeView(imageViewList.get(position));
  57.             }
  58.         });
  59.     }

  60.     /**
  61.      * 官方文档的特效一
  62.      */
  63.     class ZoomOutPageTransformer implements ViewPager.PageTransformer {
  64.         private static final float MIN_SCALE = 0.85f;
  65.         private static final float MIN_ALPHA = 0.5f;

  66.         public void transformPage(View view, float position) {
  67.             int pageWidth = view.getWidth();
  68.             int pageHeight = view.getHeight();

  69.             if (position < -1) { // [-Infinity,-1)
  70.                 // This page is way off-screen to the left.
  71.                 view.setAlpha(0);

  72.             } else if (position <= 1) { // [-1,1]
  73.                 // Modify the default slide transition to shrink the page as well
  74.                 float scaleFactor = Math.max(MIN_SCALE, 1 - Math.abs(position));
  75.                 float vertMargin = pageHeight * (1 - scaleFactor) / 2;
  76.                 float horzMargin = pageWidth * (1 - scaleFactor) / 2;
  77.                 if (position < 0) {
  78.                     view.setTranslationX(horzMargin - vertMargin / 2);
  79.                 } else {
  80.                     view.setTranslationX(-horzMargin + vertMargin / 2);
  81.                 }

  82.                 // Scale the page down (between MIN_SCALE and 1)
  83.                 view.setScaleX(scaleFactor);
  84.                 view.setScaleY(scaleFactor);

  85.                 // Fade the page relative to its size.
  86.                 view.setAlpha(MIN_ALPHA +
  87.                         (scaleFactor - MIN_SCALE) /
  88.                                 (1 - MIN_SCALE) * (1 - MIN_ALPHA));

  89.             } else { // (1,+Infinity]
  90.                 // This page is way off-screen to the right.
  91.                 view.setAlpha(0);
  92.             }
  93.         }
  94.     }

  95.     /**
  96.      * 官方文档的特效二
  97.      */
  98.     class DepthPageTransformer implements ViewPager.PageTransformer {
  99.         private static final float MIN_SCALE = 0.75f;

  100.         public void transformPage(View view, float position) {
  101.             int pageWidth = view.getWidth();

  102.             if (position < -1) { // [-Infinity,-1)
  103.                 // This page is way off-screen to the left.
  104.                 view.setAlpha(0);

  105.             } else if (position <= 0) { // [-1,0]
  106.                 // Use the default slide transition when moving to the left page
  107.                 view.setAlpha(1);
  108.                 view.setTranslationX(0);
  109.                 view.setScaleX(1);
  110.                 view.setScaleY(1);

  111.             } else if (position <= 1) { // (0,1]
  112.                 // Fade the page out.
  113.                 view.setAlpha(1 - position);

  114.                 // Counteract the default slide transition
  115.                 view.setTranslationX(pageWidth * -position);

  116.                 // Scale the page down (between MIN_SCALE and 1)
  117.                 float scaleFactor = MIN_SCALE
  118.                         + (1 - MIN_SCALE) * (1 - Math.abs(position));
  119.                 view.setScaleX(scaleFactor);
  120.                 view.setScaleY(scaleFactor);

  121.             } else { // (1,+Infinity]
  122.                 // This page is way off-screen to the right.
  123.                 view.setAlpha(0);
  124.             }
  125.         }
  126.     }

  127. }



阅读(1115) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~