Displaying Card Flip Animations display Card Flip Animation

Source: Internet
Author: User

If you want to jump ahead and see a full working example, download and run the sample app and select the Card Flip example. See the following files for the code implementation:

Src/CardFlipActivity. java
Animator/card_flip_right_in.xml
Animator/card_flip_right_out.xml
Animator/card_flip_right_in.xml
Animator/card_flip_left_out.xml
Layout/fragment_card_back.xml
Layout/fragment_card_front.xml
Http://blog.csdn.net/sergeycao

Create the Animators
Create the animations for the card flips. you'll need two animators for when the front of the card animates out and to the left and in and from the left. you'll also need two animators for when the back of the card animates in and from the right and out and to the right.

Card_flip_left_in.xml
<Set xmlns: android = "http://schemas.android.com/apk/res/android">
<! -- Before rotating, immediately set the alpha to 0. -->
<ObjectAnimator
Android: valueFrom = "1.0"
Android: valueTo = "0.0"
Android: propertyName = "alpha"
Android: duration = "0"/>

<! -- Rotate. -->
<ObjectAnimator
Android: valueFrom = "-180"
Android: valueTo = "0"
Android: propertyName = "rotationY"
Android: interpolator = "@ android: interpolator/accelerate_decelerate"
Android: duration = "@ integer/card_flip_time_full"/>

<! -- Half-way through the rotation (see startOffset), set the alpha to 1. -->
<ObjectAnimator
Android: valueFrom = "0.0"
Android: valueTo = "1.0"
Android: propertyName = "alpha"
Android: startOffset = "@ integer/card_flip_time_half"
Android: duration = "1"/>
</Set>
Card_flip_left_out.xml
<Set xmlns: android = "http://schemas.android.com/apk/res/android">
<! -- Rotate. -->
<ObjectAnimator
Android: valueFrom = "0"
Android: valueTo = "180"
Android: propertyName = "rotationY"
Android: interpolator = "@ android: interpolator/accelerate_decelerate"
Android: duration = "@ integer/card_flip_time_full"/>

<! -- Half-way through the rotation (see startOffset), set the alpha to 0. -->
<ObjectAnimator
Android: valueFrom = "1.0"
Android: valueTo = "0.0"
Android: propertyName = "alpha"
Android: startOffset = "@ integer/card_flip_time_half"
Android: duration = "1"/>
</Set>
Card_flip_right_in.xml
<Set xmlns: android = "http://schemas.android.com/apk/res/android">
<! -- Before rotating, immediately set the alpha to 0. -->
<ObjectAnimator
Android: valueFrom = "1.0"
Android: valueTo = "0.0"
Android: propertyName = "alpha"
Android: duration = "0"/>

<! -- Rotate. -->
<ObjectAnimator
Android: valueFrom = "180"
Android: valueTo = "0"
Android: propertyName = "rotationY"
Android: interpolator = "@ android: interpolator/accelerate_decelerate"
Android: duration = "@ integer/card_flip_time_full"/>

<! -- Half-way through the rotation (see startOffset), set the alpha to 1. -->
<ObjectAnimator
Android: valueFrom = "0.0"
Android: valueTo = "1.0"
Android: propertyName = "alpha"
Android: startOffset = "@ integer/card_flip_time_half"
Android: duration = "1"/>


Card_flip_right_out.xml
<Set xmlns: android = "http://schemas.android.com/apk/res/android">
<! -- Rotate. -->
<ObjectAnimator
Android: valueFrom = "0"
Android: valueTo = "-180"
Android: propertyName = "rotationY"
Android: interpolator = "@ android: interpolator/accelerate_decelerate"
Android: duration = "@ integer/card_flip_time_full"/>

<! -- Half-way through the rotation (see startOffset), set the alpha to 0. -->
<ObjectAnimator
Android: valueFrom = "1.0"
Android: valueTo = "0.0"
Android: propertyName = "alpha"
Android: startOffset = "@ integer/card_flip_time_half"
Android: duration = "1"/>
</Set>
Create the Views
Each side of the "card" is a separate layout that can contain in any content you want, such as two screens of text, two images, or any combination of views to flip. you'll then use the two layouts in the fragments that you'll later animate. the following layouts create one side of a card that shows text:

<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "match_parent"
Android: layout_height = "match_parent"
Android: orientation = "vertical"
Android: background = "# a6c"
Android: padding = "16dp"
Android: gravity = "bottom">

<TextView android: id = "@ android: id/text1"
Style = "? Android: textAppearanceLarge"
Android: textStyle = "bold"
Android: textColor = "# fff"
Android: layout_width = "match_parent"
Android: layout_height = "wrap_content"
Android: text = "@ string/card_back_title"/>

<TextView style = "? Android: textAppearanceSmall"
Android: textAllCaps = "true"
Android: textColor = "#80 ffffff"
Android: textStyle = "bold"
Android: lineSpacingMultiplier = "1.2"
Android: layout_width = "match_parent"
Android: layout_height = "wrap_content"
Android: text = "@ string/card_back_description"/>

</LinearLayout>
And the other side of the card that displays an ImageView:

<ImageView xmlns: android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "match_parent"
Android: layout_height = "match_parent"
Android: src = "@ drawable/image1"
Android: scaleType = "centerCrop"
Android: contentDescription = "@ string/description_image_1"/>
Create the Fragment
Create fragment classes for the front and back of the card. these classes return the layouts that you created previusly in the onCreateView () method of each fragment. you can then create instances of this fragment in the parent activity where you want to show the card. the following example shows nested fragment classes inside of the parent activity that uses them:

Public class CardFlipActivity extends Activity {
...
/**
* A fragment representing the front of the card.
*/
Public class CardFrontFragment extends Fragment {
@ Override
Public View onCreateView (LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState ){
Return inflater. inflate (R. layout. fragment_card_front, container, false );
}
}

/**
* A fragment representing the back of the card.
*/
Public class CardBackFragment extends Fragment {
@ Override
Public View onCreateView (LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState ){
Return inflater. inflate (R. layout. fragment_card_back, container, false );
}
}
}
Animate the Card Flip
Now, you'll need to display the fragments inside of a parent activity. to do this, first create the layout for your activity. the following example creates a FrameLayout that you can add fragments to at runtime:

<FrameLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: id = "@ + id/container"
Android: layout_width = "match_parent"
Android: layout_height = "match_parent"/>
In the activity code, set the content view to be the layout that you just created. it's also good idea to show a default fragment when the activity is created, so the following example activity shows you how to display the front of the card by default:

Public class CardFlipActivity extends Activity {

@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. activity_activity_card_flip );

If (savedInstanceState = null ){
GetFragmentManager ()
. BeginTransaction ()
. Add (R. id. container, new CardFrontFragment ())
. Commit ();
}
}
...
}
Now that you have the front of the card showing, you can show the back of the card with the flip animation at an appropriate time. create a method to show the other side of the card that does the following things:

Sets the custom animations that you created earlier for the fragment transitions.
Replaces the currently displayed fragment with a new fragment and animates this event with the custom animations that you created.
Adds the previusly displayed fragment to the fragment back stack so when the user presses the Back button, the card flips back over.
Private void flipCard (){
If (mShowingBack ){
GetFragmentManager (). popBackStack ();
Return;
}

// Flip to the back.

MShowingBack = true;

// Create and commit a new fragment transaction that adds the fragment for the back
// The card, uses custom animations, and is part of the fragment manager's back stack.

GetFragmentManager ()
. BeginTransaction ()

// Replace the default fragment animations with animator resources representing
// Rotations when switching to the back of the card, as well as animator
// Resources representing rotations when flipping back to the front (e.g. when
// The system Back button is pressed ).
. SetCustomAnimations (
R. animator. card_flip_right_in, R. animator. card_flip_right_out,
R. animator. card_flip_left_in, R. animator. card_flip_left_out)

// Replace any fragments currently in the container view with a fragment
// Representing the next page (indicated by the just-incremented currentPage
// Variable ).
. Replace (R. id. container, new CardBackFragment ())

// Add this transaction to the back stack, allowing users to press Back
// To get to the front of the card.
. AddToBackStack (null)

// Commit the transaction.
. Commit ();
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.