ViewPager做出广告轮播特效

上图:

输入图片说明

上代码:

<RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/white"
        <!-- 下面这个属性一定要,意思子控件不局限于自身空间 -->
        android:clipChildren="false">
        <android.support.v4.view.ViewPager
            android:id="@+id/viewpager"
            android:layout_width="match_parent"
            android:layout_height="119dp"
           <!-- marginleft决定左边显示的小的宽度,marginright意思一样 -->
            android:layout_marginLeft="38dp"
            android:layout_marginRight="38dp"/>
    </RelativeLayout>

Activity.java

viewpager.setOffscreenPageLimit(3);
viewpager.setAdapter(adapter);
viewpager.setPageTransformer(true, new ViewPager.PageTransformer()
{
    @Override
    public void transformPage(View page, float position)
    {
        //最重要的就是这里了
        float v = Math.abs(position);
        float v1 = (float) (0.2 * (v * v));
        page.setScaleY(1 - v1);
    }
});

PagerAdapter.java

import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;

import java.util.List;

/**
 * Created by xiaolei on 2017/4/5.
 */

public class HomeBannerAdapter extends PagerAdapter
{
    private List<View> viewLists;
    
    public HomeBannerAdapter(List<View> viewLists)
    {
        this.viewLists = viewLists;
    }
    
    @Override
    public int getCount()
    {                                                                 //获得size  
        return viewLists.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object object)
    {
        return view == object;
    }

    @Override
    public void destroyItem(View view, int position, Object object)                       //销毁Item  
    {
        ((ViewPager) view).removeView(viewLists.get(position));
    }

    @Override
    public Object instantiateItem(View view, int position)                                //实例化Item  
    {
        ((ViewPager) view).addView(viewLists.get(position), 0);
        return viewLists.get(position);
    }
}

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

扫码关注云+社区