代码之家  ›  专栏  ›  技术社区  ›  D T

旋转90度时,如何将TextureView调整为全屏?

  •  0
  • D T  · 技术社区  · 8 年前

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

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

    @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:旋转="90"

    enter image description here

    enter image description here

    我有日志: onSurfaceTextureAvailable中的宽度和高度=屏幕大小。

    2 回复  |  直到 8 年前
        1
  •  3
  •   D T    8 年前

    我的问题已通过以下方式解决: 将TextureView放入FrameLayout 和update函数updateTextureReviewScaling

    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
  •  0
  •   sajad zohrei    8 年前

    在活动中覆盖此方法:

    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清单文件中,在activity下面提到这个

    android:configChanges="orientation|screenSize".