我正在构建一个具有自动对焦的自定义相机,并且只是想知道是否有办法调用本机相机具有的相同自动对焦矩形指示器,或者我是否必须从头开始构建...任何示例或教程链接将不胜感激。
答案 0 :(得分:11)
查看最新的Jelly Bean 4.2相机处理此方式的方式可能会有所帮助。您可以按如下方式下载相机来源:
git clone https://android.googlesource.com/platform/packages/apps/Camera.git
获得代码后,导航到FocusOverlayManager
课程和PieRenderer
课程。如果您之前没有尝试过这个最新版本,则焦距计是一个类似圆形的圆圈,在焦点完成时旋转。你可以在photoshop中制作自己的方块,或者使用我过去使用的这两个中的一个(一个是我制作的iPhone ripoff,另一个是在某些版本的android相机中使用的九个补丁):
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) {}
});
答案 1 :(得分:1)
如果你的意思是在相机应用程序的预览屏幕中改变颜色的小矩形,我很确定你必须自己画出来。对不起,如果那不是您想要的答案!
但是,您可以拨打autoFocus()
,稍后会提供一个结果,告知相机是否对焦。从API 14开始,即使相机位于FOCUS_MODE_CONTINUOUS_PICTURE
,也能正常工作。
我也很抱歉,我不知道一个描述使用焦点机制的好教程。我在过去一周学到的一件事是:在开始预览图像之前不要打{{1}},因为它会导致HTC Nexus One崩溃。
我在http://marakana.com/forums/android/examples/39.html的示例代码中构建了我的第一个Android相机应用 但要注意,写在那里的代码会将每个预览帧写入SD卡并快速填满!并且那里没有关于自动对焦的代码。
编辑:当然,最终的示例代码(包括焦点指示符)位于相机应用源代码中。这个问题:Where can I get the Android camera application source code?告诉我们如何获得它。我只是按照那里的说明获得了大约35Mbytes的源代码,我恐怕还没有找到那个小聚焦矩形!