缩放中央图像回收站视图

时间:2016-02-10 08:03:48

标签: android zoom android-recyclerview carousel

我有带有图像的RecyclerView。它基于this solution。使用Glide将图像延迟加载到视图中。我需要在中央图像上添加缩放,如下所示: example

我该怎么做?

1 个答案:

答案 0 :(得分:33)

影响你想要的最直接的方法是扩展LinearLayoutManager。 正如你无疑发现的那样,正确地挂钩滚动事件是一种痛苦:

所以让我们扩展经理。我们将创建一些您可能会公开的参数。

 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;

填写构造函数,然后覆盖scrollHorizontallyBy

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

调用父版本并保存行进距离。我们需要在方法结束时返回:

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

这几乎就是你所需要的。最后一步是确保在初始化后调用此调整 - 否则直到第一次移动控件时缩放才会生效:

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

 }

这就是它的全部内容。超级响应,您可以将这个新的布局管理器放入任何水平回收站。