Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android animation flip cover and ease up (open book animation)

I have few buttons/images there. on clicking I would like such animation :

(image opens like )book cover opens, and related activity/fragment opens with ease in animation and gets full screen.

Any thoughts ?

Something similar happening in cook app iOS, Link of cook app : https://itunes.apple.com/us/app/cook/id687560846?mt=8

P.S : I have added animation, gif will run uninterrupted once completely loaded.

animation

like image 373
Haris Avatar asked Aug 18 '15 08:08

Haris


People also ask

What is the difference between ValueAnimator and ObjectAnimator?

ObjectAnimator is a subclass of ValueAnimator. Main difference is that in case of ValueAnimator you have to override onAnimationUpdate(...) method where you will specify where to apply animated value: ValueAnimator animator = ValueAnimator.


1 Answers

You can implement this animation using standard android animator with animating book as a set of multiple ImageViews - each for page representation: Cover, BackCover, FirstPage. And after animation is finished launch activity or display fragment.

Animate with:

  • x and y scale, ObjectAnimator.ofFloat(mCover, "scaleY", ...)
  • x and y transform, ObjectAnimator.ofFloat(mCover, "x", ...)
  • y-rotation ObjectAnimator.ofFloat(mCover, "rotationY", ...)

See my example

(Sure, this sample requires some optimizations/fixes, but for better understanding would be enough):

public class MainActivity extends AppCompatActivity {

    private ImageView mPage1;
    private ImageView mCover;
    private ImageView mCoverFullScreen;

    private AnimatorSet mAnimIncrease;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mCoverFullScreen = (ImageView) findViewById(R.id.cover_full_screen);
        mPage1 = (ImageView) findViewById(R.id.page1);
        mCover = (ImageView) findViewById(R.id.cover);
        mCover.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mAnimIncrease.start();
            }
        });

        mCoverFullScreen.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                if (mCover.getMeasuredHeight() <= 0 || mCoverFullScreen.getMeasuredHeight() <= 0) {
                    return;
                }
                if (Build.VERSION.SDK_INT >= 16) {
                    mCoverFullScreen.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                } else {
                    mCoverFullScreen.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                }

                initAnimator(1000);
            }
        });
    }

    private void initAnimator(long animationDuration) {

        mAnimIncrease = new AnimatorSet();
        TimeInterpolator interpolator = new LinearInterpolator();

        float deltaX = mCover.getMeasuredWidth() / 2f;
        float deltaY = mCoverFullScreen.getY() - mCover.getY();
        float scale = mCoverFullScreen.getMeasuredHeight() / (float) mCover.getMeasuredHeight();
        float scaleMiddle = (scale + 1) / 2f;

        float xStart = mCover.getX();
        float xEnd = xStart + deltaX;
        float xMiddle = xStart + deltaX * interpolator.getInterpolation(0.5f);
        float xScaledEnd = xStart + deltaX * scale;

        float yStart = mCover.getY();
        float yEnd = yStart + deltaY;
        float yMiddle = yStart + deltaY * interpolator.getInterpolation(0.5f);

        AnimatorSet coverFrontSet = new AnimatorSet();
        coverFrontSet.setDuration(animationDuration / 2);
        coverFrontSet.playTogether(
                ObjectAnimator.ofFloat(mCover, "rotationY", 0f, -90f),
                ObjectAnimator.ofFloat(mCover, "pivotX", 0f),
                ObjectAnimator.ofFloat(mCover, "x", xStart, xMiddle),
                ObjectAnimator.ofFloat(mCover, "y", yStart, yMiddle),
                ObjectAnimator.ofFloat(mCover, "scaleY", 1, scaleMiddle),
                ObjectAnimator.ofFloat(mCover, "scaleX", 1, scaleMiddle)
        );
        coverFrontSet.addListener(new AnimatorListenerStub() {
            @Override
            public void onAnimationStart(Animator animation) {
                super.onAnimationStart(animation);
                mCover.setImageResource(R.drawable.cover);
            }

            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                mCover.setImageResource(R.drawable.cover_back);
            }
        });

        AnimatorSet coverBackSet = new AnimatorSet();
        coverBackSet.setDuration(animationDuration / 2);
        coverBackSet.playTogether(
                ObjectAnimator.ofFloat(mCover, "rotationY", -90f, -180f),
                ObjectAnimator.ofFloat(mCover, "pivotX", 0f),
                ObjectAnimator.ofFloat(mCover, "x", xMiddle, xEnd),
                ObjectAnimator.ofFloat(mCover, "y", yMiddle, yEnd),
                ObjectAnimator.ofFloat(mCover, "scaleY", scaleMiddle, scale),
                ObjectAnimator.ofFloat(mCover, "scaleX", scaleMiddle, scale)
        );

        AnimatorSet coverSet = new AnimatorSet();
        coverSet.play(coverBackSet).after(coverFrontSet);

        AnimatorSet page1Set = new AnimatorSet();
        page1Set.setDuration(animationDuration);
        page1Set.playTogether(
                ObjectAnimator.ofFloat(mPage1, "scaleX", 1, scale),
                ObjectAnimator.ofFloat(mPage1, "scaleY", 1, scale),
                ObjectAnimator.ofFloat(mPage1, "x", xStart, xScaledEnd)
        );

        mAnimIncrease.play(coverSet).with(page1Set);
        mAnimIncrease.setInterpolator(interpolator);
    }
}

And Layout:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:tools="http://schemas.android.com/tools"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                tools:context=".MainActivity"
    >

    <ImageView
        android:id="@+id/page1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:src="@drawable/page1"
        />

    <ImageView
        android:id="@+id/cover"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:src="@drawable/cover"
        />

    <ImageView
        android:id="@+id/cover_full_screen"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />
</RelativeLayout>

Cover book animation example

like image 115
Oleksandr Avatar answered Sep 19 '22 03:09

Oleksandr