Skip to content
This repository has been archived by the owner on Dec 8, 2020. It is now read-only.

大于3个item时 放大的item左右间距与其他item左右间距不一样 - ScaleLayoutManager #109

Open
TonyStonem opened this issue Sep 7, 2019 · 1 comment

Comments

@TonyStonem
Copy link

大于3个item时 放大的item左右间距与其他item左右间距不一样 - ScaleLayoutManager。这该如何解决

@trycatchx
Copy link

trycatchx commented May 9, 2020

一个屏幕现在在 5个 ITEM 以内(包含五个),可以用下面方式解决 间距不一样的问题

ScaleLayoutManager.java

    @Override
    protected void setItemViewProperty(View itemView, float targetOffset) {
        float scale = calculateScale(targetOffset + mSpaceMain);
        float translationX = calTranslationX(targetOffset); //加入计算位移
        itemView.setScaleX(scale);
        itemView.setScaleY(scale);
        itemView.setTranslationX(translationX); //加入这行,这行一定要放在 setScaleX 之后
        final float alpha = calAlpha(targetOffset);
        if(itemView.getBackground() != null) {
            itemView.getBackground().setAlpha((int)(alpha * 255));
        }
    }

    /**
     * 解决 左2 和 左 1 两倍间距问题
     * @param tmp
     * @return
     */
    private float calTranslationX(float tmp) {
        //向自己 View中间聚拢缩小后产生的间距的一半
        final int halfOfItemSpace = (int)(mInterval * (1f- minScale) / 2f) ;
        //取绝对值
        float targetOffset = Math.abs(tmp);
        int position = (int) (targetOffset / mInterval);
        float mod = targetOffset % mInterval;
        float ret;
        if (position == 0) {
            //position = 0 ,居中
            ret = 0;
        } else if (position == 1) {
             // 1.0-2.0 按照滑动的 offset 算出比例,求出 translationX
            if (tmp > 0) {
                float ratio = mod / mInterval;
                ret = - ratio * halfOfItemSpace;
            } else {
                float ratio = mod / mInterval;
                ret = ratio * halfOfItemSpace;
            }
        } else {
            //position > 2 ,直接靠边
            if (tmp > 0) {
                ret = - halfOfItemSpace;
            } else {
                ret = + halfOfItemSpace;
            }

        }
        return ret;
    }

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants