我建立一个自定义的摄像头,支持自动对焦,并不仅仅是想知道如果有一种方法可以调用相同的自动对焦长方形的指标,本机相机具有或者如果我要建立一个从头开始..任何实例或教程链接会大大AP preciated。
I am building a custom camera with auto focus, and was merely wondering if there is a way to invoke the same auto-focus rectangular indicator that the native camera has or if i have to build that from scratch.. any examples or tutorial links would be greatly appreciated.
这可能有助于看看最新的果冻豆4.2摄像头处理这个问题的方式。您也可以下载相机源,如下所示:
It might be helpful to look at the way the most recent Jelly Bean 4.2 camera handles this. You can download the Camera source as follows:
git clone https://android.googlesource.com/platform/packages/apps/Camera.git
一旦你的code,导航到 FocusOverlayManager
类和 PieRenderer
类。如果你还没有尝试过了这个最新版本之前,焦度计是一个馅饼般的圆形旋转对焦完成后。你可以让你自己的广场,Photoshop或使用这两个,我已经使用在过去的一个(一个是iPhone骗人货我做了,另一种是在某些版本的Android摄像头采用了九补丁):
Once you have the code, navigate to the FocusOverlayManager
class and the PieRenderer
class. If you haven't tried out this newest version before, the focus meter is a pie-like circle that rotates upon focus completion. You can make your own square in photoshop or use one of these two that I have used in the past (one is an iPhone ripoff I made and the other is a nine-patch used in some version of the android camera):
的果冻豆的例子可能有点复杂,你在找什么,所以下面是我实现自动对焦的视觉反馈的方式的指导。该过程可以有些复杂。我不pretend我的方法是做到这一点的最好办法,但是这里有一些例子code,让你的总体思路...
The Jelly Bean example may be a little complicated for what you are looking for, so below are some guidelines for the way I implemented visual feedback for autofocus. The process can be somewhat complicated. I don't pretend my way is the best way to do this, but here is some example code that gives you the general idea...
在我的相机preVIEW布局的xml文件:
In my camera preview layout xml file:
<!-- 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类如下:
This AutofocusCrosshair class is the following:
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);
}
}
和的时候,我的活动,我要开始自动对焦我做到以下几点:
And when, in my activity, I want to start autofocus I do the following:
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);
和确保在动画的最后清除图像:
And make sure at the end of your animation to clear the image:
mAutofocusAnimation.setAnimationListener(new AnimationListener() {
@Override public void onAnimationEnd(Animation arg0) {
mAutofocusCrosshair.clear();
}
@Override public void onAnimationRepeat(Animation arg0) {}
@Override public void onAnimationStart(Animation arg0) {}
});