Android动画工具栏图标,同时更改其图像

问题描述 投票:2回答:2

在我的应用程序中,我需要以编程方式更改工具栏按钮的图像,因此我使用:

public boolean onCreateOptionsMenu( Menu menu ) {
        getMenuInflater().inflate(R.menu.main_activity_menu, menu);
            toolbarButton = menu.findItem(R.id.action_button);
            return true;
        }

toolbarButton.setIcon(R.drawable.ic_settings);

并且它工作得很好,问题是它发生得太快,所以我需要在转换中添加动画,但我不知道如何将动画添加到工具栏按钮。有人可以帮帮我吗?非常感谢!

android animation android-animation android-toolbar
2个回答
3
投票

好的,所以最后感谢Victorldavila我设法让它工作,这是我的最终代码:

View button = toolbar.findViewById(R.id.action_button);

//Remove icon animation
if (button != null) {
   Animation animation = AnimationUtils.loadAnimation(getBaseContext(), R.anim.fragment_fade_out);
   animation.setStartOffset(0);
   button.startAnimation(animation);
}

//Add icon animation
if (button != null) {
   Animation animation = AnimationUtils.loadAnimation(getBaseContext(), R.anim.fragment_slide_in_up);
   animation.setStartOffset(0);
   button.startAnimation(animation);
}

© www.soinside.com 2019 - 2024. All rights reserved.