1. 程式人生 > >viewpager實現畫廊(中間圖片全部顯示,左右顯示一部分b佈局)無限輪播效果

viewpager實現畫廊(中間圖片全部顯示,左右顯示一部分b佈局)無限輪播效果

一、佈局

<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。

二、介面卡

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