如何设置工具栏溢出菜单图标的动画 [英] How to animate toolbar overflow menu icon

查看:133
本文介绍了如何设置工具栏溢出菜单图标的动画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以为工具栏上默认的带有3个垂直点的菜单图标设置动画?

Is there a way to animate the default 3-vertical-dotted menu icon on toolbar?

我使用标准代码将工具栏用作操作栏:

I use toolbar as actionbar with the standard code:

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

,而且我还在活动中使用onCreateOptionsMenu方法,在其中为我的menu.xml文件充气 但我不知道如何获得对自动创建的溢出图标的更多控制.我最感兴趣的是如何引用菜单图标,以便对其进行动画处理.我不在乎动画类型.可以是简单的旋转动画

and I also use the onCreateOptionsMenu method inside activity where I inflate my menu.xml file but I don't know how to gain more control over the overflow icon which is created automatically. What I'm most interested in is how to reference the menu icon So I can animate it. I don't care about the animation type. It can be a simple rotation animation

推荐答案

好吧,您专门使用View

Well, you play with the View specifically ActionMenuView so try this, copy the codes into your Activity

//we declare our objects globally
Toolbar tool;  ActionMenuView amv;

然后覆盖onPrepareOptionsMenu,您决定选择return

then override onPrepareOptionsMenu, what you decide to return is your choice

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    //to be safe you can check if children are greater than 1
    amv = (ActionMenuView) tool.getChildAt(1);//hope you've met amv
    return true;
}

现在这是至关重要的部分-每当您要为" 3个verticall点"设置动画时-(您的溢出)您都必须检查可见的孩子-(例如,如果您想)真的忘记了

now this is the crucial part- whenever you want to animate the "3 verticall dots" -(your overflow) you have to check visible children-(i.e if you want to) actually forget that

amv.getChildAt(amv.getChildCount()-1).startAnimation(AnimationUtils.loadAnimation(
        MainActivity.this,R.anim.abc_fade_in));

为您提供基本的淡入动画-您现在可以拉皮条兜风.

that gives you a basic fade-in animation- you can pimp your ride now.

编辑1 :

上面的代码假设除了将onCreateOptionsMenu中的菜单放大之外,您没有添加任何工具栏.

The above code made assumptions that you have nothing added to your Toolbar aside from just inflating the menu in onCreateOptionsMenu.

假设您有一个复杂的ToolBar而不是将其用于初始化

Suppose you have a complex ToolBar use this rather for your initialisation

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    for(int i =0; i < tool.getChildCount(); ++i){
        if(tool.getChildAt(i).getClass().getSimpleName().equals("ActionMenuView")){
            amv = (ActionMenuView) tool.getChildAt(i);
            break;
        }
    }
    return true;
}

A 也可以在onCreateOptionsMenuonPrepareOptionsMenu中调用amv视图的初始化,因此我选择onPrepareOptionsMenu是因为我希望可读性

Also where you call your initialisation of amv View can be in either onCreateOptionsMenu or onPrepareOptionsMenu, i chose onPrepareOptionsMenu because i wanted readability

希望有帮助

这篇关于如何设置工具栏溢出菜单图标的动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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