问题描述
在android-7.1.1_r12
api中,android.hardware.camera2
使用 StickyBottomCaptureLayout
作为 "BottomBar"
以显示操作按钮(如开关相机,快门和最近的图片按钮).无论设备方向如何,此StickyBottomCaptureLayout
始终显示在系统栏的上方/附近(该菜单栏具有后退",主页"和其他应用程序按钮).
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).
例如,这是旋转度为0
或180
时的外观:
For example, this is what it looks when the rotation degree is 0
or 180
:
然后,通过定向设备并将其旋转度设置为90
或270
,StickyBottomCaptureLayout
现在位于系统栏附近:
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...
-
我首先尝试使用
layout-land
设置不同的布局,但是没有运气!我无法更改默认的从左到右方向,并使底部的条形图固定在270
度上的Android系统栏上.
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 on270
degrees.
我无法扩展这些小部件,但是我尝试重现这种情况.例如,我有两种布局:
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.
有人更改设备方向(始终在Android系统栏上方或附近)时,是否有解决方案来重现底部栏的情况?请记住,我的最低SDK低于21
,因此我无法访问android.hardware.camera2
api.
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.
推荐答案
我刚刚找到了解决方案:我需要为每个布局设置一个RectF
,并为其设置正确的坐标. 在源类中,我设法做到了:
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()
}
这按预期工作!
我找到了一种方法来再现平滑的方向,例如使用此答案的原生camera2
应用.使用方向侦听器和configChanges
选项,我可以在没有默认旋转动画的情况下重新组织视图.我在onLayout()
中设置适当的位置,然后调用invalidate()
. ;)
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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!