我尝试使用SurfaceView和TextureView播放相同的视频,并注意到使用TextureView渲染的图像比使用SurfaceView更具别名(更不“平滑”).
这是什么原因?有没有办法配置TextureView的渲染看起来更好?
TextureView的用法如下:
- TextureView textureView = new TextureView(this);
- textureView.setSurfaceTextureListener(new SurfaceTextureListener() {
- @Override
- public void onSurfaceTextureAvailable(SurfaceTexture surfaceTexture,int width,int height) {
- Log.i("test","onSurfaceTextureAvailable()");
- MediaPlayer player = MediaPlayer.create(TestActivity.this,Uri.parse(VIDEO_URL));
- Surface surface = new Surface(surfaceTexture);
- player.setSurface(surface);
- player.start();
- }
- @Override
- public void onSurfaceTextureUpdated(SurfaceTexture surface) {
- Log.i("test","onSurfaceTextureUpdated()");
- }
- @Override
- public void onSurfaceTextureSizeChanged(SurfaceTexture surface,int height) {
- Log.i("test","onSurfaceTextureSizeChanged()");
- }
- @Override
- public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
- Log.i("test","onSurfaceTextureDestroyed()");
- return false;
- }
- });
- setContentView(textureView);
而对于SurfaceView:
- SurfaceView surfaceView = new SurfaceView(this);
- surfaceView.getHolder().addCallback(new Callback() {
- @Override
- public void surfaceCreated(SurfaceHolder holder) {
- Log.i("test","surfaceCreated()");
- }
- @Override
- public void surfaceDestroyed(SurfaceHolder holder) {
- Log.i("test","surfaceDestroyed()");
- }
- @Override
- public void surfaceChanged(SurfaceHolder holder,int format,"surfaceChanged()");
- MediaPlayer player = MediaPlayer.create(TestActivity.this,Uri.parse(VIDEO_URL));
- player.setSurface(holder.getSurface());
- player.start();
- }
- });
- setContentView(surfaceView);
解决方法
好吧,似乎在TextureView上应用缩放(除1之外)就可以实现我正在寻找的“平滑”效果.
- textureView.setScaleX(1.00001f);
这听起来像是一个奇怪的黑客……但它确实有效.挖掘改变渲染方面的缩放比例会很有趣……