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

7

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

我该如何实现这个效果?谢谢。


你可以使用不确定进度条: http://developer.android.com/reference/android/widget/ProgressBar.html#attr_android:indeterminate - shivamDev31
我可以用它来使自定义图标旋转吗? - madx
哦,你想旋转自定义图标...我觉得你可以...让我看看是否有适合你的东西。 - shivamDev31
我认为这个方法会起作用:https://dev59.com/h2ox5IYBdhLWcg3wf0fl。不过我也不确定,所以试一下也没什么损失。 - shivamDev31
2个回答

23

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

3
如何设置菜单项? - Apurva
获取您的视图并分配动画。 - Jesson Atherton
这可能对你有用:Android - 获取菜单项的视图引用 - Jesson Atherton
我尝试使用以下代码:((View)menu.findItem(R.id.my_menu_item_id)).startAnimation(mAnimation); 但是我收到以下错误信息:java.lang.ClassCastException: android.support.v7.internal.view.menu.m cannot be cast to android.view.View - madx
你能提供一个示例展示如何做到这一点吗? - madx
显示剩余8条评论

2

最好的方法在这里:

public class HomeActivity extends AppCompatActivity {
    public static ActionMenuItemView btsync;
    public static RotateAnimation rotateAnimation;

@Override
protected void onCreate(Bundle savedInstanceState) {
    rotateAnimation = new RotateAnimation(360, 0, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
    rotateAnimation.setDuration((long) 2*500);
    rotateAnimation.setRepeatCount(Animation.INFINITE);

然后:

private void sync() {
    btsync = this.findViewById(R.id.action_sync); //remember that u cant access this view at onCreate() or onStart() or onResume() or onPostResume() or onPostCreate() or onCreateOptionsMenu() or onPrepareOptionsMenu()
    if (isSyncServiceRunning(HomeActivity.this)) {
        showConfirmStopDialog();
    } else {
        if (btsync != null) {
            btsync.startAnimation(rotateAnimation);
        }
        Context context = getApplicationContext();
        context.startService(new Intent(context, SyncService.class));
    }
}

请记住,在onCreate()、onStart()、onResume()、onPostResume()、onPostCreate()、onCreateOptionsMenu()或onPrepareOptionsMenu()中,您不能访问“btsync = this.findViewById(R.id.action_sync);”。如果您想在活动启动后立即获取它,请将其放置在postdelayed中。
public static void refreshSync(Activity context) {
    Handler handler = new Handler(Looper.getMainLooper());
    handler.postDelayed(new Runnable() {
        public void run() {
            btsync = context.findViewById(R.id.action_sync);
            if (btsync != null && isSyncServiceRunning(context)) {
                btsync.startAnimation(rotateAnimation);
            } else if (btsync != null) {
                btsync.clearAnimation();
            }
        }
    }, 1000);
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接