动画菜单项目QUOT;跳跃"当动画开始 [英] Animated menu item "jumps" when animation starts

查看:110
本文介绍了动画菜单项目QUOT;跳跃"当动画开始的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在用的是code,从这样一个问题:动画图标ActionItem 来动画我刷新 ActionBarButton 。它工作正常,只是风格似乎并不正确。当我点击该项目时,开始旋转,但只有在它跳了几个像素。这似乎是的ImageView 的风格是从菜单项的风格不同。

I am using the code from this question: Animated Icon for ActionItem to animate my refresh ActionBarButton. It works fine, except that the style doesn't seem to be right. When I click the item, it starts rotating, but only after it "jumps" a few pixels. It seems like the style of the ImageView is different from the style of the menu item.

该项目的定义是这样的:

The item is defined like this:

<item
    android:id="@+id/action_refresh"
    android:orderInCategory="100"
    android:icon="@drawable/ic_menu_refresh"
    android:showAsAction="ifRoom"

    <!-- added this myself, didn't have any effect -->
    style="@android:style/Widget.ActionButton"  
    android:title="@string/action_refresh"/>

的ImageView 是这样的:

<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:contentDescription="@string/action_refresh"
    android:src="@drawable/ic_menu_refresh"
    style="@android:style/Widget.ActionButton" />

如何样式我的菜单项,以匹配的ImageView 在旋转,或者倒过来?

How can I style my menuitem to match the ImageView in the rotation, or the other way round?

推荐答案

我找到了解决方案,通过设置查看也非动画的项目是一样的。在 menu.xml文件

I found the solution by setting the View also for the non-animated item to be the same. In the menu.xml:

<item
    android:id="@+id/action_refresh"
    android:orderInCategory="100"
    android:icon="@drawable/ic_menu_refresh"
    android:actionLayout="@layout/refresh_action_view"
    android:showAsAction="ifRoom"
    style="@android:style/Widget.ActionButton"
    android:title="@string/action_refresh"/>

然后,在 onCreateOptionsMenu ,我取的一个成员变量的观点,并附加一个onclick处理程序:

Then, in onCreateOptionsMenu, I fetch the view in a member variable, and attach an onclick handler:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.orders, menu);
    final Menu m = menu;
    refreshItem = menu.findItem(R.id.action_refresh);
    refreshItem.getActionView().setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {   
                        m.performIdentifierAction(refreshItem.getItemId(), 0);
                    }
            });
    return true;
}

的onCreate ,我们载入动画:

    rotation = AnimationUtils.loadAnimation(this, R.anim.clockwise_refresh);
    rotation.setRepeatCount(Animation.INFINITE);

最后,调用此启动动画:

Finally, call this to start the animation:

refreshItem.getActionView().startAnimation(rotation);

这阻止它:

refreshItem.getActionView().clearAnimation();

这篇关于动画菜单项目QUOT;跳跃&QUOT;当动画开始的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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