[英]Changing Hamburger Action bar Icon to Rotating Arrow Not Working, when Opening the Navigation Drawer in Android
[英]Navigation drawer icons (hamburger & arrow) animation when expand action view
我正在使用AppCompat
和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
)時,我對執行動畫更感興趣,該按鈕用作工具欄的動作視圖。
<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" />
<?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
。 同時,漢堡圖標將動畫為箭頭圖標 我在閱讀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
,則不會觀察動畫。 我懷疑搜索操作視圖使用了向左箭頭,“阻止”了導航抽屜的圖標(漢堡包和箭頭)
我沒有展開自己的自定義SearchView,就實現了這種效果。 事實是,漢堡和后退箭頭確實有兩個單獨的按鈕。 因此,盡管我們可以使用公共方法setHomeAsUpIndicator()訪問“漢堡”按鈕,但只能通過反射來訪問“后退”按鈕。 然后,我們也只需在其上設置所需的可繪制對象,並在兩個按鈕可繪制對象上同時播放動畫,而不管特定按鈕是否可見。 這是因為兩個按鈕都完全位於布局中的同一位置。
有關詳細信息,請參見帖子http://choruscode.blogspot.com/2015/09/morphing-animation-for-toolbar-back.html
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.