Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
375 views
in Technique[技术] by (71.8m points)

android - Zoom central image recycler view

I have RecyclerView with images. It based on this solution. Images are lazy loaded into view with Glide. I need to add zoom on central image like in this: example

How can i do it?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The most direct way to affect what you want is to extend LinearLayoutManager. As you've no doubt discovered, hooking into the scroll events properly is a pain:

So let's extend the manager. We'll create a few parameters that you might expose.

 public class ZoomCenterCardLayoutManager extends LinearLayoutManager {
   // Shrink the cards around the center up to 50%
   private final float mShrinkAmount = 0.5f;
   // The cards will be at 50% when they are 75% of the way between the
   // center and the edge.
   private final float mShrinkDistance = 0.75f;

Fill out your constructors, and then override scrollHorizontallyBy:

   @Override 
   public int scrollHorizontallyBy(int dx, 
      RecyclerView.Recycler recycler, RecyclerView.State state) {

Call the parent's version and save the distance travelled. We'll need to return this at the end of the method:

      int scrolled = super.scrollHorizontallyBy(dx, recycler, state);

We are going to set up a simple linear interpolation. It looks nice enough.

      float midpoint = getWidth() / 2.f;
      float d0 = 0.f;
      float d1 = mShrinkDistance * midpoint;
      float s0 = 1.f;
      float s1 = 1.f - mShrinkAmount;

Loop through all of the active children of the control, run the interpolation, and set the scale of the child.

      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;
   }

This is almost all you need. One final step is to make sure that this adjustment is called after initialization -- otherwise the zooming won't take effect until the first time the control is moved:

   @Override
   public void onLayoutChildren(Recycler recycler, State state) {
     super.onLayoutChildren(recycler, state);
     scrollHorizontallyBy(0, recycler, state);
   }

 }

And that's all there is to it. Super responsive, and you can drop this new layout manager into any horizontal recycler.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...