旋转90度后如何将TextureView调整为全屏大小?

时间:2018-09-06 04:07:28

标签: android

我使用TextureView播放视频: 录制视频时,我必须使用rotation=90来显示视频正确的方向

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#777777" >
<TextureView
            android:id="@+id/textureView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:rotation="90"
            android:layout_alignParentTop="true" />

我尝试将TextureView的setTransform设置为将视频大小调整为fullsreen,但它不起作用:

@Override
    public void onSurfaceTextureAvailable(SurfaceTexture surfaceTexture, int width, int height) {
        Surface surface = new Surface(surfaceTexture);
        try {           
            mMediaPlayer = new MediaPlayer();          
            mMediaPlayer.setDataSource(FILE_NAME);
            mMediaPlayer.setSurface(surface);
            mMediaPlayer.setLooping(false);
            mMediaPlayer.prepareAsync();            
            updateTextureViewScaling(width,height);
            // Play video when the media source is ready for playback.
            mMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                @Override
                public void onPrepared(MediaPlayer mediaPlayer) {
                    mediaPlayer.start();                  
                }
            });
        } catch (IllegalArgumentException e) {
            Log.d(TAG, e.getMessage());
        } catch (SecurityException e) {
            Log.d(TAG, e.getMessage());
        } catch (IllegalStateException e) {
            Log.d(TAG, e.getMessage());
        } catch (IOException e) {
            Log.d(TAG, e.getMessage());
        }
    }

 private void updateTextureViewScaling(int viewWidth, int viewHeight) {
        float scaleX = 1.0f;
        float scaleY = 1.0f;
        DisplayMetrics displayMetrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
        int VIDEO_HEIGHT = displayMetrics.heightPixels;
        int VIDEO_WIDTH = displayMetrics.widthPixels;
        if (VIDEO_WIDTH > viewWidth && VIDEO_WIDTH > viewHeight) {
            scaleX = (float) VIDEO_WIDTH / viewWidth;
            scaleY = (float) VIDEO_HEIGHT / viewHeight;
        } else if (VIDEO_WIDTH < viewWidth && VIDEO_HEIGHT < viewHeight) {
            scaleY = (float) viewWidth / VIDEO_WIDTH;
            scaleX = (float) viewHeight / VIDEO_HEIGHT;
        } else if (viewWidth > VIDEO_WIDTH) {
            scaleY = ((float) viewWidth / VIDEO_WIDTH) / ((float) viewHeight / VIDEO_HEIGHT);
        } else if (viewHeight > VIDEO_HEIGHT) {
            scaleX = ((float) viewHeight / VIDEO_WIDTH) / ((float) viewWidth / VIDEO_WIDTH);
        }

        // Calculate pivot points, in our case crop from center
        int pivotPointX = viewWidth / 2;
        int pivotPointY = viewHeight / 2;

        Matrix matrix = new Matrix();
        matrix.setScale(scaleX, scaleY, pivotPointX, pivotPointY);

        textureView.setTransform(matrix);
    }

目标结果:

图片1:未设置android:rotation =“ 90”

enter image description here

图片2:设置android:rotation =“ 90”

enter image description here

如何将视频的宽度和高度设置为全屏: 我有日志: onSurfaceTextureAvailable =屏幕的大小。

2 个答案:

答案 0 :(得分:1)

我的问题已通过以下方法解决: 将TextureView放在FrameLayout中 和更新功能updateTextureViewScaling

private void updateTextureViewScaling(int viewWidth, int viewHeight) {
FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) textureView.getLayoutParams();
            params.width = viewHeight;
            params.height =viewWidth;
            params.gravity= Gravity.CENTER;
            textureView.setLayoutParams(params);
}

答案 1 :(得分:0)

在“活动”中覆盖此方法:

public void onConfigurationChanged(Configuration newConfig) { 
   super.onConfigurationChanged(newConfig);
    // Checks the orientation of the screen
    if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
       Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
       textview.setRotation(90);
    } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
       Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
      textview.setRotation(0);
    }
  }

在Android清单文件中的活动下提及此

android:configChanges="orientation|screenSize".