Android 在更改图像时为工具栏图标设置动画
Android animate toolbar icon while changing its image
在我的应用程序中,我需要以编程方式更改工具栏按钮的图像,为此我使用:
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);
它工作得很好,问题是它发生得太快了,所以我需要为过渡添加动画,但我不知道如何为工具栏按钮添加动画。
有人可以帮我吗?
非常感谢!
也许这些答案可以帮助您:
好的,最后感谢 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);
}
在我的应用程序中,我需要以编程方式更改工具栏按钮的图像,为此我使用:
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);
它工作得很好,问题是它发生得太快了,所以我需要为过渡添加动画,但我不知道如何为工具栏按钮添加动画。 有人可以帮我吗? 非常感谢!
也许这些答案可以帮助您:
好的,最后感谢 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);
}