Home » Android » java – How to achieve flip animation android (GIF Added)-Exceptionshub

java – How to achieve flip animation android (GIF Added)-Exceptionshub

Posted by: admin February 26, 2020 Leave a comment

Questions:

Edited: Adding the XML layout of my view (How to achieve flip animation android (GIF Added))

TopLayout : ivImage
BottomLayout : layoutL

I want to flip the screen between these Layouts From TOP & Bottom

<androidx.cardview.widget.CardView
            app:cardCornerRadius="6dp"
            app:cardPreventCornerOverlap="true"
            app:cardUseCompatPadding="true"
            app:cardBackgroundColor="@color/White"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <ImageView
                android:id="@+id/ivImage"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:contentDescription="@string/activity_images"
                android:scaleType="fitXY" />

            <LinearLayout
                android:id="@+id/layoutL"
                android:layout_weight="5.5"
                android:layout_marginStart="10dp"
                android:layout_marginEnd="10dp"
                android:layout_marginTop="8dp"
                android:orientation="vertical"
                android:layout_width="match_parent"
                android:layout_height="0dp">

                <TextView
                    android:id="@+id/tvTTitle"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:gravity="center_vertical"
                    android:padding="4dp"
                    android:textStyle="bold"
                    android:text="@string/Topic_Title"
                    android:textColor="@color/Black"
                    android:textSize="20sp" />

                <TextView
                    android:id="@+id/postDetails"
                    android:textAllCaps="false"
                    android:text="text"
                    android:textSize="12sp"
                    android:layout_marginStart="4dp"
                    android:textStyle="normal"
                    android:textColor="@color/Silver"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"/>

                <TextView
                    android:id="@+id/tvTopicDesc"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:padding="4dp"
                    android:lineSpacingExtra="2dp"
                    android:text="@string/invalid_email_contactno"
                    android:textAllCaps="false"
                    android:textColor="#6D6D6D"
                    android:textSize="15sp" />

            </LinearLayout>

        </androidx.cardview.widget.CardView>

I want to create a layout like a given GIF, I tried so many animations but not worked perfectly for me.

How to&Answers:

Try below solution and set yourSecondView as gone in Xml.

yourButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        final ObjectAnimator oa1 = ObjectAnimator.ofFloat(yourFirstView, "scaleY", 1f, 0f);
        final ObjectAnimator oa2 = ObjectAnimator.ofFloat(yourSecondView, "scaleY", 0f, 1f);
        oa1.setInterpolator(new DecelerateInterpolator());
        oa1.setDuration(1000);
        oa2.setInterpolator(new AccelerateDecelerateInterpolator());
        oa2.setDuration(1000);
        oa1.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                yourFirstView.setVisibility(View.GONE);
                yourSecondView.setVisibility(View.VISIBLE);
                oa2.start();
            }
        });
        oa1.start();
    }
});

Output

enter image description here

I hope this can help you!