检测TextrueView 是否有画面
时间: 2024-02-02 10:03:14 浏览: 20
在 Android 中,可以通过 TextureView 的 SurfaceTextureListener 回调方法来检测 TextureView 是否有画面。具体实现方法如下:
1. 创建 TextureView,并设置 SurfaceTextureListener。
2. 在 SurfaceTextureListener 的回调方法中,检测 SurfaceTexture 是否可用。
3. 如果 SurfaceTexture 可用,则表示 TextureView 有画面;否则,表示 TextureView 没有画面。
示例代码如下:
```
public class MyTextureView extends TextureView implements TextureView.SurfaceTextureListener {
private boolean hasDrawn = false;
public MyTextureView(Context context) {
super(context);
init();
}
public MyTextureView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public MyTextureView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
private void init() {
setSurfaceTextureListener(this);
}
@Override
public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
hasDrawn = true;
}
@Override
public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
// do nothing
}
@Override
public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
hasDrawn = false;
return true;
}
@Override
public void onSurfaceTextureUpdated(SurfaceTexture surface) {
// do nothing
}
public boolean hasDrawn() {
return hasDrawn;
}
}
```
在使用 MyTextureView 时,可以通过调用 hasDrawn() 方法来检测 TextureView 是否有画面。如果返回 true,则表示 TextureView 有画面;否则,表示 TextureView 没有画面。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)