我最终不得不做一些与@tommyd非常相似的事情,以避免在视频开头和结尾出现黑色surfaceView闪光灯。但是,我发现在许多手机上,设置/取消视频视图的背景可绘制对象并没有立即发生。在我设置背景的调用与实际显示背景之间可能有大约半秒钟的延迟。
我最终做的是创建一个自定义的SurfaceView,显示一个单一的纯色,然后将其叠加在VideoView之上,并使用SurfaceView.setZOrderMediaOverlay()。
我的自定义SurfaceView受到以下方面的广泛了解:http://android-er.blogspot.com/2010/05/android-surfaceview.html
public class SolidSurfaceView extends SurfaceView implements SurfaceHolder.Callback {
private static final String TAG = SolidSurfaceView.class.getSimpleName();
private SolidSurfaceThread mThread;
private boolean mSurfaceIsValid;
private int mColor;
public SolidSurfaceView(Context context) {
super(context);
init();
}
public SolidSurfaceView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public SolidSurfaceView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
private void init() {
Log.verbose(TAG, "init");
getHolder().addCallback(this);
setZOrderMediaOverlay(true);
}
public void setColor(int color) {
mColor = color;
invalidate();
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
Log.verbose(TAG, "surfaceCreated");
mSurfaceIsValid = true;
mThread = new SolidSurfaceThread(getHolder(), this);
mThread.setRunning(true);
mThread.start();
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
Log.verbose(TAG, "surfaceDestroyed");
mSurfaceIsValid = false;
boolean retry = true;
mThread.setRunning(false);
while (retry) {
try {
mThread.join();
retry = false;
}
catch (InterruptedException e) {
Log.warning(TAG, "Thread join interrupted");
}
}
mThread = null;
}
@Override
protected void onDraw(Canvas canvas) {
if ( ! mSurfaceIsValid) {
return;
}
canvas.drawColor(mColor);
}
private static class SolidSurfaceThread extends Thread {
private final SurfaceHolder mSurfaceHolder;
private final SolidSurfaceView mSurfaceView;
private boolean mIsRunning;
public SolidSurfaceThread(SurfaceHolder surfaceHolder, SolidSurfaceView surfaceView) {
mSurfaceHolder = surfaceHolder;
mSurfaceView = surfaceView;
}
public void setRunning(boolean running) {
mIsRunning = running;
}
@Override
public void run() {
while (mIsRunning) {
Canvas c = null;
try {
c = mSurfaceHolder.lockCanvas(null);
synchronized (mSurfaceHolder) {
mSurfaceView.onDraw(c);
}
}
finally {
// do this in a finally so that if an exception is thrown
// during the above, we don't leave the Surface in an
// inconsistent state
if (c != null) {
mSurfaceHolder.unlockCanvasAndPost(c);
}
}
}
}
}
}
在承载视图的父活动中:
mVideoView = (VideoView)findViewById(R.id.video_view);
mVideoMask = (SolidSurfaceView)findViewById(R.id.video_mask);
mVideoMask.setColor(Color.BLUE);
然后,您可以执行隐藏蒙版或显示蒙版(以及隐藏黑屏 VideoView)等操作。mVideoMask.setVisibility(View.GONE)
mVideoMask.setVisibility(View.VISIBLE)
在我对各种手机的实验中,这种方法提供了非常快速的视频掩码显示/隐藏,而不是设置/清空背景。