Android ActionBar中的按钮添加旋转动画
2015-01-06 13:19
573 查看
将Menu菜单项显示在ActionBar上,这里显示一个刷新按钮,模拟在刷新动作时的添加刷新动画
菜单布局
menu.xml
显示旋转动画的view refresh_view.xml
设置旋转属性
refresh.xml
![](http://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](http://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
View Code
Activity
![](http://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](http://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
View Code
效果如下
![](http://images.cnitblog.com/blog/651797/201501/061226464218378.gif)
点击stop停止动画
参考 http://my.oschina.net/fanxiao/blog/152175
gif图片在线制作: http://picasion.com/
其他精彩文章文章
更多关于android开发文章
菜单布局
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>
显示旋转动画的view refresh_view.xml
<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" />
设置旋转属性
refresh.xml
![](http://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](http://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
<?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 except 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:pivotX="50%" android:pivotY="50%" android:toDegrees="360" />
View Code
Activity
![](http://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](http://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
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; private boolean isRefreshState = false; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } @Override public boolean onCreateOptionsMenu(Menu menu) { //MenuInflater实例化Menu目录下的Menu布局文件 getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.action_refresh: refreshMenuItemView(item); Toast.makeText(getBaseContext(), "开始刷新", Toast.LENGTH_SHORT).show(); break; case R.id.action_stop: hideRefreshAnimation(); Toast.makeText(getBaseContext(), "停止刷新", Toast.LENGTH_SHORT).show(); break; default: break; } return super.onOptionsItemSelected(item); } private void refreshMenuItemView(MenuItem item) { hideRefreshAnimation(); refreshItem = item; //LayoutInflater实例化整个布局文件 //使用ImageView设置成MenuItem的ActionView ImageView refreshView = (ImageView)getLayoutInflater().inflate(R.layout.refresh_view, null); refreshView.setImageResource(R.drawable.ic_action_refresh); refreshItem.setActionView(refreshView); //显示刷新动画 Animation animation = AnimationUtils.loadAnimation(this, R.anim.refresh); //设置重复模式 Defines what this animation should do when it reaches the end animation.setRepeatMode(Animation.RESTART); //设置重复次数 animation.setRepeatCount(Animation.INFINITE); //使用ImageView 显示旋转动画 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
效果如下
![](http://images.cnitblog.com/blog/651797/201501/061226464218378.gif)
点击stop停止动画
参考 http://my.oschina.net/fanxiao/blog/152175
gif图片在线制作: http://picasion.com/
其他精彩文章文章
jQuery教程(9)-DOM树操作之复制元素
android学习笔记(35)android
AlertDialog创建列表对话框[2]
android
shareSDK sso登录新浪和微信
mysql
索引类型详解-B-Tree索引
BroadcastReceiver
使用AlertDialog后 app奔溃了
更多关于android开发文章
相关文章推荐
- Android ActionBar中添加旋转动画
- (转)给ActionBar中的按钮添加旋转动画(刷新动画)
- 给ActionBar中的按钮添加旋转动画(刷新动画)
- Android动画(透明度,平移,旋转,拉伸,动态添加按钮)
- 给ActionBar中的按钮添加旋转动画(刷新动画)
- Android 属性动画(Property Animation 给按钮的添加添加动画效果,同样可以用在整个布局上) (下)(viewGroup创建对象竟然可以用布局的id)
- Android Actionbar 添加返回按钮
- Android仿饿了么加入购物车旋转控件自带闪转腾挪动画的按钮效果(实例详解)
- Android官方开发文档Training系列课程中文版:添加ActionBar之添加Action按钮
- android 旋转按钮和旋转变化动画
- android自定义圆角动画按钮
- Android ListView添加按钮后自身无响应.
- Android 中 画图与动画旋转效果(中级)
- 如何在Android中实现图片及动画的缩放和旋转
- 如何在Android中实现图片及动画的缩放和旋转
- Android动画及图片的缩放和旋转
- Android上Path应用左下角按钮动画效果demo原型
- Android单帧动画Rotate旋转
- Android动画及图片的缩放和旋转
- 实现Android简单动画旋转案例