我已经使用 TextureView 在 android 应用程序中预览图像捕获。这可以正常工作,但我正在尝试向用户提供已捕获图像的反馈。我想暂时隐藏图像预览,然后恢复正常预览以提供反馈。
我尝试将空白图像推送到 TextureView 失败,并且我也未能使用 setVisibility() 属性(INVISIBLE 和 GONE)暂时隐藏 TextureView 本身 - 此属性没有可见效果。我还能如何实施此反馈?
我已经使用 TextureView 在 android 应用程序中预览图像捕获。这可以正常工作,但我正在尝试向用户提供已捕获图像的反馈。我想暂时隐藏图像预览,然后恢复正常预览以提供反馈。
我尝试将空白图像推送到 TextureView 失败,并且我也未能使用 setVisibility() 属性(INVISIBLE 和 GONE)暂时隐藏 TextureView 本身 - 此属性没有可见效果。我还能如何实施此反馈?
与 Lukas 的回答类似,我在纹理视图顶部也有一个全黑视图。当用户按下快门按钮时,这个黑色视图会淡入然后淡出。我发现这个动画更接近手机原生相机的真实快门效果。
<TextureView
android:id="@+id/view_finder"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
<View
android:id="@+id/camera_overlay"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="@color/black"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
view.captureButton.setOnClickListener {
val fadeOutAnimation = AlphaAnimation(1.0f, 0.0f).apply {
duration = 250
setAnimationListener(object : Animation.AnimationListener {
override fun onAnimationRepeat(animation: Animation?) {
}
override fun onAnimationEnd(animation: Animation?) {
view.captureOverlay.alpha = 0.0f
view.captureOverlay.isVisible = false
}
override fun onAnimationStart(animation: Animation?) {
}
})
}
val fadeInAnimation = AlphaAnimation(0.0f, 1.0f).apply {
duration = 20
setAnimationListener(object : Animation.AnimationListener {
override fun onAnimationRepeat(animation: Animation?) {
}
override fun onAnimationEnd(animation: Animation?) {
view.captureOverlay.alpha = 1.0f
Handler(Looper.getMainLooper()).postDelayed({
view.captureOverlay.startAnimation(fadeOutAnimation)
}, 20)
}
override fun onAnimationStart(animation: Animation?) {
view.captureOverlay.isVisible = true
}
})
}
view.captureOverlay.startAnimation(fadeInAnimation)
/**
* Capture image here
*/
}
我通过使用一个简单的视图来做到这一点,该视图与默认情况下不可见的相机预览完全相同,并在拍摄照片时显示 100 毫秒。
camera?.takePicture(shutterCallback, null, takePictureCallback)
private val shutterCallback = Camera.ShutterCallback {
shutterEffectView.setVisible()
val handler = Handler()
val runnable = Runnable { shutterEffectView.setGone() }
handler.postDelayed(runnable, 100)
}
这段代码是 kotlin,我使用的是快门回调,因为我使用的是旧的相机 API。使用 camera2 API,我认为您必须使用此回调