谷歌最新发布的CameraX实现预览和拍照非常方便,本文主要介绍CameraX拍照之后取景框裁切的问题。(屏幕默认为竖屏)
实现预览和拍照效果,代码非常简单
xml布局
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".FullScreenClipActivity"> <androidx.camera.view.PreviewView android:id="@+id/pv" android:layout_width="match_parent" android:layout_height="match_parent"/> </RelativeLayout>
代码实现
mPreview = findViewById(R.id.pv); mPreview.post(new Runnable() { @Override public void run() { //保证mPreView和预览拍照的比例一直才能保证预览的效果和拍照之后的效果一致 ratio=getRatio(); startCamera(