菜单项动画,无限旋转其自定义图标

mad*_*adx 2 animation android menu rotation menuitem

我有一个带有图标的菜单项(例如,想象一个带有单个柳叶刀的时钟),我想让它的图标无限旋转.

我怎么能管理这个效果?谢谢.

Jes*_*ton 19

添加文件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" />
Run Code Online (Sandbox Code Playgroud)

添加文件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>
Run Code Online (Sandbox Code Playgroud)

最后在您的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);
Run Code Online (Sandbox Code Playgroud)

  • 但是如何为菜单项设置呢? (3认同)