在textureview上缩放视频以显示centercrop

时间:2018-05-04 13:46:58

标签: android textureview

我在textureview中反复播放视频,其目的是显示背景视频。我需要将textureview的宽度和高度设置为match_parent。

我想缩放视频,以便在播放它的纹理视图中看起来被剪裁。

我按照此链接TextureView scaling使用了给定的功能。

private void updateTextureViewSize(int viewWidth, int viewHeight) {
float scaleX = 1.0f;
float scaleY = 1.0f;

if (mVideoWidth > viewWidth && mVideoHeight > viewHeight) {
    scaleX = mVideoWidth / viewWidth;
    scaleY = mVideoHeight / viewHeight;
} else if (mVideoWidth < viewWidth && mVideoHeight < viewHeight) {
    scaleY = viewWidth / mVideoWidth;
    scaleX = viewHeight / mVideoHeight;
} else if (viewWidth > mVideoWidth) {
    scaleY = (viewWidth / mVideoWidth) / (viewHeight / mVideoHeight);
} else if (viewHeight > mVideoHeight) {
    scaleX = (viewHeight / mVideoHeight) / (viewWidth / mVideoWidth);
}

// 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);

mTextureView.setTransform(matrix);
mTextureView.setLayoutParams(new FrameLayout.LayoutParams(viewWidth, viewHeight));
}

我将mVideoWidth和mVideoHeight视为mediaPlayer.getVideoWidth()和mediaPlayer.getVideoHeight()。我不明白的是我应该传递什么函数参数viewWidth和viewHeight? 我尝试传递父容器的宽度和高度但是崩溃了应用程序。

这是父

的xml代码
<FrameLayout
            android:id="@+id/fl_profile_video_container"
            android:layout_width="match_parent"
            android:layout_height="300dp">

            <!--<VideoView-->
                <!--android:id="@+id/vv_profile_video"-->
                <!--android:layout_width="match_parent"-->
                <!--android:layout_height="match_parent" />-->

            <TextureView
                android:id="@+id/profile_video_texture_view"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_gravity="center"/>

            <View
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@drawable/profile_video_overlay"/>

        </FrameLayout>

这是视频播放部分的代码。

       FrameLayout profileVideoContainer = (FrameLayout) view.findViewById(R.id.fl_profile_video_container);
    profileVideoTextureView = (TextureView) view.findViewById(R.id.profile_video_texture_view);
    SharedPreferences sharedPrefs = getApplicationContext().getSharedPreferences(ProfileVideoRecordingActivity.TAG_VIDEO_PROFILE, Context.MODE_PRIVATE);
    final String profileVideoPath = sharedPrefs.getString(ProfileVideoRecordingActivity.VIDEO_PROFILE_PATH, null);
    if(profileVideoPath != null && (new File(profileVideoPath)).exists()){
        profileVideoContainer.setVisibility(View.VISIBLE);
        profileVideoTextureView.setSurfaceTextureListener(new TextureView.SurfaceTextureListener() {
            @Override
            public void onSurfaceTextureAvailable(SurfaceTexture surfaceTexture, int i, int i1) {
                Surface surface = new Surface(surfaceTexture);
                try {
                    profileVidMediaPlayer = new MediaPlayer();
                    profileVidMediaPlayer.setDataSource(mActivity, Uri.parse(profileVideoPath));
                    profileVidMediaPlayer.setSurface(surface);
                    profileVidMediaPlayer.prepareAsync();
                    profileVidMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                        @Override
                        public void onPrepared(MediaPlayer mediaPlayer) {
                            profileVidMediaPlayer.start();
                            profileVidMediaPlayer.setLooping(true);
                        }
                    });
                }catch(IOException e){
                    Log.e("DEBUG", "IOException", e);
                }
            }

            @Override
            public void onSurfaceTextureSizeChanged(SurfaceTexture surfaceTexture, int i, int i1) {

            }

            @Override
            public boolean onSurfaceTextureDestroyed(SurfaceTexture surfaceTexture) {
                return true;
            }

            @Override
            public void onSurfaceTextureUpdated(SurfaceTexture surfaceTexture) {

            }
        });
    }else{
        profileVideoContainer.setVisibility(View.GONE);
    }

如何缩放视频以使其看起来像ImageView上的属性一样进行中心裁剪?

1 个答案:

答案 0 :(得分:2)

我尝试使用https://www.binpress.com/video-cropping-with-texture-view/中描述的相同缩放方法,但是缩放比例不正确。

我找到了另一个示例,如何为TextureView缩放视频,它对我有用:https://github.com/yqritc/Android-ScalableVideoView/blob/master/library/src/main/java/com/yqritc/scalablevideoview/ScaleManager.java