挑战:ViewPager的自定义动画。更改所选元素的高度(查看折叠)

时间:2013-06-05 06:39:34

标签: java android animation layout android-viewpager

我目前正在研究ViewPager中切换页面之间的自定义动画。当我向左滑动时,View向左移动,从下方移动,新的View将出现在前面。我想制作一个视图,向左移动(我处理)缩小,如下图所示:

enter image description here enter image description here enter image description here

在第二张和第三张图片上,我没有想到新的View出现在前面,但我认为没有必要。您是否知道如何修改代码?

我想更改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);
        }
    }
}

更新:

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

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

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

2 个答案:

答案 0 :(得分:5)

尝试使用位置浮点变量。 获得布局的高度和......:

relativeLayout.setLayoutParams(....,position*height+height)

答案 1 :(得分: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.*);