簡體   English   中英

Android:僅在相機預覽中旋轉疊加按鈕

[英]Android: Rotate only overlay Buttons on camera preview

我有一個使用LinearLayout作為主要布局的Android應用程序,並通過相機預覽填充了SurfaceView。 在此,我為另一個LinearLayout填充了三個Button和一個自定義TextView。 我希望攝像機預覽始終保持橫向,並且覆蓋布局會根據設備的方向進行更改。

我嘗試在活動清單中設置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)
        {
            ...
        }
    });
}

任何有關如何完成此操作的想法將不勝感激!

您可以使用自定義方向事件監聽器來獲取方向並根據您的界面進行設置。

首先,創建一個類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);


}

然后將以下行添加到活動標記下的Android清單中

  android:configChanges="orientation|keyboardHidden|screenSize"

確保您沒有在清單中為此活動添加 android:screenOrientation屬性。

然后在相機活動的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();
  }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM