简体   繁体   English

自定义底部导航视图

[英]Customize Bottom Navigation View

I have developed an android application using OpenCV. 我已经使用OpenCV开发了一个android应用程序。 The user interface has a bottom navigation view. 用户界面具有底部导航视图。 When I pressed items in the Bottom Navigation View it applies different filters to the JavaCameraView in real time. 当我在底部导航视图中按下项目时,它将实时将不同的过滤器应用于JavaCameraView。

My problem is that the bottom navigation looks flat. 我的问题是底部导航看起来很平坦。 I want to make the items like buttons, elevated from their positions, so they are not in the same plane as the rest of the User Interface. 我想使按钮之类的项目从其位置抬高,因此它们与用户界面的其余部分不在同一平面上。

I am aware of the Bottom sheet, but that won't help me. 我知道底页,但这对我没有帮助。 I have already fully developed the app and finalized the design. 我已经完全开发了该应用程序并完成了设计。 I can't use any View other than Bottom Navigation View. 除底部导航视图外,我无法使用任何其他视图。

Is there any XML attribute or any method I can use on the BottomNavigationView object to make the Items elevated from their positions? 我可以在BottomNavigationView对象上使用任何XML属性或任何方法来使Item从其位置抬高吗?

The navigation view: 导航视图:

<android.support.design.widget.BottomNavigationView
  android:id="@+id/navigation"
  android:layout_width="match_parent"
  android:layout_height="45dp"
  android:layout_gravity="start"
  app:itemTextColor="@color/cardview_light_background"
  android:background="?android:attr/windowBackground"
  app:menu="@menu/nav" />

The item code: 物品代码:

<?xml version="1.0" encoding="utf-8"?>
  <menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <item android:id="@+id/fiter1" android:title="Filter 1" />
    <item android:id="@+id/filter2" android:title="Filter 2" />
    <item android:id="@+id/filter3" android:title="Filter 3" />
  </menu>

Using Customized class we can Draw Curved custom shapes in Curved Bottom Navigation View. 使用Customized类,我们可以在“曲线底部导航视图”中绘制“曲线”自定义形状。 By using Below XML 通过使用以下XML

<android.tutorial.curvedbottombar.CurvedBottomNavigationView
            android:id="@+id/customBottomBar"
            android:layout_width="match_parent"
            android:layout_height="62dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent" />

For Java class is 对于Java类是

public class CurvedBottomNavigationView extends BottomNavigationView {
    private Path mPath;
    private Paint mPaint;


    /** the CURVE_CIRCLE_RADIUS represent the radius of the fab button */
    public final int CURVE_CIRCLE_RADIUS = 256 / 3;
    // the coordinates of the first curve
    public Point mFirstCurveStartPoint = new Point();
    public Point mFirstCurveEndPoint = new Point();
    public Point mFirstCurveControlPoint2 = new Point();
    public Point mFirstCurveControlPoint1 = new Point();

    //the coordinates of the second curve
    @SuppressWarnings("FieldCanBeLocal")
    public Point mSecondCurveStartPoint = new Point();
    public Point mSecondCurveEndPoint = new Point();
    public Point mSecondCurveControlPoint1 = new Point();
    public Point mSecondCurveControlPoint2 = new Point();
    public int mNavigationBarWidth;
    public int mNavigationBarHeight;

    public CurvedBottomNavigationView(Context context) {
        super(context);
        init();
    }

    public CurvedBottomNavigationView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public CurvedBottomNavigationView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        mPath = new Path();
        mPaint = new Paint();
        mPaint.setStyle(Paint.Style.FILL_AND_STROKE);
        mPaint.setColor(Color.WHITE);
        setBackgroundColor(Color.TRANSPARENT);
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);

    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        // get width and height of navigation bar
        // Navigation bar bounds (width & height)
         mNavigationBarWidth = getWidth();
         mNavigationBarHeight = getHeight();
        // the coordinates (x,y) of the start point before curve
        mFirstCurveStartPoint.set((mNavigationBarWidth / 2) - (CURVE_CIRCLE_RADIUS * 2) - (CURVE_CIRCLE_RADIUS / 3), 0);
        // the coordinates (x,y) of the end point after curve
        mFirstCurveEndPoint.set(mNavigationBarWidth / 2, CURVE_CIRCLE_RADIUS + (CURVE_CIRCLE_RADIUS / 4));
        // same thing for the second curve
        mSecondCurveStartPoint = mFirstCurveEndPoint;
        mSecondCurveEndPoint.set((mNavigationBarWidth / 2) + (CURVE_CIRCLE_RADIUS * 2) + (CURVE_CIRCLE_RADIUS / 3), 0);

        // the coordinates (x,y)  of the 1st control point on a cubic curve
        mFirstCurveControlPoint1.set(mFirstCurveStartPoint.x + CURVE_CIRCLE_RADIUS + (CURVE_CIRCLE_RADIUS / 4), mFirstCurveStartPoint.y);
        // the coordinates (x,y)  of the 2nd control point on a cubic curve
        mFirstCurveControlPoint2.set(mFirstCurveEndPoint.x - (CURVE_CIRCLE_RADIUS * 2) + CURVE_CIRCLE_RADIUS, mFirstCurveEndPoint.y);

        mSecondCurveControlPoint1.set(mSecondCurveStartPoint.x + (CURVE_CIRCLE_RADIUS * 2) - CURVE_CIRCLE_RADIUS, mSecondCurveStartPoint.y);
        mSecondCurveControlPoint2.set(mSecondCurveEndPoint.x - (CURVE_CIRCLE_RADIUS + (CURVE_CIRCLE_RADIUS / 4)), mSecondCurveEndPoint.y);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        mPath.reset();
        mPath.moveTo(0, 0);
        mPath.lineTo(mFirstCurveStartPoint.x, mFirstCurveStartPoint.y);

        mPath.cubicTo(mFirstCurveControlPoint1.x, mFirstCurveControlPoint1.y,
                mFirstCurveControlPoint2.x, mFirstCurveControlPoint2.y,
                mFirstCurveEndPoint.x, mFirstCurveEndPoint.y);

        mPath.cubicTo(mSecondCurveControlPoint1.x, mSecondCurveControlPoint1.y,
                mSecondCurveControlPoint2.x, mSecondCurveControlPoint2.y,
                mSecondCurveEndPoint.x, mSecondCurveEndPoint.y);

        mPath.lineTo(mNavigationBarWidth, 0);
        mPath.lineTo(mNavigationBarWidth, mNavigationBarHeight);
        mPath.lineTo(0, mNavigationBarHeight);
        mPath.close();

        canvas.drawPath(mPath, mPaint);
    }
}

You can Download complete Source Code from Here 您可以从此处下载完整的源代码

在此处输入图片说明

As elevation is not supported by menu items, You can design your icons and provide some highlights and shadows as mentioned in android UI guideline so they look little elevated. 由于菜单项不支持高程,因此您可以设计图标并提供一些高亮和阴影,如android UI指南中所述,这样它们看起来就很少升高了。 Refer this andorid guide on how to design menu icons. 有关如何设计菜单图标的信息,请参阅本ororid指南。 https://developer.android.com/guide/practices/ui_guidelines/icon_design_menu https://developer.android.com/guide/practices/ui_guidelines/icon_design_menu

Check this image for understanding. 检查此图像以了解。 在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM