Java 将TextureView添加到main.xml:Android
我正在尝试使用TextureView捕获图像,但main.xml文件被TextureView覆盖,因此无法查看main.xml文件的内容。我正在尝试将TextureView添加到main.xml中。请给出关于如何改进我的代码的建议 爪哇:Java 将TextureView添加到main.xml:Android,java,android,textureview,Java,Android,Textureview,我正在尝试使用TextureView捕获图像,但main.xml文件被TextureView覆盖,因此无法查看main.xml文件的内容。我正在尝试将TextureView添加到main.xml中。请给出关于如何改进我的代码的建议 爪哇: public类MainActivity扩展活动实现TextureView.SurfaceTextureListener{ 私人摄像机麦卡梅拉; 私有纹理视图mTextureView; @凌驾 创建时受保护的void(Bundle savedInstanceSt
public类MainActivity扩展活动实现TextureView.SurfaceTextureListener{
私人摄像机麦卡梅拉;
私有纹理视图mTextureView;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTextureView=(TextureView)findViewById(R.id.TextureView W1);
mTextureView=new TextureView(此);
mTextureView.setSurfaceTextureListener(此);
}
@凌驾
SurfaceTextureAvailable上的公共空心(SurfaceTexture曲面、整型宽度、整型高度){
mCamera=Camera.open(0);
试一试{
mCamera.setPreviewTexture(表面);
}
捕获(IOE异常){
e、 printStackTrace();
}
mCamera.startPreview();
}
@凌驾
已销毁SurfaceTexture上的公共布尔值(SurfaceTexture arg0){
mCamera.stopPreview();
mCamera.release();
返回true;
}
@凌驾
SurfaceTextureSizeChanged上的公共无效(SurfaceTexture arg0、int arg1、int arg2){
//TODO自动生成的方法存根
}
@凌驾
SurfaceTextureUpdated上的公共无效(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" />
我不明白你的问题 您说要将
TextureView
添加到activity\u main.xml布局中。但它已经存在了
显然,如果使用setContentView(mTextureView)代码>在setContentView(R.layout.activity\u main)之后代码>整个布局将消失。您将活动内容设置为单个视图,而不是整个布局
您还正在创建一个新的TextureView
,而不使用activity\u主布局上的
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTextureView = (TextureView)findViewById(R.id.textureView1);
mTextureView.setSurfaceTextureListener(this);
}
我想你应该这样做。但我从未测试过这个
似乎您正在尝试使用TextureView制作相机预览,有一个很好的示例,还有一个更高级的版本。检查此代码这对我有用
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>
View.addView(mTextureView);这一行不断地向我显示错误。它要求我在文本视图中添加cast。我已经更新了我的代码,请检查它。addView(mTextureView)代码>表示您正在从布局中获取TextureView
,并添加新创建的TextureView
并将其添加到布局中。您不需要有2个TextureView
即可启动with@Nicholas泰勒…这样做是不对的…必须用不正确的方式来做吗?我能做些什么来消除这个错误?我以前试过这个…它只会显示带有hello world的布局…不会有纹理视图。这就是为什么我选择了新的TextureView,您不需要超过一个TextureView
我看到您删除了setContentView(mTextureView)代码>哪个更好。您的问题可能是您在xml上留了很大的空白,请尝试删除以下内容:android:layout\u marginLeft=“372dp”
和以下内容:android:layout\u marginTop=“180dp”
。如果在小屏幕上进行测试,很容易将textureview置于屏幕边界之外;setContentView之后(R.layout.activity_main);整个布局将消失。我想查看纹理视图以及布局。当我运行此程序时,它将只显示纹理视图。Y加载布局后是否使用setcontentview?您的textureview在布局右侧显示。然后只加载布局就足够了看看你给我的答案super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);myTexture=新的TextureView(此);myTexture.setSurfaceTextureListener(此);setContentView(myTexture);是的,现在我明白了。相机没有打开。很抱歉,我没有使用textureview,所以感到困惑。但我在代码中使用了它…仍然不可见。在我的xml中,有一个纹理视图和一个文本视图“Hello world”。我想显示这两个。但目前只显示Hello world