我正在試圖創建一個應用程序,我將在屏幕底部有一個按鈕,並且有一個攝像頭預覽會佔用剩下的空間。我使用CameraPreview example code作爲起點。 CameraPreview以編程方式創建ViewGroup,並且它自己可以正常工作。我現在想要把這個ViewGroup放到另一個「視圖容器」中,這個視圖容器是我在使用XML的佈局中創建的。把一個ViewGroup放在一個容器裏
所以基本上,我有一個佈局和一個空間,我想嵌入CameraPreview ViewGroup。
謝謝。
我正在試圖創建一個應用程序,我將在屏幕底部有一個按鈕,並且有一個攝像頭預覽會佔用剩下的空間。我使用CameraPreview example code作爲起點。 CameraPreview以編程方式創建ViewGroup,並且它自己可以正常工作。我現在想要把這個ViewGroup放到另一個「視圖容器」中,這個視圖容器是我在使用XML的佈局中創建的。把一個ViewGroup放在一個容器裏
所以基本上,我有一個佈局和一個空間,我想嵌入CameraPreview ViewGroup。
謝謝。
我認爲這不是:
mPreview = new Preview(this);
setContentView(mPreview);
你會想這樣做:
mPreview = new Preview(this);
setContentView(R.layout.YOUR_XML_LAYOUT);
RelativeLayout yourLayoutContainer = (RelativeLayout) findViewById(R.id.YOUR_LAYOUT_CONTAINER_ID);
yourLayoutContainer.addView(mPreview);
謝謝。我嘗試過,但沒有預覽顯示,只是一個黑色的空白空間。 – fromvega
這是巨大的錯誤檢查簡化各種回調去除,也其實我有我的相機代碼在一個單獨的課程中,以便我可以在不同的應用程序中使用它,而不會讓它朝着正確的方向移動。儘管我沒有在這裏展示它,但我在一個父容器中預覽了各種疊加層,從來沒有出現過基本的Android相機故障之外的任何問題(總是必須處於橫向方向,必須始終具有正確的預覽大小設置等。)
public class CameraActivity extends Activity {
private SurfaceView cameraSurface = null;
private myCamera camera = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.camerasurface);
cameraSurface = (SurfaceView) this.findViewById(R.id.cameraSurface);
camera = new bThereCamera(this,cameraSurface);
}
的XML相機活性
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<SurfaceView android:id="@+id/cameraSurface"
android:layout_height="fill_parent"
android:layout_width="fill_parent"/>
</RelativeLayout>
和一個非常簡化的相機類(相機類結合到活動的壽命,所以應該用上下文被傳遞沒問題),但這只是我的實現,沒有理由不能完全符合單個類。所有的回調已除最低限度
public class myCamera implements SurfaceHolder.Callback {
private Camera cameraDevice = null;
private SurfaceView cameraSurface = null;
private SurfaceHolder cameraSurfaceHolder = null;
public myCamera(CameraActivity activity, SurfaceView surface)
{
parent = activity;
cameraSurface = surface;
cameraSurfaceHolder = cameraSurface.getHolder();
cameraSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
cameraSurfaceHolder.addCallback(this);
}
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h)
{
Camera.Parameters params = cameraDevice.getParameters();
Camera.Size size = getBestPreviewSize(params,w,h);
if (size != null)
params.setPreviewSize(size.width, size.height);
cameraDevice.setParameters(params);
cameraDevice.startPreview();
}
public void surfaceCreated(SurfaceHolder holder)
{
try {
// Get the device interface
cameraDevice = Camera.open(); // This is different for 2.2 and 2.3+ you will need
// a mediator class if you want to use both ...
cameraDevice.setPreviewDisplay(cameraSurfaceHolder);
} catch (IOException e) { }
}
public void surfaceDestroyed(SurfaceHolder holder)
{
if (null == cameraDevice)
return;
cameraDevice.stopPreview();
cameraDevice.release();
cameraDevice = null;
}
public Camera.Size getBestPreviewSize(Camera.Parameters parameters, int w, int h)
{
Camera.Size result = null;
for (Camera.Size size : parameters.getSupportedPreviewSizes())
{
if (size.width <= w && size.height <= h)
{
if (null == result)
result = size;
else
{
int resultDelta = w - result.width + h - result.height;
int newDelta = w - size.width + h - size.height;
if (newDelta < resultDelta)
result = size;
}
}
}
return result;
}
此外,如果你想真正深入到相機中下載Android相機應用程序版本爲2.1(如果你得到一個更新的版本將有不兼容的東西)移除,並採取一看,他們做一些有趣的事情(這裏沒有顯示)關於預覽,捕捉,表現等。
這應該是相當容易的,你有沒有嘗試過它遇到問題?如果是的話,他們是什麼? – Idistic
我試過類似公民conn的建議,但沒有預覽,只是一個黑色的空白空間。 – fromvega