我有一个带有图标的菜单项(例如,想象一个带有单个柳叶刀的时钟),我想让它的图标无限旋转.
我怎么能管理这个效果?谢谢.
解决方法:
添加文件res / layout / iv_refresh.xml(将ic_launcher替换为您的自定义图标):
<?xml version="1.0" encoding="utf-8"?>
<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:contentDescription="@string/app_name"
android:src="@drawable/ic_launcher" />
添加文件res / anim / rotate_refresh.xml:
<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="1000"
android:fromdegrees="0"
android:interpolator="@android:anim/linear_interpolator"
android:pivotX="50%"
android:pivotY="50%"
android:todegrees="360">
</rotate>
最后在您的java代码中,您可以像这样启动动画:
LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
ImageView iv = (ImageView)inflater.inflate(R.layout.iv_refresh, null);
Animation rotation = AnimationUtils.loadAnimation(this, R.anim.rotate_refresh);
rotation.setRepeatCount(Animation.INFINITE);
iv.startAnimation(rotation);
menu.findItem(R.id.my_menu_item_id).setActionView(iv);