本文介绍了如何在camera2 Android api中创建一个BottomBar作为StickyBottomCaptureLayout?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

上下文:

android-7.1.1_r12 api 中,android.hardware.camera2 使用

并且,通过定位设备并获得 90270 的旋转度,StickyBottomCaptureLayout 现在靠近系统栏:

通常,上面的截图应该在左边有粘性栏,在右边有摄像头...

尝试:

  1. 我首先尝试使用 layout-land 设置不同的布局,但没有成功!我无法更改默认的从左到右的方向并使底部栏以 270 度粘在 Android 系统栏上.

  2. 我无法扩展 这些小部件,但我试图重现这个案例.例如,我有两个布局:

    <ViewGroup .../>//包含上面的视图<StickyBottomBar .../>//与 StickyBottomCaptureLayout 相同的行为</FrameLayout>

    在每次方向更改时,我都会获取旋转的设备并为上部布局和粘滞条设置正确的重力,如下所示:

    if (rotation == 0) {//视图重力 = TOP//粘性重力 = 底部} else if (旋转== 90) {//视图重力 = LEFT//粘性重力 = 正确} else if (旋转== 180) {//视图重力 = BOTTOM//粘性重力 = TOP}否则如果(旋转== 270){//视图重力 = RIGHT//粘性重力 = LEFT}

然而,这根本不起作用.我不知道这些小部件是如何让它正常工作的.

问题:

当设备方向改变时(总是在Android系统栏上方或附近),有人有解决方案来重现底栏的情况吗?请记住,我的最低 SDK 低于 21,因此我无法访问 android.hardware.camera2 api.

解决方案

我刚刚找到了解决方案:我需要为每个布局设置一个 RectF 并为它们设置正确的坐标.从源类,我设法做到这一点:

@Overrideprotected void onLayout(boolean 改变,int left,int top,int right,int bottom) {//从上下文 Surface.ROTATION 旋转int 度 = getDeviceRotation(getContext());//粘性底部栏的大小int offsetSize = (int) getResources().getDimension(R.dimen.sticky_bar_default_size);//布局粘性底栏RectF bottomBarRect = getStickyBarRect(degrees, offsetSize);mBottomBar.layout((int)bottomBarRect.left, (int)bottomBarRect.top,(int)bottomBarRect.right, (int)bottomBarRect.bottom);//布局上视图RectF upperViewRect = getUpperViewRect(degrees, offsetSize);mUpperView.layout(...);//和上面一样的逻辑无效();}//获取坐标位置以设置粘性底部栏私有 RectF getStickyBarRect(int 度数,int offsetSize) {浮动左 = 0,顶部 = 0,右 = 0,底部 = 0;int width = getWidth();int height = getHeight();if (degrees == 0) {//底部粘滞条顶部 = 高度 - 偏移量;右 = 宽度;底部 = 高度;} else if (degrees == 90) {//粘滞条在右边左 = 宽度 - 偏移量;右 = 宽度;底部 = 高度;} else if (degrees == 180) {//顶部粘滞条右 = 宽度;底部 = 高度 - 偏移量;} else if (degrees == 270) {//左侧粘滞条右 = 偏移量;底部 = 高度;}返回新的 RectF(左、上、右、下);}//获取坐标位置以设置上视图私有 RectF getUpperViewRect(int 度数,int offsetSize) {//与 getStickyBarRect() 相同的逻辑}

这按预期工作!

我找到了一种方法来重现平滑方向,就像使用 这个答案camera2 应用程序一样>.使用方向侦听器和 configChanges 选项,我可以在没有默认旋转动画的情况下重新组织视图.我在 onLayout() 中设置适当的位置并调用 invalidate().;)

Context:

In the android-7.1.1_r12 api, the android.hardware.camera2 uses a StickyBottomCaptureLayout as a "BottomBar" to display the action buttons (as switch-camera, shutter and recent picture buttons). Whatever the device orientation, this StickyBottomCaptureLayout is displayed always above/near the system bar (which has back, home and other apps buttons).

For example, this is what it looks when the rotation degree is 0 or 180:

And, by orienting the device and get a rotation degree as 90 or 270, the StickyBottomCaptureLayout is now near the system bar:

Usually, this above screenshot should has the sticky bar on the left and camera on the right...

Tries:

  1. I firstly tried to set different layout with layout-land, but no luck! I cannot change the default left-to-right orientation and get the bottom bar sticks to the Android system bar on 270 degrees.

  2. I cannot extend these widgets, but I tried to reproduce the case. For example, I got two layouts:

    <FrameLayout>
        <ViewGroup .../> // containing the upper views
    
        <StickyBottomBar .../> // same behavior as StickyBottomCaptureLayout
    </FrameLayout>
    

    On each orientation changes, I get the rotation's device and set the correct gravity for upper layout and the sticky bar, something as follows:

    if (rotation == 0) {
        // views gravity = TOP
        // sticky gravity = BOTTOM
    } else if (rotation == 90) {
        // views gravity = LEFT
        // sticky gravity = RIGHT
    } else if (rotation == 180) {
        // views gravity = BOTTOM
        // sticky gravity = TOP
    } else if (rotation == 270) {
        // views gravity = RIGHT
        // sticky gravity = LEFT
    }
    

However, this is not working at all. I don't know how these widgets make it work properly.

Question:

Does someone has a solution to reproduce the case of the bottom bar when the device orientation is changing (always above or near the Android system bar)? Keeping in mind that my minimum SDK is below 21, so I have no access to android.hardware.camera2 api.

解决方案

I just found the solution: I need to set a RectF for each layout and set them with the right coordinates. From the source class, I manage to do this:

@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
    // rotation from context Surface.ROTATION
    int degrees = getDeviceRotation(getContext());
    // size of the sticky bottom bar
    int offsetSize = (int) getResources().getDimension(R.dimen.sticky_bar_default_size);

    // layout sticky bottom bar
    RectF bottomBarRect = getStickyBarRect(degrees, offsetSize);
    mBottomBar.layout((int) bottomBarRect.left, (int) bottomBarRect.top,
            (int) bottomBarRect.right, (int) bottomBarRect.bottom);

    // layout upper view
    RectF upperViewRect = getUpperViewRect(degrees, offsetSize);
    mUpperView.layout(...); // same logic as above

    invalidate();
}

// Gets the coordinates positions to set the Sticky Bottom Bar
private RectF getStickyBarRect(int degrees, int offsetSize) {
    float left = 0, top = 0, right = 0, bottom = 0;
    int width = getWidth();
    int height = getHeight();

    if (degrees == 0) { // stickybar at bottom
        top = height - offsetSize;
        right = width;
        bottom = height;
    } else if (degrees == 90) { // stickybar at right
        left = width - offsetSize;
        right = width;
        bottom = height;
    } else if (degrees == 180) { // stickybar at top
        right = width;
        bottom = height - offsetSize;
    } else if (degrees == 270) { // stickybar at left
        right = offsetSize;
        bottom = height;
    }

    return new RectF(left, top, right, bottom);
}

// Gets the coordinates positions to set the upper views
private RectF getUpperViewRect(int degrees, int offsetSize) {
    // same logic as getStickyBarRect()
}

And this works as expected!


I found a way to reproduce the smooth orientation like the native camera2 app with this answer. Using an orientation listener and the configChanges options, I'm able to reorganize the views without the default rotation animation. I set the proper positions in onLayout() and call invalidate(). ;)

这篇关于如何在camera2 Android api中创建一个BottomBar作为StickyBottomCaptureLayout?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-25 17:50