我知道 CameraX 有点新,但它允许缩放吗?
我不是指在拍照之前直接在屏幕上(TextureView)放大图像吗?
我知道 CameraX 有点新,但它允许缩放吗?
我不是指在拍照之前直接在屏幕上(TextureView)放大图像吗?
他们最近添加了同样的支持。它将包含在下一个版本中。
假设这些依赖项:
dependencies {
implementation "androidx.camera:camera-core:1.0.0-alpha08"
implementation "androidx.camera:camera-camera2:1.0.0-alpha08"
implementation "androidx.camera:camera-lifecycle:1.0.0-alpha02"
implementation "androidx.camera:camera-view:1.0.0-alpha05"
implementation "androidx.camera:camera-extensions:1.0.0-alpha05"
}
这将是CameraFragment.kt
图书馆的1.0.0-alpha08
:
class CameraFragment: Fragment(), View.OnTouchListener, ScaleGestureDetector.OnScaleGestureListener {
private lateinit var scaleDetector: ScaleGestureDetector
private lateinit var container: ConstraintLayout
private lateinit var previewView: PreviewView
private lateinit var cameraControl: CameraControl
private lateinit var cameraInfo: CameraInfo
private var lastScaleFactor = 0f
...
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
scaleDetector = ScaleGestureDetector(context, this);
...
}
@SuppressLint("MissingPermission", "ClickableViewAccessibility")
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
container = view as ConstraintLayout
previewView = container.findViewById(R.id.previewView)
previewView.setOnTouchListener(this)
...
}
@SuppressLint("ClickableViewAccessibility")
override fun onTouch(view: View?, event: MotionEvent?): Boolean {
scaleDetector.onTouchEvent(event)
return true
}
override fun onScaleBegin(detector: ScaleGestureDetector?): Boolean {
return true
}
override fun onScaleEnd(detector: ScaleGestureDetector?) {
}
override fun onScale(detector: ScaleGestureDetector?): Boolean {
val zoomRatio: Float? = cameraInfo.zoomRatio.value?.toFloat()
val minZoomRatio: Float? = cameraInfo.minZoomRatio.value?.toFloat()
val maxZoomRatio: Float? = cameraInfo.maxZoomRatio.value?.toFloat()
val scaleFactor = scaleDetector.getScaleFactor()
if (lastScaleFactor == 0f || (Math.signum(scaleFactor) == Math.signum(lastScaleFactor))) {
cameraControl.setZoomRatio(Math.max(minZoomRatio!!, Math.min(zoomRatio!! * scaleFactor, maxZoomRatio!!)))
lastScaleFactor = scaleFactor
} else {
lastScaleFactor = 0f
}
return true
}
}
CameraXBasic/1.0.0-alpha08已经有PreviewView
.
捏放大:
val camera = cameraProvider.bindToLifecycle(...
val scaleGestureDetector = ScaleGestureDetector(this,
object : ScaleGestureDetector.SimpleOnScaleGestureListener() {
override fun onScale(detector: ScaleGestureDetector): Boolean {
val scale = camera.cameraInfo.zoomState.value!!.zoomRatio * detector.scaleFactor
camera.cameraControl.setZoomRatio(scale)
return true
}
})
previewView.setOnTouchListener { view, event ->
view.performClick()
scaleGestureDetector.onTouchEvent(event)
return@setOnTouchListener true
}