【问题标题】:Adding TextureView to main.xml: Android将 TextureView 添加到 main.xml:Android
【发布时间】:2014-01-06 05:59:30
【问题描述】:

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

Java:

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" />

【问题讨论】:

    标签: java android textureview


    【解决方案1】:

    我不明白你的问题。

    您说您想将TextureView 添加到您的activity_main.xml 布局中。但它已经在那里了。

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

    您也在创建一个新的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

    【讨论】:

    • View.addView(mTextureView);这条线不断地向我显示错误。它要求我将演员表添加到 Text_View ..我已经更新了我的代码请检查它
    • well View.addView(mTextureView); 表示您正在从布局中获取TextureView,并添加一个新创建的TextureView 并将其添加到其中。您不需要有 2 个TextureViews 即可开始
    • @Nicholas Tyler...这样做有错吗...必须以这种方式完成吗?我该怎么做才能消除这个错误?
    • 我之前试过这个...它只会显示带有 hello world 的布局...不会有纹理视图。这就是我选择新 Textureview 的原因
    • 好吧,您不需要多个TextureView 我看到您删除了setContentView(mTextureView);,这样更好。您的问题可能是您在 xml 上设置了很大的余量,请尝试删除此:android:layout_marginLeft="372dp" 和此:android:layout_marginTop="180dp"。如果您在小屏幕上进行测试,很容易将纹理视图放在屏幕边界之外。
    【解决方案2】:

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

    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>
    

    【讨论】:

    • 但是如果我们使用 setContentView(myTexture);在 setContentView(R.layout.activity_main) 之后;整个布局将消失。我想查看纹理视图以及布局。当我运行这个程序时,它只会显示纹理视图。
    • Y 你在加载布局后使用 setcontentview 吗?您的纹理视图在布局右侧。然后它足以加载仅布局
    • 看看你给我的答案 super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); myTexture = new TextureView(this); myTexture.setSurfaceTextureListener(this); setContentView(myTexture);
    • 是的,现在我明白了。相机未打开。抱歉我没用过textureview所以弄糊涂了。
    • 但我在我的代码中使用了它......仍然不可见。在我的 xml 中有一个纹理视图以及一个文本视图“Hello world”。我想显示这两个。但目前只显示 Hello world
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多