美文网首页
ViewPager做出广告轮播特效

ViewPager做出广告轮播特效

作者: xiaolei123 | 来源:发表于2017-11-17 15:43 被阅读55次

    上图:


    输入图片说明

    上代码:

    <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);
        }
    }
    

    相关文章

      网友评论

          本文标题:ViewPager做出广告轮播特效

          本文链接:https://www.haomeiwen.com/subject/jbyjvxtx.html