1

我已经看到很多在主 Activity 中使用 TextureView 的示例,但我正在尝试将其放入片段中。

我已经创建了最简单的示例,它的 onCreateView 被调用,onActivityCreated 也被调用,但是在传回 TextureView 后没有调用 onSurfaceTextureAvailable。

我错过了什么?

谢谢G

public class FullscreenActivity extends Activity {

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

public class TextureViewFragment  extends Fragment
implements TextureView.SurfaceTextureListener {

    private TextureView         mTextureView;

     @Override
     public void onActivityCreated(Bundle savedInstanceState) {
          super.onActivityCreated(savedInstanceState);
     }

     @Override
     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

          mTextureView = new TextureView(getActivity());
          mTextureView.setSurfaceTextureListener(this);
          mTextureView.setOpaque(false);    
         return mTextureView;
    }

    @Override
    public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
    }

    @Override
    public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
         // TODO Auto-generated method stub
        return false;
    }

    @Override
    public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width,
        int height) {
         // TODO Auto-generated method stub
     }

    @Override
    public void onSurfaceTextureUpdated(SurfaceTexture surface) {
       // TODO Auto-generated method stub   
    }
}


activity_fullscreen.xml:

<FrameLayout 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:background="#0099cc"
             tools:context=".FullscreenActivity" >

     <fragment class="com.example.test.TextureViewFragment"
               android:id="@+id/graphTextureView"
               android:layout_width="0px" android:layout_height="match_parent" />
 </FrameLayout>
4

3 回答 3

2

这是因为TextureView必须具有非零大小并且可见。根据评论,layout_width设置为0px。将其更改为非零值。

(老问题,但为后代发布答案)

于 2015-10-02T15:23:11.547 回答
1

您应该将“android:hardwareAccelerated=true”添加到您的 AndroidManifest.xml。

TextureView 需要硬件加速。

于 2015-10-12T08:45:42.847 回答
0

TextureView应该在布局 xml 文件中。而不是new用来实例化它,你应该使用这样的东西来获取它:

View view = inflater.inflate(R.layout.layout_video, container, false);
mTextureView = (TextureView) view.findViewById(R.id.video_texture_view);

然后在您的layout_video.xml文件中,您需要一个TextureViewid为 video_texture_view

于 2016-08-31T18:29:53.250 回答