Android 菜单动画效果

为标题栏菜单项添加动画效果,需要设置菜单的 actionView,并且给 actionView设置动画。点击菜单时开始动画,完成菜单项对应的功能后停止动画。

菜单项的 actionView





旋转动画:





模拟延时动画效果

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        super.onCreateOptionsMenu(menu, inflater);
        inflater.inflate(R.menu.item_rotate, menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.refresh: {
                mRefreshItem = item;
                refresh();
                return true;
            }
        }
        return super.onOptionsItemSelected(item);
    }

    private void refresh() {
        if (getActivity() == null) {
            return;
        }
        ImageView imageView = (ImageView) getActivity().getLayoutInflater()
                .inflate(R.layout.action_view_refresh, null);

        Animation animation = AnimationUtils.loadAnimation(getActivity(), R.anim.clockwise_refresh);
        animation.setRepeatCount(Animation.INFINITE);
        imageView.startAnimation(animation);
        mRefreshItem.setActionView(imageView);

        new Thread() {
            @Override
            public void run() {
                try {
                    sleep(3000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } finally {
                    getView().post(new Runnable() {
                        @Override
                        public void run() {
                            mRefreshItem.getActionView().clearAnimation();
                            mRefreshItem.setActionView(null);
                        }
                    });
                }

            }
        }.start();
    }

注意最后要移除动画 clearAnimation(),设置 actionViewnull

你可能感兴趣的:(Android 菜单动画效果)