大家好,又见面了,我是你们的朋友全栈君。
一、布局
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:layout_marginTop="10dp" android:gravity="center_horizontal" android:background="#FFFFFF" android:clipChildren="false" android:layerType="software" >
<android.support.v4.view.ViewPager android:id="@+id/vp" android:layout_width="300dp" android:layout_height="220dp" android:layout_gravity="center" android:clipChildren="false" > </android.support.v4.view.ViewPager>
</LinearLayout>
发现上面的RelativeLayout(可以用其它layout替换)和ViewPager的android:clipChildren都设置为了false。
android:clipChildren表示是否限制子View在其范围内,在animations动画以及本文的情况下可以发挥很大的作用。默认为true。
二、适配器
import android.content.Context; import android.support.v4.view.PagerAdapter; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import com.baway.chilijie.bean.Data; import com.bumptech.glide.Glide; import java.util.List; /** * Created by MacBook- on 2017/4/13. */ public class MyViewpagerAdapter extends PagerAdapter { private List<Data.MyInfo.MyInfoList> myInfoLists; private Context context; private int xinWidth; private int xinHeight; public MyViewpagerAdapter(List<Data.MyInfo.MyInfoList> myInfoLists, Context context) { this.context=context; this.myInfoLists=myInfoLists; } @Override public int getCount() { return Integer.MAX_VALUE; } @Override public boolean isViewFromObject(View view, Object object) { return view==object; } @Override public Object instantiateItem(ViewGroup container, int position) { ImageView vp_iv= new ImageView(context); Glide.with(context).load(myInfoLists.get(position%myInfoLists.size()).getActivityImg()).into(vp_iv); container.addView(vp_iv); return vp_iv; } @Override public void destroyItem(ViewGroup container, int position, Object object) { container.removeView((View) object); } }
三、自定义
PageTransformer类
import android.support.v4.view.ViewPager; import android.view.View; /** * Created by MacBook- on 2017/4/14. */ public class MyGallyPageTransformer implements ViewPager.PageTransformer { private static final float min_scale = 0.85f; @Override public void transformPage(View page, float position) { float scaleFactor = Math.max(min_scale, 1 - Math.abs(position)); if (position < -1) { page.setScaleX(scaleFactor); page.setScaleY(scaleFactor); } else if (position < 0) { page.setScaleX(scaleFactor); page.setScaleY(scaleFactor); } else if (position >= 0 && position < 1) { page.setScaleX(scaleFactor); page.setScaleY(scaleFactor); } else if (position >= 1) { page.setScaleX(scaleFactor); page.setScaleY(scaleFactor); } } }
四、Activity
public class MainActivity extends AppCompatActivity{
private ViewPager vp;@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);vp = (ViewPager) findViewById(R.id.vp);vp.setOffscreenPageLimit(3); int pagerWidth = (int) (getResources().getDisplayMetrics().widthPixels * 3.0f / 5.0f); ViewGroup.LayoutParams lp = vp.getLayoutParams(); if (lp == null) { lp = new ViewGroup.LayoutParams(pagerWidth, ViewGroup.LayoutParams.MATCH_PARENT); } else { lp.width = pagerWidth; } vp.setLayoutParams(lp); //setPageMargin表示设置图片之间的间距 vp.setPageMargin(getResources().getDimensionPixelSize(R.dimen.activity_horizontal_margin)); vp.setPageTransformer(true,new MyGallyPageTransformer()); vp.setAdapter(new MyViewpagerAdapter(data.getActivityInfo().getActivityInfoList(),getActivity())); vp.setCurrentItem(2000);} }
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/134031.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...