8

我目前正在处理 ViewPager 中切换页面之间的自定义动画。当我向左滑动时,视图向左移动,新视图从下方移到前面。我想让视图向左移动(我处理)以缩小,如下图所示:

在此处输入图像描述在此处输入图像描述在此处输入图像描述

在第二张和第三张图片中,我没有看到新视图出现在前面,但我认为没有必要。您知道如何修改代码吗?

我想改变 TableLayout、RelativeLayout 和 FrameLayout 的高度并保持两个 TextView 的高度。此外,我将不得不更改整个视图的 X 位置。

我期待您的创造性答案(代码)。下面我附上我的动画代码。

import android.view.View;
import android.widget.RelativeLayout;
import android.annotation.SuppressLint;
import android.support.v4.view.ViewPager.PageTransformer;

public class DepthPageTransformer implements PageTransformer {
    private static float MIN_SCALE = 0.75f;

    @SuppressLint("NewApi")
    public void transformPage(View view, float position) {
        int pageWidth = view.getWidth();

        if (position < -1) { // [-Infinity,-1)
            // This page is way off-screen to the left.
            view.setAlpha(0);

        } else if (position <= 0) { // [-1,0]
            // Use the default slide transition when moving to the left page
            view.setTranslationX(0);
            view.setScaleX(1);
            view.setScaleY(1);
            //float scaleFactor = (1 - Math.abs(position));

            //WHAT TO PUT HERE TO ARCHIVE MY GOAL??

            //This is how I can get particular layouts:
            // RelativeLayout relativeLayout = (RelativeLayout) view.findViewById(R.id.fragment_object_canvas_RelativeLayout1);
            //relativeLayout.setScaleX(scaleFactor);
            //relativeLayout.setScaleY(scaleFactor);

        } else if (position <= 1) { // (0,1]
            // Fade the page out.
            view.setAlpha(1 - position);

            // Counteract the default slide transition
            view.setTranslationX(pageWidth * -position);
            //view.setRotation(1 - (position*360));

            // Scale the page down (between MIN_SCALE and 1)
            float scaleFactor = MIN_SCALE
                    + (1 - MIN_SCALE) * (1 - Math.abs(position));
            view.setScaleX(scaleFactor);
            view.setScaleY(scaleFactor);

        } else { // (1,+Infinity]
            // This page is way off-screen to the right.
            view.setAlpha(0);
        }
    }
}

更新:

我可以使用以下代码操作整个视图的高度(现在我只能这样做):

  LinearLayout relativeLayout = (LinearLayout) view.findViewById(R.id.fragment_object_canvas_linearLayout1);
            relativeLayout.setLayoutParams(new FrameLayout.LayoutParams(relativeLayout.getWidth(), NEW_HEIGHT_HERE)));

但是,我不确定应该将什么放入 NEW_HEIGHT_HERE 变量以使其正常工作...

4

2 回答 2

5

try to work with a position float variable. Get the height of your layout and...:

relativeLayout.setLayoutParams(....,position*height+height)
于 2013-06-12T06:08:25.053 回答
1

您似乎非常坚持人们发布原始代码,但为什么要重新发明轮子?这个库有很多很棒的 ViewPager 动画,包括一个与您似乎要求的非常相似的动画。

https://github.com/jfeinstein10/JazzyViewPager

JazzyViewPager - 一个易于使用的 ViewPager,它添加了一组很棒的自定义滑动动画。只需将您的 ViewPagers 更改为 JazzyViewPagers 就可以了!


public enum TransitionEffect {
        Standard,
        Tablet,
        CubeIn,
        CubeOut,
        Flip,
        Stack,
        ZoomIn,
        ZoomOut,
        RotateUp,
        RotateDown,
        Accordion
    }

您可以通过调用选择动画

private JazzyViewPager mJazzy;
/* ... */
mJazzy.setTransitionEffect(TransitionEffect.*);
于 2013-06-14T00:14:50.700 回答