中间凹陷的 BottomNavigationView

本文介绍如何创建一个中间凹陷效果的BottomNavigationView,包括XML布局、菜单设置和自定义GapNavigationView类的详细步骤。通过修改源码,避免中间按钮的点击事件,并分析BottomNavigationView的源码以理解其工作原理。
摘要由CSDN通过智能技术生成

中间凹陷的 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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值