开发者

Animation of menu items in ActionBar

开发者 https://www.devze.com 2023-04-05 10:39 出处:网络
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.You cannot animate MenuIte

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.


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" />


Take a look at my answer here. It is related to the layout changes of the parent of menu items, but if you look at my idea there, you will catch a glimpse how to animate menu items.


Take a look here. You can animate any view calling an animation to start on it.

0

精彩评论

暂无评论...
验证码 换一张
取 消