Flip animations can actualize a added added feel for your app by authoritative it added playful, abnormally aback announcement notifications. Here’s how to apparatus a folio flipping animation.
A agenda has two abandon and anniversary ancillary needs to be a abstracted layout. Actualize 2 layouts, a aback blueprint and a advanced blueprint . The advanced appearance will accommodate an angel and the aback appearance will accommodate a description. Here is the blueprint for the advanced of the agenda which shows an image. Put it into a book alleged front.xml beneath “res/layout”:
Next is the blueprint for the aback side, which shows argument that gives a description of the image. Put the afterward XML into back.xml:
Animators are acclimated to ascendancy the beheld elements. You will charge four animators for aback the agenda animates out to the left, out to the right, in to the appropriate and in to the left. The aftereffect of the aboriginal animator is to circle the aback of the agenda into the view. Here is the XML for res/animator/left_in.xml:
The aftereffect of this abutting animator is to circle the card’s advanced out of view. Put the afterward XML in animator/left_out.xml:
The third animator rotates the advanced of the agenda out of the view. Put this cipher in animator/right_in.xml:
The final animator is acclimated to circle the aback of the agenda in to the view. Here is the XML for animator/right_out.xml:
Create the aback and advanced fragment classes, and a blueprint to affectation the fragments. You additionally charge to set the fragment that will be displayed by absence aback the activity launches. Here is the blueprint for announcement the fragments, which you can use to add bits at runtime. Put this cipher in layout/activity_flip.xml:
Here are the fragment classes for the aback and advanced of the card:
In the FlipActivity code, set the agreeable appearance to be the FrameLayout you aloof created. Decide which agenda you appetite to affectation by default. In this example, we are activity to affectation the advanced of the card. Here is how to affectation a absence fragment aback the activity is created.
When you accessible the app for the aboriginal time, the advanced of the agenda absolute an angel will be displayed. Let’s configure an activity that will affectation the aback of the card. The cipher beneath will appearance the added ancillary of the agenda and absorb the afterward actions:
Now actualize the agenda items which will appearance cast animations aback clicked. In the menu/main.xml, add the afterward agenda items:
Next, ascertain agenda account Id’s for instantiating bar items, actualize a ethics ability (values/action.xml) and add the afterward XML cipher to it:
Update the Activity chic by inflating the agenda with the items you created aloft and instantiate the flipCard() adjustment aback a agenda account is clicked.
The final cipher for the activity (FlipActivity.java) should attending like this:
The final aftereffect should attending like this:
Create views
Create the Animators
Create Bits and breathing the Flips
Conclusion
See Also: hack skype
public chic FlipActivity extends Activity { ... accessible changeless chic CardFrontFragment extends Fragment { accessible CardFrontFragment() {} @Override accessible Appearance onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { acknowledgment inflater.inflate(R.layout.fragment_card_front, container, false); } } accessible changeless chic CardBackFragment extends Fragment { accessible CardBackFragment() {} @Override accessible Appearance onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { acknowledgment inflater.inflate(R.layout.fragment_card_back, container, false); } } }
public chic FlipActivity extends Activity { @Override adequate abandoned onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_flip); if (savedInstanceState == null) { getFragmentManager() .beginTransaction() .add(R.id.container, new CardFrontFragment()) .commit(); } ... }
private abandoned flipCard() { if (mShowingBack) { getFragmentManager().popBackStack(); return; } // Cast to the back. mShowingBack = true; getFragmentManager() .beginTransaction() .setCustomAnimations( R.animator.right_in, R.animator.right_out, R.animator.left_in, R.animator.left_out) .replace(R.id.container, new CardBackFragment()) // Add this transaction to the aback stack, acceptance users to columnist Back // to get to the advanced of the card. .addToBackStack(null) // Accomplish the transaction. .commit(); }
@Override public boolean onCreateOptionsMenu(Menu menu) { super.onCreateOptionsMenu(menu); MenuItem account = menu.add(Menu.NONE, R.id.action_flip, Menu.NONE, mShowingBack ? R.string.action_photo : R.string.action_info); item.setIcon(mShowingBack ? R.drawable.ic_action_photo : R.drawable.ic_action_info); item.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM); acknowledgment true; } @Override public boolean onOptionsItemSelected(MenuItem item) { about-face (item.getItemId()) { case android.R.id.home: NavUtils.navigateUpTo(this, new Intent(this, FlipActivity.class)); acknowledgment true; case R.id.action_flip: flipCard(); acknowledgment true; } acknowledgment super.onOptionsItemSelected(item); }
package com.example.vaatiesther.flipactionanimation; import android.app.Activity; import android.app.Fragment; import android.app.FragmentManager; import android.content.Intent; import android.os.Bundle; import android.support.v4.app.NavUtils; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; public chic FlipActivity extends Activity implements FragmentManager.OnBackStackChangedListener { clandestine boolean mShowingBack = false; @Override adequate abandoned onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_flip); if (savedInstanceState == null) { getFragmentManager() .beginTransaction() .add(R.id.container, new CardFrontFragment()) .commit(); } abroad { mShowingBack = (getFragmentManager().getBackStackEntryCount() > 0); } getFragmentManager().addOnBackStackChangedListener(this); } @Override accessible boolean onCreateOptionsMenu(Menu menu) { super.onCreateOptionsMenu(menu); MenuItem account = menu.add(Menu.NONE, R.id.action_flip, Menu.NONE, mShowingBack ? R.string.action_photo : R.string.action_info); item.setIcon(mShowingBack ? R.drawable.ic_action_photo : R.drawable.ic_action_info); item.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM); acknowledgment true; } @Override accessible boolean onOptionsItemSelected(MenuItem item) { about-face (item.getItemId()) { case android.R.id.home: // Navigate "up" the audience anatomy to the launchpad activity. // See http://developer.android.com/design/patterns/navigation.html for more. NavUtils.navigateUpTo(this, new Intent(this, FlipActivity.class)); acknowledgment true; case R.id.action_flip: flipCard(); acknowledgment true; } acknowledgment super.onOptionsItemSelected(item); } clandestine abandoned flipCard() { if (mShowingBack) { getFragmentManager().popBackStack(); return; } // Cast to the back. mShowingBack = true; getFragmentManager() .beginTransaction() .setCustomAnimations( R.animator.right_in, R.animator.right_out, R.animator.left_in, R.animator.left_out) .replace(R.id.container, new CardBackFragment()) // Add this transaction to the aback stack, acceptance users to columnist Back // to get to the advanced of the card. .addToBackStack(null) // Accomplish the transaction. .commit(); } @Override accessible abandoned onBackStackChanged() { mShowingBack = (getFragmentManager().getBackStackEntryCount() > 0); // Aback the aback assemblage changes, invalidate the options agenda (action bar). invalidateOptionsMenu(); } accessible changeless chic CardFrontFragment extends Fragment { accessible CardFrontFragment() {} @Override accessible Appearance onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { acknowledgment inflater.inflate(R.layout.fragment_card_front, container, false); } } accessible changeless chic CardBackFragment extends Fragment { accessible CardBackFragment() {} @Override accessible Appearance onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { acknowledgment inflater.inflate(R.layout.fragment_card_back, container, false); } } }
- It sets the animations that you created for the fragment transitions.
- It replaces the currently displayed fragment with a new fragment and animates this accident with your animations.
- It adds the ahead displayed fragment to the fragment aback accumulation appropriately aback the user presses the Aback button, the agenda flips backward.
Comments
Post a Comment