实现效果如下:
点击4个按钮分别实现4个角度的旋转。
-
目标版本设置为21,这样不需要动态申请权限。
-
在清单文件申请CAMERA权限
-
布局
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <FrameLayout android:id="@+id/fl_container" android:layout_width="match_parent" android:layout_height="match_parent" /> <LinearLayout android:layout_width="match_parent" android:orientation="horizontal" android:layout_height="wrap_content" android:layout_alignParentBottom="true"> <View android:layout_width="6dp" android:layout_height="0dp"/> <Button android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:id="@+id/btn_0" android:onClick="onClick" android:text="0"/> <View android:layout_width="6dp" android:layout_height="0dp"/> <Button android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:id="@+id/btn_90" android:onClick="onClick" android:text="90"/> <View android:layout_width="6dp" android:layout_height="0dp"/> <Button android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:onClick="onClick" android:id="@+id/btn_180" android:text="180"/> <View android:layout_width="6dp" android:layout_height="0dp"/> <Button android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:onClick="onClick" android:id="@+id/btn_270" android:text="270"/> <View android:layout_width="6dp" android:layout_height="0dp"/> </LinearLayout> </RelativeLayout>
-
代码
class MainActivity : AppCompatActivity() { private var cameraView: FrameLayout? = null private var camera: Camera? = null private lateinit var cameraContainer: ViewGroup override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) cameraContainer = findViewById(R.id.fl_container) } override fun onResume() { super.onResume() startPreview() } override fun onPause() { super.onPause() stopPreview() } private fun startPreview() { val surfaceView = SurfaceView(this) surfaceView.holder.addCallback(object : SurfaceHolder.Callback { override fun surfaceCreated(holder: SurfaceHolder) { camera = Camera.open() camera?.setPreviewDisplay(holder) camera?.setDisplayOrientation(0) camera?.startPreview() } override fun surfaceDestroyed(holder: SurfaceHolder) { } override fun surfaceChanged(holder: SurfaceHolder, format: Int, width: Int, height: Int) {} }) cameraView = FrameLayout(this).apply { addView(surfaceView) } cameraContainer.addView(cameraView) } private fun stopPreview() { camera?.stopPreview() camera?.release() camera = null cameraView?.removeAllViews() (cameraView?.parent as? ViewGroup)?.removeAllViews() cameraView = null } fun onClick(view: View) { when (view.id) { R.id.btn_0 -> camera?.setDisplayOrientation(0) R.id.btn_90 -> camera?.setDisplayOrientation(90) R.id.btn_180 -> camera?.setDisplayOrientation(180) R.id.btn_270 -> camera?.setDisplayOrientation(270) } } }