中间凹陷的 BottomNavigationView(请滑倒最底部直接复制使用)
简书地址:https://www.jianshu.com/p/04c44a882332
直接上代码
注:使用时一定先指定Background为透明色
添加menu为奇数个,最中间item的icon title都为空
xml:
<?xml version="1.0" encoding="utf-8"?>
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00BCD4">
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:background="#00FFFFFF"
app:menu="@menu/navigation"
app:layout_constraintBottom_toBottomOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
Menu:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
android:id="@+id/navigation_home"
android:icon="@drawable/nav_selector_home"
android:title="首页" />
android:id="@+id/navigation_find"
android:icon="@drawable/nav_selector_find"
android:title="发现" />
android:id="@+id/navigation_null"
android:icon="@null"
android:title="@null"
/>
android:id="@+id/navigation_message"
android:icon="@drawable/nav_selector_message"
android:title="消息" />
android:id="@+id/navigation_mine"
android:icon="@drawable/nav_selector_mine"
android:title="我的" />
</menu>
GapNavigationView类:
注:需先自行导入 BottomNavigationView
public class GapNavigationView extends BottomNavigationView {
Contextcontext;
public GapNavigationView(Context context) {
super(context);
this.context = context;
}
public GapNavigationView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public GapNavigationView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
@SuppressLint("DrawAllocation")
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
setLayerType(View.LAYER_TYPE_SOFTWARE, null);
//将中间类圆区域间距设置为总高度的 3/4
int centerRadius = getHeight() *3/4;
//设置阴影大小
float shadowLength =5f;
//创建画笔
Paint paint =new Paint();
//画笔抗锯齿
paint.setAntiAlias(true);
//创建路径
Path path =new Path();
//开始画View
//将起点设置在阴影之下
path.moveTo(0, shadowLength);
//凹陷部分
path.lineTo(getWidth() /2f - centerRadius, shadowLength);
path.lineTo(getWidth()/2f - centerRadius/3f *2f ,shadowLength + centerRadius/4f);
path.lineTo(getWidth()/2f - centerRadius/4f ,shadowLength + centerRadius *3/4f);
path.lineTo(getWidth()/2f + centerRadius/4f ,shadowLength + centerRadius *3/4f);
path.lineTo(getWidth()/2f + centerRadius/3f *2f ,shadowLength + centerRadius/4f);
path.lineTo(getWidth()/2f + centerRadius,shadowLength);
//封闭区域
path.lineTo(getWidth(), shadowLength);
path.lineTo(getWidth(), getHeight());
path.lineTo(0, getHeight());
path.lineT