protected void onDraw(Canvas canvas) { super.onDraw(canvas); if (mTempBitmap != null) { canvas.drawBitmap(mTempBitmap, 0, 0, null); } }在这代码报错'onDraw(Canvas)' cannot override 'onDraw(Canvas)' in 'android.view.TextureView'; overridden method is finad要怎样修改
时间: 2023-11-28 20:47:56 浏览: 172
这个错误提示是因为你的类继承了TextureView,而TextureView已经有了一个名为onDraw(Canvas)的final方法,无法被重写。如果你需要在TextureView上绘制图形,可以使用SurfaceTexture来实现。
如果你想要在TextureView上绘制图形,可以在onSurfaceTextureAvailable回调中获取SurfaceTexture,并创建一个新的线程来绘制图形。具体实现可以参考以下代码:
```
public class MyTextureView extends TextureView implements TextureView.SurfaceTextureListener, Runnable {
private SurfaceTexture mSurfaceTexture;
private boolean mIsDrawing;
public MyTextureView(Context context) {
super(context);
setSurfaceTextureListener(this);
}
@Override
public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
mSurfaceTexture = surface;
new Thread(this).start();
}
@Override
public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
}
@Override
public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
mSurfaceTexture = null;
mIsDrawing = false;
return true;
}
@Override
public void onSurfaceTextureUpdated(SurfaceTexture surface) {
}
@Override
public void run() {
mIsDrawing = true;
while (mIsDrawing) {
Canvas canvas = mSurfaceTexture.lockCanvas(null);
// 在canvas上绘制图形
mSurfaceTexture.unlockCanvasAndPost(canvas);
}
}
}
```
阅读全文