大家好,又见面了,我是你们的朋友全栈君。
一、布局
<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);
} }