I'm using AppCompat and Toolbar.
当导航抽屉图标从汉堡包转换为箭头或反之时,我确保会有动画 .
我使用以下技术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>
-
当我按
action_search
时,搜索图标将扩展为AutoCompleteTextView
. At the same time, hamburger icon will animate to arrow icon -
当我按下箭头图标时,箭头图标将动画回汉堡包图标 .
我尝试通过在阅读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
时,汉堡包图标确实变为箭头图标 . 但是,没有动画 . 我可以知道有什么我错过了吗?
更新
我怀疑导航抽屉使用的左箭头与搜索操作视图使用的左箭头不同 . 它们是两个不同的实例 .
这是因为如果我单击 action_settings
,我可以观察动画 . 但是,如果我单击 action_search
,我将不会观察动画 . 我怀疑搜索操作视图使用左箭头,"block"导航抽屉的图标(包括汉堡包和箭头)
1 回答
我没有展开我自己的自定义SearchView就达到了这个效果 . 事实是,汉堡和后箭有两个单独的按钮 . 因此,虽然我们可以使用公共方法setHomeAsUpIndicator()访问汉堡按钮,但只能通过反射来访问后退按钮 . 然后我们也在它上面设置所需的drawable,并同时在两个按钮drawable上播放动画,而不管特定按钮是否是可见的 . 这适用于两个按钮完全位于布局中的相同位置 .
详情请见http://choruscode.blogspot.com/2015/09/morphing-animation-for-toolbar-back.html