这个方法对我有用,我解决的问题是在创建RecyclerView的时候,秤上的item并没有应用到它们上,顺便说一下,我使用的数字是根据我的需要,而你根据需要自己调整比例
public class CenterZoomLayoutManager extends LinearLayoutManager {
private final float mShrinkAmount = 0.25f;
private final float mShrinkDistance = 2.0f;
public CenterZoomLayoutManager(Context context) {
super(context);
}
public CenterZoomLayoutManager(Context context, int orientation, boolean reverseLayout) {
super(context, orientation, reverseLayout);
}
public CenterZoomLayoutManager(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
@Override
public void onLayoutCompleted(RecyclerView.State state) {
super.onLayoutCompleted(state);
scaleChild();
}
@Override
public int scrollHorizontallyBy(int dx, RecyclerView.Recycler recycler, RecyclerView.State state) {
int orientation = getOrientation();
if (orientation == HORIZONTAL) {
scaleChild();
return super.scrollHorizontallyBy(dx, recycler, state);
} else {
return 0;
}
}
private void scaleChild() {
float midPoint = getWidth() / 2.f;
float d1 = mShrinkDistance * midPoint;
for (int i = 0; i < getChildCount(); i++) {
View child = getChildAt(i);
float childMidPoint = (getDecoratedRight(child) + getDecoratedLeft(child)) / 2f;
float d = Math.min(d1, Math.abs(midPoint - childMidPoint));
float scale = 1.05f - mShrinkAmount * d / d1;
child.setScaleY(scale);
child.setScaleX(scale);
}
}
}
一定要调用onLayoutCompleted中的scaleChild方法。在创建 RecyclerView 布局时将比例应用于项目。不只是在滚动时。这是很重要的一点
@Override
public void onLayoutCompleted(RecyclerView.State state) {
super.onLayoutCompleted(state);
scaleChild();
}
下一个重点是在 returning super.scrollHorizontallyBy (dx, recycler, state) 之前使用 scaleChild() 方法。称呼。直到滚动完成。每次都在项目上完成比例
@Override
public int scrollHorizontallyBy(int dx, RecyclerView.Recycler recycler, RecyclerView.State state) {
int orientation = getOrientation();
if (orientation == HORIZONTAL) {
scaleChild();
return super.scrollHorizontallyBy(dx, recycler, state);
} else {
return 0;
}
}
最终,这成为了我的程序输出