Add rotation animation and androidactionbar to buttons in Android ActionBar

Source: Internet
Author: User

Add rotation animation and androidactionbar to buttons in Android ActionBar

Display the Menu item on ActionBar. A refresh button is displayed to simulate the addition of a refresh animation during the refresh action.

 

Menu Layout

Menu. xml

<menu xmlns:android="http://schemas.android.com/apk/res/android" >    <item        android:id="@+id/action_stop"        android:orderInCategory="100"        android:showAsAction="always"        android:title="@string/action_stop"/>    <item        android:id="@+id/action_refresh"        android:orderInCategory="100"        android:showAsAction="always"        android:icon="@drawable/ic_action_refresh"        /></menu>

 

Display the view refresh_view.xml of the rotating Animation

<ImageView 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 attributes

Refresh. xml

<? Xml version = "1.0" encoding = "UTF-8"?> <! -- Copyright 2012 GitHub Inc. licensed under the Apache License, Version 2.0 (the "License"); you may not use this file License t in compliance with the License. you may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "as is" BASIS, without warranties or conditions of any kind, either express or implied. see the License for the specific language governing permissions and limitations under the License. --> <rotate xmlns: android = "http://schemas.android.com/apk/res/android" android: duration = "2000" android: fromDegrees = "0" android: interpolator = "@ android: anim/linear_interpolator" android: required Tx = "50%" android: Required ty = "50%" android: toDegrees = "360"/>View Code

 

Activity

Package com. example. actionbarmenubtnrefresh; import android. OS. bundle; import android. app. activity; import android. view. menu; import android. view. menuItem; import android. view. view; import android. view. animation. animation; import android. view. animation. animationUtils; import android. widget. imageView; import android. widget. toast; public class MainActivity extends Activity {protected MenuItem refreshItem; pri Vate boolean isRefreshState = false; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main) ;}@ Override public boolean onCreateOptionsMenu (Menu menu) {// MenuInflater instantiates the Menu layout file getMenuInflater () under the Menu directory (). inflate (R. menu. main, menu); return true ;}@ Override public boolean onOptionsItemSelected (MenuItem item) {switch (item. g EtItemId () {case R. id. action_refresh: refreshMenuItemView (item); Toast. makeText (getBaseContext (), "Start to refresh", Toast. LENGTH_SHORT ). show (); break; case R. id. action_stop: hideRefreshAnimation (); Toast. makeText (getBaseContext (), "stop refreshing", Toast. LENGTH_SHORT ). show (); break; default: break;} return super. onOptionsItemSelected (item);} private void refreshMenuItemView (MenuItem item) {hideRefreshAnimation (); RefreshItem = item; // LayoutInflater instantiate the entire layout file // use ImageView to set it to the ActionView ImageView refreshView = (ImageView) getLayoutInflater () of MenuItem (). inflate (R. layout. refresh_view, null); refreshView. setImageResource (R. drawable. ic_action_refresh); refreshItem. setActionView (refreshView); // display and refresh Animation animation Animation = AnimationUtils. loadAnimation (this, R. anim. refresh); // set the recurrence mode Defines what this animation sho Could do when it reaches the end animation. setRepeatMode (Animation. RESTART); // set the number of repeaters to animation. setRepeatCount (Animation. INFINITE); // use ImageView to display the rotation animation refreshView. startAnimation (animation); isRefreshState = true;} private void hideRefreshAnimation () {if (refreshItem! = Null) {View view = refreshItem. getActionView (); if (view! = Null) {view. clearAnimation (); refreshItem. setActionView (null); isRefreshState = false ;}}}}View Code

 

The effect is as follows:

 

Click stop to stop the animation.

 

 

Reference http://my.oschina.net/fanxiao/blog/152175

GIF image online production: http://picasion.com/

Related Article

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.