旋转CustomView而不影响活动 [英] Rotate CustomView without affecting the activity
问题描述
我有一个浮动窗口,其中我的浮动窗口充当视频,并且在视频内部还有一个控件,位于浮动窗口下方
Hi I have a floating window in which my floating window serves as the video and it also have a controls inside the video which under the floating window
现在我的问题是,有可能我只能在不影响活动方向的情况下从浮动窗口旋转自定义视图
Now my question is it possible that I can rotate my custom view from floating window only without affecting the orientation of my activity
如果有人已经尝试过,请指导我.
If someone already tried it please guide me towards it.
谢谢.
推荐答案
我以前从未做过.但是我有个主意.您可以使用以下代码获取设备上屏幕的当前方向.
I have never done it before. But I have an idea. You can use the below code to get the current orientation of the screen on your device.
OrientationEventListener onrientationEventListener = new OrientationEventListener(context, SensorManager.SENSOR_DELAY_UI) {
@Override
public void onOrientationChanged(int rotation) {
Logger.e("Orientation: " + rotation);
}
};
然后,根据旋转"的值,您可以在Android中使用旋转动画来旋转自定义视图.
And after that, depends on the value of "rotation", you can use rotate animation in Android to rotate your custom view.
@Beginer:这是我实现的代码.我在小型相机应用程序的自定义视图中使用了以上代码.它有助于我知道拍摄后应该旋转位图的度数. toScreenOrientation()
方法下面将返回以度(0,90,180,270)为单位的值.您还可以自己修改(无论您想要什么).
@Beginer: Here is the code I implemented it. I used the above code in my custom view in my small camera app. It helps me to know which degree should I rotate the bitmap after taken. The toScreenOrientation()
method bellow return a value in degrees (0, 90, 180, 270) you also modify it by yourself(whatever you want).
使用setOrientationChangedListener()
方法帮助父对象(活动,片段等)也接收回调.
Using setOrientationChangedListener()
method to help the parent(Activity, Fragment, etc.) receives a callback also.
public class TakePhotoView extends ConstraintLayout {
private static final int SCREEN_ORIENTATION_0 = 0;
private static final int SCREEN_ORIENTATION_90 = 90;
private static final int SCREEN_ORIENTATION_180 = 180;
private static final int SCREEN_ORIENTATION_270 = 270;
private OnOrientationChangedListener mOnOrientationChangedListener;
private OrientationEventListener mOrientationEventListener;
private int mScreenRotation;
public TakePhotoView(Context context) {
this(context, null);
}
public TakePhotoView(Context context, AttributeSet attrs) {
super(context, attrs);
//....do something here
mOrientationEventListener = new OrientationEventListener(context, SensorManager.SENSOR_DELAY_UI) {
@Override
public void onOrientationChanged(int rotation) {
Logger.e("Orientation: " + rotation);
if (rotation == OrientationEventListener.ORIENTATION_UNKNOWN) {
mScreenRotation = DEFAULT_SCREEN_ROTATION;
return;
}
mScreenRotation = rotation;
if(mOnOrientationChangedListener != null){
mOnOrientationChangedListener.onOrientationChanged(rotation);
}
}
};
}
private void takePhoto(Camera camera) {
if (camera != null) {
camera.takePicture(null, null, mPictureCallback);
}
}
private Camera.PictureCallback mPictureCallback = new Camera.PictureCallback() {
@Override
public void onPictureTaken(byte[] data, Camera camera) {
final int screenRotation = mScreenRotation;
int rotate = toScreenOrientation(screenRotation);
// Rotate/Flip the bitmap depends on the 'rotate' value
}
};
/**
* Converts sensor rotation in degrees to screen orientation constants.
*
* @param rotation sensor rotation angle in degrees
* @return Screen orientation angle in degrees (0, 90, 180, 270).
*/
private int toScreenOrientation(int rotation) {
if (rotation > 290 || rotation <= 70) {
return SCREEN_ORIENTATION_0;
} else if (rotation > 70 && rotation <= 110) {
return SCREEN_ORIENTATION_90;
} else if (rotation > 110 && rotation <= 250) {
return SCREEN_ORIENTATION_180;
} else {
return SCREEN_ORIENTATION_270;
}
}
public void setOrientationChangedListener(OnOrientationChangedListener orientationChangedListener){
this.mOnOrientationChangedListener = orientationChangedListener;
}
////////////////////////////////////////////////////////////////////////////////////////////////
// Interfaces
////////////////////////////////////////////////////////////////////////////////////////////////
public interface OnOrientationChangedListener {
void onOrientationChanged(int rotation);
}
}
即使禁用清单中的旋转功能,我也可以使用此侦听器吗? ->仍然可以正常工作.
Can I use this listener even I disable rotation in my manifest? -> It still works fine.
希望它会有所帮助.
这篇关于旋转CustomView而不影响活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!