material menu
2.0.0
任何問題或拉取請求都不會解決。如果需要更改,請考慮分叉您自己的版本。
變形 Android 選單、返回、關閉和檢查按鈕
完全控制動畫:
compile ' com.balysv.materialmenu:material-menu:2.0.0 '
請參閱自述文件以設定舊版本的庫。
將其用作Toolbar
列中的獨立繪圖:
private MaterialMenuDrawable materialMenu ;
protected void onCreate ( Bundle savedInstanceState ) {
super . onCreate ( savedInstanceState );
setContentView ( R . layout . toolbar );
Toolbar toolbar = ( Toolbar ) findViewById ( R . id . toolbar );
setSupportActionBar ( toolbar );
toolbar . setNavigationOnClickListener ( new View . OnClickListener () {
@ Override public void onClick ( View v ) {
// Handle your drawable state here
materialMenu . animateState ( newState );
}
});
materialMenu = new MaterialMenuDrawable ( this , Color . WHITE , Stroke . THIN );
toolbar . setNavigationIcon ( materialMenu );
}
一個普通的舊View
,用於繪製圖標並提供 API 來操作其狀態。您可以將其嵌入到任何佈局中,包括Toolbar
。
也可以透過 xml 屬性進行自訂:
app:mm_color="color" // Color of drawable
app:mm_visible="boolean" // Visible
app:mm_transformDuration="integer" // Transformation animation duration
app:mm_scale="integer" // Scale factor of drawable
app:mm_strokeWidth="integer" // Stroke width of icons (can only be 1, 2 or 3)
app:mm_rtlEnabled="boolean" // Enabled RTL layout support (flips all drawables)
app:mm_iconState="enum" // Set the intial state of the drawable (burger, arrow, x or check)
有四種圖標狀態:
BURGER , ARROW , X , CHECK
改變可繪製狀態
MaterialMenu . animateIconState ( IconState state )
變更可繪製狀態而不使用動畫
MaterialMenu . setIconState ( IconState state )
若要手動為可繪製物件設定動畫(即在導覽抽屜投影片上):
MaterialMenu . setTransformationOffset ( AnimationState state , float value )
隱藏或顯示可繪製物件:
MaterialMenu . setVisible ( boolean visible )
其中AnimationState
是BURGER_ARROW, BURGER_X, ARROW_X, ARROW_CHECK, BURGER_CHECK, X_CHECK
之一, value
介於0
和2
之間
注意:圖示狀態由目前偏移值決定。確保對向前動畫使用0
到1
之間的offset
,對向後動畫使用1
和2
之間的偏移量,以便在活動重新建立時正確儲存圖示狀態。
如上所述,在 ActionBar 中實作MaterialMenu
並新增自訂DrawerListener
:
private DrawerLayout drawerLayout ;
private boolean isDrawerOpened ;
private MaterialMenuDrawable materialMenu ;
@ Override
protected void onCreate ( Bundle savedInstanceState ) {
super . onCreate ( savedInstanceState );
toolbar = ( Toolbar ) findViewById ( R . id . toolbar );
setSupportActionBar ( toolbar );
materialMenu = new MaterialMenuDrawable ( this , Color . WHITE , Stroke . THIN );
toolbar . setNavigationIcon ( materialMenu );
drawerLayout = ( DrawerLayout ) findViewById ( R . id . drawer_layout );
drawerLayout . setDrawerListener ( new DrawerLayout . SimpleDrawerListener () {
@ Override
public void onDrawerSlide ( View drawerView , float slideOffset ) {
materialMenu . setTransformationOffset (
MaterialMenuDrawable . AnimationState . BURGER_ARROW ,
isDrawerOpened ? 2 - slideOffset : slideOffset
);
}
@ Override
public void onDrawerOpened ( View drawerView ) {
isDrawerOpened = true ;
}
@ Override
public void onDrawerClosed ( View drawerView ) {
isDrawerOpened = false ;
}
@ Override
public void onDrawerStateChanged ( int newState ) {
if ( newState == DrawerLayout . STATE_IDLE ) {
if ( isDrawerOpened ) {
menu . setIconState ( MaterialMenuDrawable . IconState . ARROW );
} else {
menu . setIconState ( MaterialMenuDrawable . IconState . BURGER );
}
}
}
});
}
巴利斯·瓦倫圖克維修斯
Copyright 2016 Balys Valentukevicius
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.