0

I need to make horizontal image scroll for Android. Images are lazy loaded from ArrayList with URLs. Central image must be zoomed during the swipe and locked after scrolling stops. Here is view example.

How to make it? Can i use PagerAdadpter?

Andrey Rankov
  • 1,964
  • 2
  • 17
  • 33
  • http://stackoverflow.com/a/17438032, please check posts before asking a duplicate. – Keshav Feb 04 '16 at 19:06
  • 2
    Possible duplicate of [Android ViewPager with center item bigger](http://stackoverflow.com/questions/17437343/android-viewpager-with-center-item-bigger) – Webbies Feb 04 '16 at 20:05

2 Answers2

0

UPDATE: solved it with RecyclerView
https://gist.github.com/lauw/fc84f7d04f8c54e56d56#file-snappingrecyclerview-java-L19

Andrey Rankov
  • 1,964
  • 2
  • 17
  • 33
0
BannerImgAdapter adapter = new BannerImgAdapter(activity, BAnnerData);
        recyclerView.setLayoutManager(new CenterZoomLayoutManager(this, LinearLayoutManager.HORIZONTAL,
                false));
        recyclerView.setAdapter(adapter); 
class : CenterZoomLayoutManager : 

public class CenterZoomLayoutManager extends LinearLayoutManager 
{

    //private final float mShrinkAmount = 0.15f;
    //private final float mShrinkDistance = 0.9f;

    private final float mShrinkAmount = 0.15f;
    private final float mShrinkDistance = 0.9f;

    public CenterZoomLayoutManager(Context context) {
        super(context);
    }

    public CenterZoomLayoutManager(Context context, int orientation, boolean reverseLayout) {
        super(context, orientation, reverseLayout);
    }


    @Override
    public int scrollVerticallyBy(int dy, RecyclerView.Recycler recycler, RecyclerView.State state) {
        int orientation = getOrientation();
        if (orientation == VERTICAL) {
            int scrolled = super.scrollVerticallyBy(dy, recycler, state);
            float midpoint = getHeight() / 2.f;
            float d0 = 0.f;
            float d1 = mShrinkDistance * midpoint;
            float s0 = 1.f;
            float s1 = 1.f - mShrinkAmount;
            for (int i = 0; i < getChildCount(); i++) {
                View child = getChildAt(i);
                float childMidpoint =
                        (getDecoratedBottom(child) + getDecoratedTop(child)) / 2.f;
                float d = Math.min(d1, Math.abs(midpoint - childMidpoint));
                float scale = s0 + (s1 - s0) * (d - d0) / (d1 - d0);
                child.setScaleX(scale);
                child.setScaleY(scale);
            }
            return scrolled;
        } else {
            return 0;
        }
    }

    @Override
    public int scrollHorizontallyBy(int dx, RecyclerView.Recycler recycler, RecyclerView.State state) {
        int orientation = getOrientation();
        if (orientation == HORIZONTAL) {
            int scrolled = super.scrollHorizontallyBy(dx, recycler, state);

            float midpoint = getWidth() / 2.f;
            float d0 = 0.f;
            float d1 = mShrinkDistance * midpoint;
            float s0 = 1.f;
            float s1 = 1.f - mShrinkAmount;
            for (int i = 0; i < getChildCount(); i++) {
                View child = getChildAt(i);
                float childMidpoint =
                        (getDecoratedRight(child) + getDecoratedLeft(child)) / 2.f;
                float d = Math.min(d1, Math.abs(midpoint - childMidpoint));
                float scale = s0 + (s1 - s0) * (d - d0) / (d1 - d0);
                child.setScaleX(scale);
                child.setScaleY(scale);
            }
            return scrolled;
        } else {
            return 0;
        }

    }
}
ChristianYami
  • 586
  • 1
  • 9
  • 17