Displays the menu item on Actionbar, where a refresh button is displayed to simulate the addition of a refresh animation when the action is refreshed
Menu layout
Menu.xml
<Menuxmlns:android= "Http://schemas.android.com/apk/res/android" > <ItemAndroid:id= "@+id/action_stop"android:orderincategory= "+"android:showasaction= "Always"Android:title= "@string/action_stop"/> <ItemAndroid:id= "@+id/action_refresh"android:orderincategory= "+"android:showasaction= "Always"Android:icon= "@drawable/ic_action_refresh" /></Menu>
View Refresh_view.xml with rotation animation displayed
<xmlns:android= "http://schemas.android.com/apk/res/android" Style = "@android: Style/widget.actionbutton" android:layout_width= "Wrap_content" android:layout_height= "Wrap_content" android:scaletype= " Centerinside " android:contentdescription=" Refresh " />
Set rotation properties
Refresh.xml
<?XML version= "1.0" encoding= "Utf-8"?><!--Copyright: GitHub Inc. Licensed under the Apache License, Version 2.0 (the "License"); You are not a use of this file except in compliance with the License. Obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 unless required by applicable law or Agreed to writing, software distributed under the License are distributed on a "as is" BASIS, without warranties OR C Onditions of any KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. -<Rotatexmlns:android= "Http://schemas.android.com/apk/res/android"android:duration= "$"android:fromdegrees= "0"Android:interpolator= "@android: Anim/linear_interpolator"Android:pivotx= "50%"Android:pivoty= "50%"android:todegrees= " the" />
View Code
Activity
PackageCom.example.actionbarmenubtnrefresh;ImportAndroid.os.Bundle;Importandroid.app.Activity;ImportAndroid.view.Menu;ImportAndroid.view.MenuItem;ImportAndroid.view.View;ImportAndroid.view.animation.Animation;ImportAndroid.view.animation.AnimationUtils;ImportAndroid.widget.ImageView;ImportAndroid.widget.Toast; Public classMainactivityextendsActivity {protectedMenuItem Refreshitem;Private BooleanIsrefreshstate =false; @Overrideprotected voidOnCreate (Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate); Setcontentview (R.layout.main); } @Override Public BooleanOncreateoptionsmenu (Menu menu) {//Menuinflater instantiating the menu layout file under the menu directoryGetmenuinflater (). Inflate (R.menu.main, menu);return true; } @Override Public BooleanOnoptionsitemselected (MenuItem Item) {Switch(Item.getitemid ()) { CaseR.id.action_refresh:refreshmenuitemview (item); Toast.maketext (Getbasecontext (), "Start Refresh", Toast.length_short). Show (); Break; CaseR.id.action_stop:hiderefreshanimation (); Toast.maketext (Getbasecontext (), "Stop Refresh", Toast.length_short). Show (); Break;default: Break; }return Super. onoptionsitemselected (item); }Private voidRefreshmenuitemview (MenuItem Item) {hiderefreshanimation (); Refreshitem = Item;//Layoutinflater instantiating the entire layout file//Actionview with ImageView set to MenuItemImageView Refreshview = (ImageView) getlayoutinflater (). Inflate (R.layout.refresh_view,NULL); Refreshview.setimageresource (R.drawable.ic_action_refresh); Refreshitem.setactionview (Refreshview);//Show Refresh AnimationsAnimation Animation = animationutils.loadanimation ( This, R.anim.refresh);//Set Repeat mode defines what is this animation should does when it reaches the endAnimation.setrepeatmode (Animation.restart);//set number of repetitionsAnimation.setrepeatcount (Animation.infinite);//using ImageView to display rotation animationsRefreshview.startanimation (animation); Isrefreshstate =true; }Private voidHiderefreshanimation () {if(Refreshitem! =NULL{View view = Refreshitem.getactionview ();if(View! =NULL) {view.clearanimation (); Refreshitem.setactionview (NULL); Isrefreshstate =false; } } } }
View Code
The effect is as follows
Tap Stop to stop the animation
Reference http://my.oschina.net/fanxiao/blog/152175
GIF pictures online production: http://picasion.com/
Other great article articles
jquery Tutorials (9)-dom tree Operations copy elements Android learning note (Android Alertdialog) create List dialog [2]android sharesdk SSO login Sina and MySQL Index type in detail-b-tree index broadcastreceiver use the Alertdialog after the app crashes
More about Android development articles
Add rotation animations to the buttons in Android Actionbar