我正在构建一个自动对焦的定制相机,只是想知道是否有一种方法可以调用与原生相机相同的自动对焦矩形指示器,或者我必须从头开始构建它。如果有任何示例或教程链接,将不胜感激。
我正在构建一个自动对焦的定制相机,只是想知道是否有一种方法可以调用与原生相机相同的自动对焦矩形指示器,或者我必须从头开始构建它。如果有任何示例或教程链接,将不胜感激。
看一下最新的Jelly Bean 4.2相机是如何处理这个问题的可能会有所帮助。您可以按照以下方式下载相机源代码:
git clone https://android.googlesource.com/platform/packages/apps/Camera.git
一旦您拥有代码,请导航到FocusOverlayManager
类和PieRenderer
类。如果您之前没有尝试过此最新版本,则焦点仪表是一个像馅饼一样的圆圈,在焦点完成后会旋转。您可以在Photoshop中制作自己的正方形,或者使用我过去使用过的这两个中的一个(一个是我制作的iPhone山寨机,另一个是某个版本的Android相机中使用的Nine-patch):
Jelly Bean示例可能对于您寻找的内容有些复杂,因此以下是一些实施自动对焦的视觉反馈方式的指南。该过程可能有些复杂。我并不是在假装我的方法是完成此操作的最佳方法,但是以下是一些示例代码,可以帮助您了解其总体概念...
在我的相机预览布局xml文件中:
<!-- Autofocus crosshairs -->
<RelativeLayout
android:id="@+id/af_casing"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true"
android:clipChildren="false" >
<com.package.AutofocusCrosshair
android:id="@+id/af_crosshair"
android:layout_width="65dp"
android:layout_height="65dp"
android:clipChildren="false" >
</com.package.AutofocusCrosshair>
</RelativeLayout>
这个AutofocusCrosshair类是以下内容:
public class AutofocusCrosshair extends View {
private Point mLocationPoint;
public AutofocusCrosshair(Context context, AttributeSet attrs) {
super(context, attrs);
}
private void setDrawable(int resid) {
this.setBackgroundResource(resid);
}
public void showStart() {
setDrawable(R.drawable.focus_crosshair_image);
}
public void clear() {
setBackgroundDrawable(null);
}
}
当我想要开始自动对焦时,在我的活动中,我会执行以下操作:
mAutofocusCrosshair = (AutofocusCrosshair) findViewById(R.id.af_crosshair);
//Now add your own code to position this within the view however you choose
mAutofocusCrosshair.showStart();
//I'm assuming you'll want to animate this... so start an animation here
findViewById(R.id.af_casing).startAnimation(mAutofocusAnimation);
确保在动画结束时清除图像:
mAutofocusAnimation.setAnimationListener(new AnimationListener() {
@Override public void onAnimationEnd(Animation arg0) {
mAutofocusCrosshair.clear();
}
@Override public void onAnimationRepeat(Animation arg0) {}
@Override public void onAnimationStart(Animation arg0) {}
});
如果您指的是相机应用程序预览屏幕中变换颜色的小矩形,我很确定您必须自己绘制它。如果这不是您想要的答案,我很抱歉!
但是,您可以调用autoFocus()
,它将稍后提供一个结果,告诉您相机是否对焦。自API 14以来,即使相机处于FOCUS_MODE_CONTINUOUS_PICTURE
模式下,它也可以工作。
很抱歉,我不知道有哪些好的教程描述使用对焦机制。在过去的一周中,我学到了一件事情:不要在开始预览图像之前调用autoFocus()
,因为它会导致HTC Nexus One崩溃。
我从http://marakana.com/forums/android/examples/39.html的示例代码构建了我的第一个Android相机应用程序,但请注意,那里编写的代码会将每个预览帧写入SD卡并快速填满!而且那里没有关于自动对焦的代码。
编辑:当然,包括焦点指示器的最终示例代码在相机应用程序源代码中。这个问题:我在哪里可以获取Android相机应用程序源代码?告诉你如何获取它。我只是按照那里的说明,得到了大约35兆字节的源代码,但我恐怕还没有找到小的对焦矩形!