und*_*oid 1 animation android android-actionbar android-actionbar-compat
DrawerLayout我的应用程序中有一个ActionBar. 打开菜单时,该图标不可见。再次关闭菜单后,该图标将重新出现。现在,它只是立即显示\消失。我想为图标添加淡入淡出动画。有什么办法可以达到这个效果吗?
此代码当前用于切换图标:
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
if(!drawerOpened){
inflater.inflate(R.menu.chats_activity_action, menu);
} else {
actionBar.setDisplayUseLogoEnabled(false);
}
return true;
}
Run Code Online (Sandbox Code Playgroud)
这是在 XML 文件中定义图标的方式:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" >
<item
android:id="@+id/menu_filter"
android:title="Add a user"
app:showAsAction="always"
android:icon="@drawable/plus_icon"/>
</menu>
Run Code Online (Sandbox Code Playgroud)
首先,让MenuItem你的布局不可见:
<item
android:id="@+id/menu_filter"
android:title="Add a user"
app:showAsAction="always"
android:icon="@drawable/plus_icon"
android:visible="false"/> <!-- New attribute -->
Run Code Online (Sandbox Code Playgroud)
然后修改你的onCreateOptionsMenu:
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
if(!drawerOpened){
inflater.inflate(R.menu.chats_activity_action, menu);
final MenuItem item = menu.findItem(R.id.menu_filter);
// Post delayed so the view can be built,
// otherwise findViewById(R.id.menu_filter) would be null
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
AlphaAnimation animation = new AlphaAnimation(0.0f, 1.0f);
animation.setDuration(1000);
// Make item visible and start the animation
item.setVisible(true);
findViewById(R.id.menu_filter).startAnimation(animation);
}
}, 1);
} else{
actionBar.setDisplayUseLogoEnabled(false);
inflater.inflate(R.menu.chats_activity_action, menu);
final MenuItem item = menu.findItem(R.id.menu_filter);
item.setVisible(true);
// Post delayed so the view can be built,
// otherwise findViewById(R.id.menu_filter) would be null
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
AlphaAnimation animation = new AlphaAnimation(0.1f, 0.0f);
animation.setFillEnabled(true);
animation.setFillAfter(true);
animation.setDuration(1000);
// start the animation
findViewById(R.id.menu_filter).startAnimation(animation);
}
}, 1);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
item.setVisible(false);
}
}, 1000); // The animation is finished after 1000ms
}
return true;
}
Run Code Online (Sandbox Code Playgroud)
基本上,即使抽屉打开,物品也会充气。之后,该项目通过动画淡出并在动画结束后设置为不可见。
| 归档时间: |
|
| 查看次数: |
1322 次 |
| 最近记录: |