Android:仅在相机预览中旋转叠加按钮

标签 android camera android-orientation

我有一个 Android 应用程序,它使用 LinearLayout 作为主布局,并带有一个由相机预览填充的 SurfaceView。 在这里,我用三个按钮和一个自定义 TextView 膨胀另一个 LinearLayout。我希望相机预览始终保持横向,并且覆盖布局会根据设备方向发生变化。

我尝试在 Activity 的 list 中设置 android:screenOrientation="landscape" 但是(当然)膨胀的布局也始终保持固定,而不设置 android:screenOrientation 属性还有相机预览旋转,减慢应用程序并显示奇怪的预览形状因素。这里是布局的相关代码:

private void setupLayout()
{
    setContentView(R.layout.main);
    getWindow().setFormat(PixelFormat.UNKNOWN);

    // Release camera if owned by someone else
    if (camera != null)
        releaseCamera();

    surfaceView = (SurfaceView) findViewById(R.id.camerapreview);
    surfaceHolder = surfaceView.getHolder();
    surfaceHolder.addCallback(this);
    surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

    controlInflater = LayoutInflater.from(getBaseContext());
    View viewControl = controlInflater.inflate(R.layout.control, null);
    LayoutParams layoutParamsControl = new LayoutParams(
            LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
    this.addContentView(viewControl, layoutParamsControl);


    buttonGetCollectingData = (Button) findViewById(R.id.getcolldata);
    buttonGetCollectingData.setOnClickListener(new Button.OnClickListener()
    {
        public void onClick(View arg0)
        {
            ...
        }
    });

    btnBackHome = (Button) findViewById(R.id.btnBackHome);
    btnBackHome.setOnClickListener(new Button.OnClickListener()
    {
        public void onClick(View arg0)
        {
            ...
        }
    });

    autoFitTextViewMainMsg = (AutoFitTextView) findViewById(R.id.autoFitTextViewMainMsg);

    buttonTakePicture.setOnClickListener(new Button.OnClickListener()
    {

        public void onClick(View arg0)
        {
            ...
        }
    });
}

任何关于如何实现这一点的想法将不胜感激!

最佳答案

您可以使用自定义方向事件监听器来获取方向并根据需要设置您的 UI。

首先,创建一个类CustomOrientationEventListener

public abstract class CustomOrientationEventListener  extends OrientationEventListener {


private static final String TAG = "CustomOrientationEvent";
private int prevOrientation = OrientationEventListener.ORIENTATION_UNKNOWN;
private Context context;
private final int ROTATION_O    = 1;
private final int ROTATION_90   = 2;
private final int ROTATION_180  = 3;
private final int ROTATION_270  = 4;
private int rotation = 0;

public CustomOrientationEventListener(Context context) {
    super(context);
    this.context = context;
}

@Override
public void onOrientationChanged(int orientation) {

    if (android.provider.Settings.System.getInt(context.getContentResolver(), Settings.System.ACCELEROMETER_ROTATION, 0) == 0) // 0 = Auto Rotate Disabled
        return;
    int currentOrientation = OrientationEventListener.ORIENTATION_UNKNOWN;
    if (orientation >= 340 || orientation < 20 && rotation != ROTATION_O) {
        currentOrientation = Surface.ROTATION_0;
        rotation = ROTATION_O;

    } else if (orientation >= 70 && orientation < 110 && rotation != ROTATION_90) {
        currentOrientation = Surface.ROTATION_90;
        rotation = ROTATION_90;

    } else if (orientation >= 160 && orientation < 200 && rotation != ROTATION_180) {
        currentOrientation = Surface.ROTATION_180;
        rotation = ROTATION_180;

    } else if (orientation >= 250 && orientation < 290 && rotation != ROTATION_270) {
        currentOrientation = Surface.ROTATION_270;
        rotation = ROTATION_270;
    }

    if (prevOrientation != currentOrientation && orientation != OrientationEventListener.ORIENTATION_UNKNOWN) {
            prevOrientation = currentOrientation;
            if (currentOrientation != OrientationEventListener.ORIENTATION_UNKNOWN) {
                onSimpleOrientationChanged(rotation);
        }
    }
}

public abstract void onSimpleOrientationChanged(int orientation);


}

然后将以下行添加到您的 Activity 标签下的 Android list

  android:configChanges="orientation|keyboardHidden|screenSize"

确保您不添加android:screenOrientation 属性到该 Activity 的 list 中。

然后在相机 Activity 的 onCreate 中使用 CustomOrientationEventListener

public class YourActivity extends AppCompatActivity {

  private CustomOrientationEventListener customOrientationEventListener;

  final int ROTATION_O    = 1;
  final int ROTATION_90   = 2;
  final int ROTATION_180  = 3;
  final int ROTATION_270  = 4;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);

       .....

      customOrientationEventListener = new 
 CustomOrientationEventListener(getBaseContext()) {
          @Override
          public void onSimpleOrientationChanged(int orientation) {
              switch(orientation){
                  case ROTATION_O:
                      //rotate as on portrait
                 yourButton.animate().rotation(0).setDuration(500).start();
                      break;
                  case ROTATION_90:
                      //rotate as left on top
                 yourButton.animate().rotation(-90).setDuration(500).start();
                      break;
                  case ROTATION_270:
                      //rotate as right on top
                 yourButton.animate().rotation(90).setDuration(500).start();
                      break;
                  case ROTATION_180:
                      //rotate as upside down
                 yourButton.animate().rotation(180).setDuration(500).start();
                      break;

              }
          }
      };

  }

  @Override
  protected void onResume() {
      super.onResume();
      setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
      customOrientationEventListener.enable();
  }

  @Override
  protected void onPause() {
      super.onPause();
      customOrientationEventListener.disable();
  }

  @Override
  protected void onDestroy() {
      super.onDestroy();
      customOrientationEventListener.disable();
  }
}

关于Android:仅在相机预览中旋转叠加按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16733307/

相关文章:

android - 有没有办法对 setRotation 进行动画处理,或者应该将其替换为 RotateAnimation?

android - 如何使 ViewPager 的项目在方向改变时被销毁?

android - 处理 android 中的运行时方向变化

Android 相机无法从后台服务拍照

java - 2D游戏相机逻辑

android - TargetApi 注释不是 api 级别 16(Android 4.1)的一部分吗?

java - 安卓UDP丢包

android - 如何编写一个使用 Context 从 Android 框架中检索数据的用例

android - LinkedIn 抄写员 OAuth 库无法允许访问 LinkedIn 帐户

swift - 从自定义相机获取深度数据