【问题标题】:Navigation drawer icons (hamburger & arrow) animation when expand action view展开动作视图时的导航抽屉图标(汉堡和箭头)动画
【发布时间】:2015-07-28 21:46:49
【问题描述】:

我正在使用AppCompatToolbar

我确保在抽屉式导航图标从汉堡包变为箭头或反之时会有动画。

我使用以下技术https://stackoverflow.com/a/26469738/72437

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="drawerArrowStyle">@style/DrawerArrowStyle</item>
    </style>

    <style name="DrawerArrowStyle" parent="Widget.AppCompat.DrawerArrowToggle">
        <item name="spinBars">true</item>
        <item name="color">@android:color/white</item>
    </style>

</resources>

当我按下导航抽屉时,动画会发生,以滑入和滑出菜单。但是,它不是很有用。根据材料设计指南,导航抽屉片段应该是全高的。因此,当它开始滑出时,它会阻止导航抽屉图标。

在处理我的搜索按钮 (action_search) 时,我对执行动画更感兴趣,它充当我工具栏的操作视图。

main_menu.xml

<item android:id="@+id/action_search"
    android:title="Search me"
    android:icon="@drawable/ic_add_white_24dp"
    app:showAsAction="always|collapseActionView"
    app:actionLayout="@layout/collapsible_searchtext" />

<item android:id="@+id/action_settings" android:title="@string/action_settings"
    android:orderInCategory="100" app:showAsAction="never" />

collapsible_searchtext.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    >
    <android.widget.AutoCompleteTextView
        android:id="@+id/search"
        android:dropDownWidth="match_parent"
        android:completionThreshold="1"
        android:inputType="textNoSuggestions"
        android:imeOptions="actionSearch|flagNoExtractUi"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:singleLine="true"
        android:hint="Search stock"
        android:layout_gravity="center_vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</RelativeLayout>
  1. 当我按action_search 时,搜索图标将扩展为AutoCompleteTextView同时,汉堡图标会动画成箭头图标
  2. 当我按下箭头图标时,箭头图标将动画回汉堡图标。

我尝试通过阅读https://stackoverflow.com/a/26480439/72437后使用以下代码来实现目标1

private void animateHamburgerToArrow() {
    ValueAnimator anim = ValueAnimator.ofFloat(0f, 1f);
    anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
        @Override
        public void onAnimationUpdate(ValueAnimator valueAnimator) {
            float slideOffset = (Float) valueAnimator.getAnimatedValue();
            actionBarDrawerToggle.onDrawerSlide(drawerLayout, slideOffset);
        }
    });
    anim.setInterpolator(new DecelerateInterpolator());
    // You can change this duration to more closely match that of the default animation.
    anim.setDuration(500);
    anim.start();
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if (actionBarDrawerToggle.onOptionsItemSelected(item)) {
        return true;
    }

    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        animateHamburgerToArrow();
        return true;
    } else if (id == R.id.action_search) {
        animateHamburgerToArrow();
        return true;
    }

    return super.onOptionsItemSelected(item);
}

当我按下action_search 时,汉堡图标确实会变成箭头图标。但是,没有动画。我可以知道我错过了什么吗?

更新

我怀疑导航抽屉使用的左箭头与搜索操作视图使用的左箭头不同。它们是 2 个不同的实例。

这是因为如果我点击action_settings,我可以观察到动画。但是,如果我点击action_search,我将不会观察到动画。我怀疑搜索操作视图使用的左箭头,“阻止”导航抽屉的图标(汉堡包和箭头)

【问题讨论】:

  • 你能做到吗?即使我面临同样的问题,
  • 我还在试验中。我的主要参考是stackoverflow.com/a/27680449/72437 关键思想是避免app:actionViewClass 的默认行为。相反,我们创建自己的自定义视图并拥有自己的自定义代码,以在工具栏中显示自定义视图。我仍在尝试它github.com/yccheok/toolbar-experiment(未完成的工作)但是,我想说@Mike 给出的方向相当好
  • 好的,即使我在做研发,如果我实现了也会告诉你

标签: android animation


【解决方案1】:

我在没有展开我自己的自定义 SearchView 的情况下实现了这个效果。 问题是,汉堡和后退箭头实际上有两个单独的按钮。因此,虽然我们可以使用公共方法 setHomeAsUpIndicator() 访问汉堡按钮,但只能通过反射访问后退按钮。然后我们只需在其上设置所需的可绘制对象,并同时在两个按钮可绘制对象上播放动画,而不管特定按钮是否可见。这是因为两个按钮在布局中的位置完全相同。

详情见帖子http://choruscode.blogspot.com/2015/09/morphing-animation-for-toolbar-back.html

【讨论】:

    猜你喜欢
    • 2022-08-15
    • 1970-01-01
    • 1970-01-01
    • 2016-02-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多