Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Challenge: Custom Animation of ViewPager. Change height of chosen elements (View fold)

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:

enter image description hereenter image description hereenter image description here

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...

like image 762
Marek Avatar asked Jun 05 '13 06:06

Marek


People also ask

How do you wrap the height of a ViewPager to the height of its current fragment?

To solve this, rather than calling viewPager. setOffscreenPageLimit(2) , I simply replaced View child = getChildAt(getCurrentItem()); in the above code with Fragment childFragment = (Fragment)getAdapter(). instantiateItem(this, getCurrentItem()); View child = childFragment.

How many methods does the PageTransformer of a ViewPager expose?

PageTransformer interface and supply it to the view pager. The interface exposes a single method, transformPage() . At each point in the screen's transition, this method is called once for each visible page (generally there's only one visible page) and for adjacent pages just off the screen.

How does ViewPager2 work?

ViewPager2 supports paging through a modifiable collection of fragments, calling notifyDatasetChanged() to update the UI when the underlying collection changes. This means that your app can dynamically modify the fragment collection at runtime, and ViewPager2 will correctly display the modified collection.

What is a view pager?

ViewPager in Android allows the user to flip left and right through pages of data. In our android ViewPager application we'll implement a ViewPager that swipes through three views with different images and texts.


2 Answers

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

relativeLayout.setLayoutParams(....,position*height+height)
like image 55
Alex Avatar answered Sep 24 '22 03:09

Alex


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.*);
like image 28
codepringle Avatar answered Sep 22 '22 03:09

codepringle