源码:http://download.csdn.net/detail/lm_zp/9534744
activity_main.xml
<LinearLayout 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"
android:orientation="vertical"
tools:context=".MainActivity" >
<FrameLayout
android:layout_width="match_parent"
android:layout_height="160dp"
android:clipChildren="false"
android:layout_centerInParent="true"
android:background="#aadc71ff"
>
<android.support.v4.view.ViewPager
android:id="@+id/id_viewpager"
android:layout_width="match_parent"
android:layout_marginLeft="100dp"
android:layout_marginRight="100dp"
android:clipChildren="false"
android:layout_height="120dp"
android:layout_gravity="center"
>
</android.support.v4.view.ViewPager>
</FrameLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="160dp"
android:clipChildren="false"
android:layout_centerInParent="true"
android:background="#aadc71ff"
>
<android.support.v4.view.ViewPager
android:id="@+id/id_viewpager1"
android:layout_width="match_parent"
android:layout_marginLeft="100dp"
android:layout_marginRight="100dp"
android:clipChildren="false"
android:layout_height="120dp"
android:layout_gravity="center"
>
</android.support.v4.view.ViewPager>
</FrameLayout>
</LinearLayout>
Android:clipChildren="false"
,该属性的意思就是在子View进行绘制时不要去裁切它们的显示范围。
MainActivity.java
package com.example.myviewpager;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
public class MainActivity extends Activity {
private ViewPager mViewPager ,mViewPager1;
private PagerAdapter mAdapter;
int[] imgRes = {R.drawable.a, R.drawable.b, R.drawable.c,R.drawable.d, R.drawable.e, R.drawable.f,R.drawable.g};
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mViewPager = (ViewPager) findViewById(R.id.id_viewpager);
//设置Page间间距
mViewPager.setPageMargin(20);
//设置缓存的页面数量
mViewPager.setOffscreenPageLimit(3);
//定制动画
mViewPager.setPageTransformer(true, new AlphaPageTransformer());
mViewPager.setAdapter(mAdapter = new PagerAdapter()
{
@Override
public Object instantiateItem(ViewGroup container, int position)
{
ImageView view = new ImageView(MainActivity.this);
view.setImageResource(imgRes[position]);
container.addView(view);
return view;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object)
{
container.removeView((View) object);
}
@Override
public int getCount()
{
return imgRes.length;
}
@Override
public boolean isViewFromObject(View view, Object o)
{
return view == o;
}
});
mViewPager1 = (ViewPager) findViewById(R.id.id_viewpager1);
//设置Page间间距
mViewPager1.setPageMargin(20);
//设置缓存的页面数量
mViewPager1.setOffscreenPageLimit(3);
//定制动画
mViewPager1.setPageTransformer(true, new RotateDownPageTransformer());
mViewPager1.setAdapter(mAdapter = new PagerAdapter()
{
@Override
public Object instantiateItem(ViewGroup container, int position)
{
ImageView view = new ImageView(MainActivity.this);
view.setImageResource(imgRes[position]);
container.addView(view);
return view;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object)
{
container.removeView((View) object);
}
@Override
public int getCount()
{
return imgRes.length;
}
@Override
public boolean isViewFromObject(View view, Object o)
{
return view == o;
}
});
}
}
//第一个两边隐藏
/MyViewPager/src/com/example/myviewpager/AlphaPageTransformer.java
package com.example.myviewpager;
import android.support.v4.view.ViewPager;
import android.view.View;
public class AlphaPageTransformer implements ViewPager.PageTransformer{
private static final float DEFAULT_MIN_ALPHA = 0.5f;
private float mMinAlpha = DEFAULT_MIN_ALPHA;
@Override
public void transformPage(View view, float position) {
// TODO Auto-generated method stub
if (position < -1)//看不到的一页 *
{
view.setAlpha(mMinAlpha);
} else if (position <= 1)
{ // [-1,1]
if (position < 0) //[0,-1]//滑出的页 0.0 ~ -1 *
{
float factor = mMinAlpha + (1 - mMinAlpha) * (1 + position);
view.setAlpha(factor);
} else//[1,0]//滑进的页 1 ~ 0.0 *
{
float factor = mMinAlpha + (1 - mMinAlpha) * (1 - position);
view.setAlpha(factor);
}
} else
{ // (1,+Infinity]//看不到的另一页 *
view.setAlpha(mMinAlpha);
}
}
}
第二种旋转
/MyViewPager/src/com/example/myviewpager/RotateDownPageTransformer.java
package com.example.myviewpager;
import android.support.v4.view.ViewPager.PageTransformer;
import android.view.View;
public class RotateDownPageTransformer implements PageTransformer {
private static final float DEFAULT_MAX_ROTATE = 15.0f;
private float mMaxRotate = DEFAULT_MAX_ROTATE;
public void transformPage(View view, float position)
{
if (position < -1)
{ // [-Infinity,-1)
// This page is way off-screen to the left.
view.setRotation(mMaxRotate * -1);
view.setPivotX(view.getWidth());
view.setPivotY(view.getHeight());
} else if (position <= 1)
{ // [-1,1]
if (position < 0)//[0,-1]
{
view.setPivotX(view.getWidth() * (0.5f + 0.5f * (-position)));
view.setPivotY(view.getHeight());
view.setRotation(mMaxRotate * position);
} else//[1,0]
{
view.setPivotX(view.getWidth() * 0.5f * (1 - position));
view.setPivotY(view.getHeight());
view.setRotation(mMaxRotate * position);
}
} else
{ // (1,+Infinity]
// This page is way off-screen to the right.
view.setRotation(mMaxRotate);
view.setPivotX(view.getWidth() * 0);
view.setPivotY(view.getHeight());
}
}
}