Challenge: Custom Animation of ViewPager. Change h

2019-04-06 12:42发布

问题:

I am currently working on custom animation between switching pages in ViewPager. When I slide to the left, View is moving to the left and from below of it new View is coming to the front. I would like to make the view, that moves to the left (that I dispose) to shrink like on the folowing images:

On the second and third image I didn't picture new View coming to the front, but I think it wasn't necessary. Do you have any idea how can I modify the code?

I would like to change height of TableLayout, RelativeLayout and FrameLayout and keep the height of both TextViews. Also I would have to change X-position of the whole View.

I am looking forward for your creative answers (code). Below I attach my code for the animation.

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);
        }
    }
}

UPDATE:

I can manipulate with height of whole View (for now I can do only this) with the following code:

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

However, I am not sure what should I put into NEW_HEIGHT_HERE variable to make it work corectly...

回答1:

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

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


回答2:

You seem pretty insistent on people posting original code, but why reinvent the wheel? This library has a ton of great animations for a ViewPager, including one very similar to what you seem to be requesting.

https://github.com/jfeinstein10/JazzyViewPager

JazzyViewPager - An easy to use ViewPager that adds an awesome set of custom swiping animations. Just change your ViewPagers to JazzyViewPagers and you're good to go!


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

You can select your animation by calling

private JazzyViewPager mJazzy;
/* ... */
mJazzy.setTransitionEffect(TransitionEffect.*);