使用 ActionBarActivity/AppCompat v21 刷新菜单项动画

Refresh Menu Item Animation with ActionBarActivity/AppCompat v21

与此处相同的问题:Refresh menu item animation in ActionBarSherlock

除了我想在 API 11 中使用 ActionBarActivity 和 AppCompat v21 Material Design

refresh.xml

<?xml version="1.0" encoding="utf-8"?>

<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:src="@drawable/ic_menu_refresh"
    android:background="#F00"/>

MyFragment.java

private View mRefresh;
private MenuItem mRefreshItem;
private boolean mIsRefreshing;
private Animation mRotate;

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mRotate = AnimationUtils.loadAnimation(getActivity(), R.anim.rotate);
        mRotate.setRepeatCount(Animation.INFINITE);
        mRotate.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {

            }

            @Override
            public void onAnimationRepeat(Animation animation) {
                if(!mIsRefreshing){
                    mRefreshItem.getActionView().clearAnimation();
                    mRefreshItem.setActionView(null);
                }
            }
        });

        LayoutInflater inflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mRefresh = inflater.inflate(R.layout.refresh, null);
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        super.onCreateOptionsMenu(menu, inflater);
        inflater.inflate(R.menu.menu, menu);
        mRefreshItem = menu.findItem(R.id.action_refresh);

        if(mIsRefreshing){
            mRefreshItem.setActionView(mRefresh);
            mRefresh.startAnimation(mRotate);
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch(item.getItemId()){
            case R.id.action_refresh:
                if(!mIsRefreshing) {
                    mRefreshItem.setActionView(mRefresh);
                    mRefresh.startAnimation(mRotate);
                    startTask();
                }
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

动作视图未与静态图标对齐(红色背景只是为了让我能看到视图):

将填充设置为 0:

<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:src="@drawable/ic_menu_refresh"
    android:padding="0dp"
    style="@style/Widget.AppCompat.ActionButton"/>