Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Animation of menu items in ActionBar

When you long click on email item in Gmail application (Honeycomb) the context-like menu is shown with it's items animated on start. How this is made? Thanks.

like image 807
Eugene Avatar asked Nov 27 '22 16:11

Eugene


1 Answers

You cannot animate MenuItems, so the trick is to set a same looking View in its place with MenuItem.setActionView() and animate that view as you would normally and then unset it when animation is done with MenuItem.setActionView(null)

public class MainFragment extends Fragment {

   private Menu mOptionsMenu;

   @Override
   public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
       mOptionsMenu = menu;
       inflater.inflate(R.menu.fragment_main, menu);
   }

   private void animateActionBarItem() {
       final MenuItem refreshItem = mOptionsMenu.findItem(R.id.action_refresh);
       refreshItem.setActionView(R.layout.actionbar_foo);
       refreshItem.getActionView()
            .animate()
            .setInterpolator(new AccelerateInterpolator())
            .setDuration(100L)
            .scaleX(2F)
            .scaleY(2F)
            .withEndAction(new Runnable() {

                @Override
                public void run() {
                    refreshItem.setActionView(null);
                }
            });
      }

}

XML for R.layout.actionbar_foo

<?xml version="1.0" encoding="utf-8"?>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
    style="?android:attr/actionButtonStyle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/ic_action_accept" />
like image 151
urSus Avatar answered Dec 09 '22 17:12

urSus