具有CubeOutTransformation和视差效果的ViewPager

时间:2018-09-19 09:36:08

标签: android android-fragments android-viewpager parallax

我有带分页的viewpager。每个片段都包含具有视差效果的图像的RecyclerView。 viewpager还实现了CubeOutTransformation。在viewpager滚动过程中,多维数据集输出转换的唯一左片段被更新。您可以在this imagethis gif上看到我在说什么。您也可以从此link克隆项目,并自己查看代码。左右滑动时,它以某种方式表现出不同的表现(我认为它应该以一种方式表现,左右视差图像视图都必须更新)。如何解决此问题?或任何自行解决的提示。 视差图像的代码摘自以下lib

public class ScrollParallaxImageView extends ImageView implements ViewTreeObserver.OnScrollChangedListener {
    private int[] viewLocation = new int[2];
    private boolean enableScrollParallax = true;

    private ParallaxStyle parallaxStyles;

    public ScrollParallaxImageView(Context context) {
        this(context, null);
    }

    public ScrollParallaxImageView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ScrollParallaxImageView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        if (!enableScrollParallax || getDrawable() == null) {
            super.onDraw(canvas);
            return;
        }

        if (parallaxStyles != null){
            getLocationInWindow(viewLocation);
            parallaxStyles.transform(this, canvas, viewLocation[0], viewLocation[1]);
        }

        super.onDraw(canvas);
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        getViewTreeObserver().addOnScrollChangedListener(this);
    }

    @Override
    protected void onDetachedFromWindow() {
        getViewTreeObserver().removeOnScrollChangedListener(this);
        super.onDetachedFromWindow();
    }

    @Override
    public void onScrollChanged() {
        if (enableScrollParallax) {
            invalidate();
        }
    }

    public void setParallaxStyles(ParallaxStyle styles) {
        if (parallaxStyles != null) {
            parallaxStyles.onDetachedFromImageView(this);
        }
        parallaxStyles = styles;
        parallaxStyles.onAttachedToImageView(this);
    }

    public void setEnableScrollParallax(boolean enableScrollParallax) {
        this.enableScrollParallax = enableScrollParallax;
    }

    public interface ParallaxStyle {
        void onAttachedToImageView(ScrollParallaxImageView view);
        void onDetachedFromImageView(ScrollParallaxImageView view);
        void transform(ScrollParallaxImageView view, Canvas canvas, int x, int y);
    }
}

视差转换在此类中发生:

public class VerticalMovingStyle implements ScrollParallaxImageView.ParallaxStyle {

    @Override
    public void onAttachedToImageView(ScrollParallaxImageView view) {
        // only supports CENTER_CROP
        view.setScaleType(ImageView.ScaleType.CENTER_CROP);
    }

    @Override
    public void onDetachedFromImageView(ScrollParallaxImageView view) {

    }

    @Override
    public void transform(ScrollParallaxImageView view, Canvas canvas, int x, int y) {
        if (view.getScaleType() != ImageView.ScaleType.CENTER_CROP) {
            return;
        }

        // image's width and height
        int iWidth = view.getDrawable().getIntrinsicWidth();
        int iHeight = view.getDrawable().getIntrinsicHeight();
        if (iWidth <= 0 || iHeight <= 0) {
            return;
        }

        // view's width and height
        int vWidth = view.getWidth() - view.getPaddingLeft() - view.getPaddingRight();
        int vHeight = view.getHeight() - view.getPaddingTop() - view.getPaddingBottom();

        // device's height
        int dHeight = view.getResources().getDisplayMetrics().heightPixels;

        if (iWidth * vHeight < iHeight * vWidth) {
            // avoid over scroll
            if (y < -vHeight) {
                y = -vHeight;
            } else if (y > dHeight) {
                y = dHeight;
            }

            float imgScale = (float) vWidth / (float) iWidth;
            float max_dy = Math.abs((iHeight * imgScale - vHeight) * 0.5f);
            float translateY = -(2 * max_dy * y + max_dy * (vHeight - dHeight)) / (vHeight + dHeight);
            canvas.translate(0, translateY);
        }
    }
}

0 个答案:

没有答案