ViewPager中间大两边小效果的实现

Activity部分:

public class MainActivity extends Activity {

    private ViewPager mViewPager;
    private ViewPaperAdapter mVpAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mViewPager = (ViewPager) this.findViewById(R.id.viewpager);

        List imageIds = new ArrayList();
        imageIds.add(R.drawable.image01);
        imageIds.add(R.drawable.image02);
        imageIds.add(R.drawable.image03);

        mVpAdapter = new ViewPaperAdapter(this, imageIds);
        mViewPager.setOffscreenPageLimit(3);
        mViewPager.setAdapter(mVpAdapter);
//      这个值主要负责两边控件与中间控件的距离
        mViewPager.setPageMargin(-300);
        mViewPager.setPageTransformer(false, new DepthPageTransformer());
    }

}

适配器部分:

public class ViewPaperAdapter extends PagerAdapter {
    private Context mContext;
    private List mImageIds;

    /**
     * @param mainActivity
     * @param integers
     */
    public ViewPaperAdapter(Context context, List imageIds) {
        this.mContext = context;
        this.mImageIds = imageIds;
    }

    @Override
    public int getCount() {
        return this.mImageIds.size();
    }

    @Override
    public boolean isViewFromObject(View arg0, Object arg1) {
        return arg0 == arg1;
    }

    @Override
    public void destroyItem(View container, int position, Object object) {
        ((ViewPager) container).removeView((View) object);
    }

    @Override
    public Object instantiateItem(View container, int position) {
        ImageView image=new ImageView(mContext);
        image.setImageResource(mImageIds.get(position));
        image.setScaleType(ScaleType.FIT_XY);
        image.setPadding(200, 80, 200, 80);
        ((ViewPager) container).addView(image);
        return image;
    }

}

效果部分:

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public class DepthPageTransformer implements PageTransformer {
    private static final float MIN_SCALE = 0.5f;
    private static final float MIN_ALPHA = 0.8f;

    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    @SuppressLint("NewApi")
    public void transformPage(View view, float position) {
        if (position < -1) { // [-Infinity,-1)
            // This page is way off-screen to the left.
            view.setScaleY(MIN_SCALE);
            view.setAlpha(MIN_ALPHA);
        } else if (position == 0) { // [-1,0]
            // Use the default slide transition when moving to the left page
            view.setScaleY(MIN_SCALE);
            view.setAlpha(MIN_ALPHA);
        } else if (position <= 1) { // (0,1]
            // Fade the page out.

            // Counteract the default slide transition

            // Scale the page down (between MIN_SCALE and 1)
            float scaleFactor = MIN_SCALE + (1 - MIN_SCALE)* (1 - Math.abs(position));
            float alphaFactor = MIN_ALPHA + (1 - MIN_ALPHA)* (1 - Math.abs(position));
            view.setScaleY(scaleFactor);
            view.setAlpha(alphaFactor);
        } else { // (1,+Infinity]
            // This page is way off-screen to the right.
            view.setScaleY(MIN_SCALE);
            view.setAlpha(MIN_ALPHA);
        }
    }
}

xml布局:


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffff00"
    android:orientation="vertical" >

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingLeft="100dp"
        android:paddingRight="100dp" />

LinearLayout>

你可能感兴趣的:(ViewPager)