【问题标题】:Animated menu item "jumps" when animation starts动画开始时动画菜单项“跳跃”
【发布时间】:2013-10-09 00:07:40
【问题描述】:

我正在使用来自这个问题的代码:Animated Icon for ActionItem 为我的刷新设置动画 ActionBarButton。它工作正常,只是样式似乎不正确。当我单击该项目时,它开始旋转,但只有在它“跳跃”几个像素之后。 ImageView 的样式似乎与菜单项的样式不同。

项目是这样定义的:

<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,或反之?

【问题讨论】:

    标签: android android-animation menuitem


    【解决方案1】:

    我通过将View 也设置为非动画项目来找到解决方案。在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 处理程序:

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

    最后,调用这个来启动动画:

    refreshItem.getActionView().startAnimation(rotation);
    

    然后停止它:

    refreshItem.getActionView().clearAnimation();
    

    【讨论】:

    • getActionView() 为我返回 null
    • 什么是我定义此布局的 refresh_action_view 布局
    • @Zapnologica 确保在尝试获取操作视图之前设置一个操作视图。
    • 正如@Zapnologica 指出的那样,上面的代码对我也不起作用-getActionView() 也为我返回了 null。我花了一段时间才意识到需要做什么,所以这里是:View image = findViewById(R.id.action_refresh); // Replace the ID with your image's one refreshItem.setActionView(image);
    【解决方案2】:

    最好把ImageView改成ImageButton,不要在menu.xml中改变菜单项的布局,这样图片就不会跳转,也不需要添加点击处理程序。它也将是正确的大小,如果您使用 ImageView 而不是 ImageButton,则不会出现这种情况。

    这就是你所需要的:

    <ImageButton 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"
    android:id="@+id/refresh_view"
    style="@android:style/Widget.ActionButton" />
    

    【讨论】:

      【解决方案3】:

      在 ImageView 中添加一些填充。就我而言,每边 8dp。

      <ImageView xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:paddingRight="8dp"
          android:paddingLeft="8dp"
          android:src="@drawable/ic_action_important" />
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-04-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多