How to add an animation to the Actionbar (2.3 supported)
Note: You need to import the v4 package.
Private MenuItem refreshItem;
@ Override
Public boolean onOptionsItemSelected (MenuItem item ){
Switch (item. getItemId ()){
Case R. id. action_refresh:
ShowRefreshAnimation (item );
Return true;
Default:
Return super. onOptionsItemSelected (item );
}
}
Private void showRefreshAnimation (MenuItem item ){
RefreshItem = item;
// Use an ImageView to set it to the ActionView of MenuItem, so that we can use this ImageView to display the rotation animation.
ImageView refreshActionView = (ImageView) LayoutInflater. from (getActivity ()).
Inflate (R. layout. actionbar_refresh_view, null );
RefreshActionView. setImageResource (R. drawable. refreshnormal );
// RefreshItem. setActionView (refreshActionView); // This method must be later than 11
MenuItemCompat. setActionView (item, refreshActionView); // replace it with the v4 compatibility package.
// Display the refresh Animation
Animation animation = AnimationUtils. loadAnimation (getActivity (), R. anim. rotateanim );
Animation. setRepeatMode (Animation. RESTART );
Animation. setRepeatCount (Animation. INFINITE );
RefreshActionView. startAnimation (animation );
}
Public void hideRefreshAnimation (){
If (refreshItem! = Null ){
/// View view = refreshItem. getActionView ();
View view = MenuItemCompat. getActionView (refreshItem );
If (view! = Null ){
View. clearAnimation ();
RefreshItem. setActionView (null );
}
}
}
Xmlns: funshion = "http://schemas.android.com/apk/res-auto">
Android: id = "@ + id/action_deleted"
Android: icon = "@ drawable/common_delete_btn_selector"
Android: title = "@ string/delete"
Funshion: showAsAction = "ifRoom"/>
Android: id = "@ + id/action_refresh"
Android: icon = "@ drawable/refreshnormal"
Android: title = "@ string/refresh"
Funshion: showAsAction = "ifRoom"/>
Actionbar_refresh_view.xml
Style = "@ android: style/Widget. ActionButton"
Android: layout_height = "wrap_content"
Android: scaleType = "centerInside"/>