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)
这就是它的全部内容.超级响应,您可以将这个新的布局管理器放入任何水平回收器.
归档时间: |
|
查看次数: |
7984 次 |
最近记录: |