Add rotation animations to the buttons in Android Actionbar

Source: Internet
Author: User

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/

Add rotation animations to the buttons in Android Actionbar

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.