安卓画廊效果

1:效果图

645cffcaf8c68ad7fc2eef256e37a4c.jpg
04a4c299711908a1e90a360e7786b24.jpg
a6dcdda58333bc3ef3666a595fff265.jpg

2:代码

//首先创建一个ZoomOutPageTransformer
public class ZoomOutPageTransformer implements ViewPager.PageTransformer {
//缩小倍数可以调节
    private static final float MAX_SCALE = 1f;
    private static final float MIN_SCALE = 0.6f;//0.85f

    @Override
    public void transformPage(@NonNull View page, float position) {


        if (position <= 1) {

            float scaleFactor = MIN_SCALE + (1 - Math.abs(position)) * (MAX_SCALE - MIN_SCALE);

            page.setScaleX(scaleFactor);

            if (position > 0) {

                page.setTranslationX(-scaleFactor * 2);

            } else if (position < 0) {

                page.setTranslationX(scaleFactor * 2);

            }

            page.setScaleY(scaleFactor);

        } else {

            page.setScaleX(MIN_SCALE);

            page.setScaleY(MIN_SCALE);
        }

    }
}

2:创建activity代码


        List urlList = new ArrayList<>();
        urlList.add("https://tenfei03.cfp.cn/creative/vcg/800/new/VCG41N697946430.jpg");
        urlList.add("https://tenfei04.cfp.cn/creative/vcg/800/new/VCG41N970090320.jpg");
        urlList.add("https://tenfei02.cfp.cn/creative/vcg/800/version23/VCG41615107150.jpg");
        mViewPager.setAdapter(new FlowerAdapter(this, urlList));
        mViewPager.setPageMargin(20);
        mViewPager.setOffscreenPageLimit(urlList.size());
        mViewPager.setPageTransformer(true, new ZoomOutPageTransformer());//设置画廊模式



3:xml代码






    




4:适配器

public class FlowerAdapter extends PagerAdapter {

    private List mData;

    private Context mContext;

    @Override
    public int getCount() {
        return mData.size();// 返回数据的个数
    }

    public FlowerAdapter(Context ctx, List data) {
        this.mContext = ctx;
        this.mData = data;
    }

    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
        return view == object;// 过滤和缓存的作用

    }

    @NonNull
    @Override
    public Object instantiateItem(@NonNull ViewGroup container, int position) {
        View view = View.inflate(container.getContext(), R.layout.result_flower_item, null);

        ImageView imageView = view.findViewById(R.id.iv_photo);
        Glide.with(mContext).load(mData.get(position)).into(imageView);

        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(mContext, "当前条目:" + position, Toast.LENGTH_SHORT).show();
            }

        });
        container.addView(view);//添加到父控件

        return view;
    }
}

你要还不会,就加我微信,我去你家教你

你可能感兴趣的:(安卓画廊效果)