将TextureView添加到main.xml:Android

时间:2014-01-06 05:59:30

标签: java android textureview

我正在尝试使用TextureView捕获图像,但是mainView文件被TextureView覆盖,因此我看不到main.xml文件的内容。我正在尝试将TextureView添加到main.xml中。请提供有关如何改进我的代码的建议。

爪哇:

public class MainActivity extends Activity implements TextureView.SurfaceTextureListener {
    private Camera mCamera;
    private TextureView mTextureView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mTextureView = (TextureView) findViewById(R.id.textureView1);
        mTextureView = new TextureView(this);
        mTextureView.setSurfaceTextureListener(this);
    }

    @Override
    public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
        mCamera = Camera.open(0);
        try {
            mCamera.setPreviewTexture(surface);
        } 
        catch(IOException e) {
            e.printStackTrace();
        }
        mCamera.startPreview();
    }

    @Override
    public boolean onSurfaceTextureDestroyed(SurfaceTexture arg0) {
        mCamera.stopPreview();
        mCamera.release();
        return true;
    }

    @Override
    public void onSurfaceTextureSizeChanged(SurfaceTexture arg0, int arg1, int arg2) {
        // TODO Auto-generated method stub
    }

    @Override
    public void onSurfaceTextureUpdated(SurfaceTexture arg0) {
    }
}

布局资源XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/hello_world" />

<TextureView
    android:id="@+id/textureView1"
    android:layout_width="350dp"
    android:layout_height="350dp"
    android:layout_below="@+id/textView1" />

2 个答案:

答案 0 :(得分:0)

我不明白你的问题。

您说要在activity_main.xml布局中添加TextureView。但它已经存在了。

显然,如果您在setContentView(mTextureView);之后使用setContentView(R.layout.activity_main);,整个布局将会消失。您正在将活动内容设置为单个视图而不是整个布局。

您还在创建一个新的TextureView,而不是使用activity_main布局中的那个。

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mTextureView = (TextureView)findViewById(R.id.textureView1);
    mTextureView.setSurfaceTextureListener(this);
}

我认为你应该做这样的事情。我从来没有测试过这个。

您似乎正在尝试使用TextureView进行相机预览,有一个很好的示例here和一个更高级的版本here

答案 1 :(得分:-1)

检查此代码,这对我有用。

MainActivity.java。

import java.io.IOException;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.SurfaceTexture;
import android.hardware.Camera;
import android.os.Bundle;
import android.view.Gravity;
import android.view.Menu;
import android.view.TextureView;
import android.view.TextureView.SurfaceTextureListener;
import android.view.View;
import android.widget.FrameLayout;

public class MainActivity extends Activity implements SurfaceTextureListener {

   private TextureView myTexture;
   private Camera mCamera;

   @SuppressLint("NewApi")
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      myTexture = new TextureView(this);
      myTexture.setSurfaceTextureListener(this);
      setContentView(myTexture);
   }
   @Override
   public boolean onCreateOptionsMenu(Menu menu) {
      // Inflate the menu; this adds items to the action bar if it is present.
      getMenuInflater().inflate(R.menu.main, menu);
      return true;
   }
   @SuppressLint("NewApi")
   @Override
   public void onSurfaceTextureAvailable(SurfaceTexture arg0, int arg1,
   int arg2) {
      mCamera = Camera.open();
      Camera.Size previewSize = mCamera.getParameters().getPreviewSize();
      myTexture.setLayoutParams(new FrameLayout.LayoutParams(
      previewSize.width, previewSize.height, Gravity.CENTER));
      try {
         mCamera.setPreviewTexture(arg0);
        } catch (IOException t) {
        }
      mCamera.startPreview();
      myTexture.setAlpha(1.0f);
      myTexture.setRotation(90.0f);
   }

   @Override
   public boolean onSurfaceTextureDestroyed(SurfaceTexture arg0) {
      mCamera.stopPreview();
      mCamera.release();
      return true;
   }

   @Override
   public void onSurfaceTextureSizeChanged(SurfaceTexture arg0, int arg1,
   int arg2) {
   // TODO Auto-generated method stub
   }
   @Override
   public void onSurfaceTextureUpdated(SurfaceTexture arg0) {
   // TODO Auto-generated method stub
   }
}

activity_main.xml中

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:paddingBottom="@dimen/activity_vertical_margin"
   android:paddingLeft="@dimen/activity_horizontal_margin"
   android:paddingRight="@dimen/activity_horizontal_margin"
   android:paddingTop="@dimen/activity_vertical_margin"
   tools:context=".MainActivity" >

   <TextureView
      android:id="@+id/textureView1"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignParentTop="true"
      android:layout_centerHorizontal="true" />
</RelativeLayout>

的AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
   package="com.example.textureview"
   android:versionCode="1"
   android:versionName="1.0" >

   <uses-sdk
      android:minSdkVersion="8"
      android:targetSdkVersion="17" />
   <uses-permission android:name="android.permission.CAMERA"/>
   <application
      android:allowBackup="true"
      android:icon="@drawable/ic_launcher"
      android:label="@string/app_name"
      android:theme="@style/AppTheme" >
      <activity 
         android:name="com.example.textureview.MainActivity"
         android:label="@string/app_name" >
         <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
         </intent-filter>
       </activity>
   </application>
</manifest>