缩放中央图像回收器视图 [英] Zoom central image recycler view

查看:66
本文介绍了缩放中央图像回收器视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有带有图像的RecyclerView.它基于此解决方案.使用Glide将图像延迟加载到视图中.我需要像这样在中央图像上添加缩放:

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:

我该怎么办?

推荐答案

影响您想要的内容的最直接方法是扩展LinearLayoutManager. 毫无疑问,正确地陷入滚动事件是一种痛苦:

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;

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

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);
     scrollVerticallyBy(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.

这篇关于缩放中央图像回收器视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆