我正在使用一个textureview反复播放视频,它的目的是显示一个背景视频。我需要将textureview的宽度和高度设置为match_parent。
我想缩放视频,使其在播放的textureview上看起来被裁剪。
我按照此链接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));
}
我使用 mediaPlayer.getVideoWidth() 和 mediaPlayer.getVideoHeight() 获得了 mVideoWidth 和 mVideoHeight。但我不明白应该传入什么作为函数参数 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上的属性一样居中裁剪?