菜单项动画,无限旋转其自定义图标 [英] Menu item animation, rotate indefinitely its custom icon

查看:27
本文介绍了菜单项动画,无限旋转其自定义图标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a menu item with an icon (imagine for example a clock with a single lancet), I would like to make its icon rotate indefinitely.

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

How could I manage this effect? Thank you.

推荐答案

添加文件 res/layout/iv_refresh.xml(将 ic_launcher 替换为您的自定义图标):

Add a file res/layout/iv_refresh.xml (replace ic_launcher with your custom icon):

<?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代码中你可以像这样开始动画:

Finally in your java code you can start the animation like this:

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);

这篇关于菜单项动画,无限旋转其自定义图标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆