【问题标题】:How resize TextureView to fullscreen when rotation 90?旋转 90 时如何将 TextureView 大小调整为全屏?
【发布时间】:2018-09-06 04:07:28
【问题描述】:

我使用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 以将视频大小调整为全屏,但它不起作用:

@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"

图 2:设置 android:rotation="90"

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

【问题讨论】:

  • 我将 updateTextureViewScaling 更改为您的 setupMatrix,视频无法显示。
  • 必须使用android:rotation="90"?
  • 我使用了 setupMatrix(width,height,90);,但它无法正常工作。
  • 添加 android:screenOrientation="sensor"
  • 它在哪里添加?

标签: android


【解决方案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);
}

【讨论】:

    【解决方案2】:

    在 Activity 中重写此方法:

    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 Manifest 文件中,在活动下提及这一点

    android:configChanges="orientation|screenSize".
    

    【讨论】:

    • 不行。更换手机时我不想旋转屏幕。 android:screenOrientation="portrait"
    • 我的问题:如果我没有设置 android:rotation="90",视频显示旋转 90(图 1)。如果我设置 android:rotation="90",视频大小显示图像 2。我想要图像 2,它可以全屏显示相同的图像 1。
    • 必须在布局中设置 android:rotation="90" 吗?
    • 您的代码只旋转 textview ,旋转后不设置全屏视频。
    • 现在,我可以通过:android:rotation="90" 进行旋转,我的问题是视频全屏(视频高度 = 屏幕高度)。
    猜你喜欢
    • 2017-10-19
    • 2013-05-01
    • 1970-01-01
    • 2015-12-28
    • 1970-01-01
    • 1970-01-01
    • 2015-07-17
    • 2014-08-03
    • 1970-01-01
    相关资源
    最近更新 更多