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

标签 android animation menu rotation menuitem

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

我该如何管理这种效果?谢谢。

最佳答案

添加一个文件 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);

关于android - 菜单项动画,无限旋转其自定义图标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28840815/

相关文章:

android - 有没有办法使用 Android 模拟器和 'geo fix' 传递当前速度数据以及纬度和经度?

javascript - 如果鼠标悬停在 div 上则显示菜单

html - 垂直菜单导航 - 菜单滚动时不显示子菜单

安卓 : creating drawables with CSS input

java - Android 没有选择 facebook 显示图像

java - Libgdx |我应该在暂停期间处理我的 Assets 并在简历中重新创建它们吗?

html - 在没有水平滚动条出现的情况下在窗口边缘稍微旋转一个div

android - 从 Android 中的过渡动画中排除 BottomNavigation

c# - 如何通过 WPF 动画使图像变大(产生选择的错觉)?

javascript - 如何将导航栏移动到另一个位置