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.