leochuan / ViewPagerLayoutManager

ViewPager like LayoutManager which supports some awesome animations and infinite scroll.
Apache License 2.0
1.75k stars 333 forks source link

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

Open TonyStonem opened 5 years ago

TonyStonem commented 5 years ago

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

trycatchx commented 4 years ago

一个屏幕现在在 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;
    }