作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试为相机预览开发一个自定义小部件。我想用相机预览填充屏幕并在其上绘制一些按钮。
我尝试通过以下方式创建自定义小部件:
import java.io.IOException;
import android.content.Context;
import android.hardware.Camera;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
class CapturePreview extends SurfaceView implements SurfaceHolder.Callback {
SurfaceHolder mHolder;
Camera mCamera;
CapturePreview(Context context) {
super(context);
// Install a SurfaceHolder.Callback so we get notified when the
// underlying surface is created and destroyed.
mHolder = getHolder();
mHolder.addCallback(this);
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
public void surfaceCreated(SurfaceHolder holder) {
// The Surface has been created, acquire the camera and tell it where
// to draw.
mCamera = Camera.open();
try {
mCamera.setPreviewDisplay(holder);
} catch (IOException exception) {
mCamera.release();
mCamera = null;
// TODO: add more exception handling logic here
}
}
public void surfaceDestroyed(SurfaceHolder holder) {
// Surface will be destroyed when we return, so stop the preview.
// Because the CameraDevice object is not a shared resource, it's very
// important to release it when the activity is paused.
mCamera.stopPreview();
mCamera = null;
}
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
// Now that the size is known, set up the camera parameters and begin
// the preview.
Camera.Parameters parameters = mCamera.getParameters();
parameters.setPreviewSize(w, h);
mCamera.setParameters(parameters);
mCamera.startPreview();
}
}
<RelativeLayout android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/RelativeLayout">
<dev.video.client.CapturePreview android:id="@+id/capturePreview" android:layout_width="wrap_content" android:layout_height="wrap_content"/>
</RelativeLayout>
private CapturePreview mPreview;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setFullscreen();
setContentView(R.layout.main);
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
//mPreview = new CapturePreview(this);
//setContentView(mPreview);
mPreview = (CapturePreview)this.findViewById(R.id.capturePreview);
}
最佳答案
您需要在 SurfaceView 中创建以下构造函数才能使其工作:CapturePreview(Context context, AttributeSet attrs)
您可能知道,Java 中的构造函数不是从它的父类(super class)继承的,但这是当您在 XML 文件中使用该组件时 Android 尝试调用的构造函数(而不是以编程方式定义您的 GUI)。当然,那是行不通的,因此我认为错误消息应该更清晰。
AttributeSet包含 XML 声明中提到的每个属性,因此如果您有组件的自定义属性,您可以在构造函数中检索并对其执行操作。
关于android-widget - 安卓 : How to create a custom camera preview widget?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/951121/
我是一名优秀的程序员,十分优秀!