缩放中央图像回收站视图

And*_*kov 7 android zoom carousel android-recyclerview

我有RecyclerView图像.它基于这个解决方案.使用Glide将图像延迟加载到视图中.我需要在中央图像上添加缩放,如下所示: 例

我该怎么做?

Mic*_*ays 39

影响你想要的最直接的方法是扩展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;
Run Code Online (Sandbox Code Playgroud)

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

   @Override 
   public int scrollHorizontallyBy(int dx, 
      RecyclerView.Recycler recycler, RecyclerView.State state) {
Run Code Online (Sandbox Code Playgroud)

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

      int scrolled = super.scrollHorizontallyBy(dx, recycler, state);
Run Code Online (Sandbox Code Playgroud)

我们将设置一个简单的线性插值.它看起来不错.

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

循环遍历控件的所有活动子项,运行插值,并设置子项的比例.

      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;
   }
Run Code Online (Sandbox Code Playgroud)

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

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

 }
Run Code Online (Sandbox Code Playgroud)

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

  • 谢谢你的澄清.在这里找到了另一个解决方案:http://stackoverflow.com/a/30041459/5571200 (2认同)
  • 我对发送给你的方向不满意.我改变了方法直接覆盖布局管理器.我知道我说这是一种痛苦,但我正在度假.一旦我回到家,我就快速编码,以确保它看起来不错.它比我想象的要容易,而且因为我实际上可以测试它,所以我觉得发布实际代码更有信心.我不打扰滚动钩子. (2认同)
  • 使用这个:https://github.com/yarolegovich/DiscreteScrollView。这很棒 (2认同)