Android:如何在工具栏的菜单图标上制作过渡动画? [英] Android: how to make transition animations on toolbar's menu icons?
本文介绍了Android:如何在工具栏的菜单图标上制作过渡动画?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果您使用的是最新版的WhatsApp,您会注意到,如果您长按聊天窗口中的一个文本框,那么工具栏上的菜单图标将会随着旋转动画而变化.
If you use the last version of WhatsApp you will notice that if you long click a textbox in a chat, then the menu icons on the toolbar will change with a nice rotating animation.
我该如何再现这种效果?我知道我应该使菜单无效,而不是使动画无效.
How could I reproduce that effect? I know I should invalidate the menu but not how to make the animation.
推荐答案
- 使用
Toolbar
. - 等待工具栏的项目膨胀.
- 找到有问题的物品
- 使项目动起来
- Use a
Toolbar
. - Wait for the Toolbar to have its items inflated.
- Find the item in question
- Animate the item
示例:
mToolbar = (Toolbar) findViewById(R.id.toolbar);
mToolbar.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
@Override
public void onLayoutChange(View v, int left, int top, int right, int bottom,
int oldLeft, int oldTop, int oldRight, int oldBottom) {
View item = mToolbar.findViewById(R.id.action_add_item);
if (item != null) {
mToolbar.removeOnLayoutChangeListener(this);
item.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ObjectAnimator animator = ObjectAnimator
.ofFloat(v, "rotation", v.getRotation() + 180);
animator.start();
}
});
}
}
});
注意R.id.action_add_item
是MenuItem
的id
属性.
这篇关于Android:如何在工具栏的菜单图标上制作过渡动画?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文