Gus*_*Gus 15 video android android-layout android-videoview
在Android的VideoView中,有没有办法实现与ImageView.ScaleType.CENTER_CROP相同的效果?
也就是说,我希望我的VideoView能够播放视频,使其在不失真的情况下填满整个屏幕.如果视频宽高比不完全适合屏幕,则应裁剪而不是扭曲.
以下解决方案将填满屏幕,但不保持视频的宽高比:https: //stackoverflow.com/a/6927300/1068656
此解决方案保持视频的宽高比,但不会填满整个屏幕(视频会缩放,直到较长的一侧到达屏幕边缘,从而在侧面引入条形图):https: //stackoverflow.com/a/4855315/1068656
Dee*_*egi 13
虽然为时已晚,但它可能会帮助其他人寻找同样的问题.以下答案保持宽高比(videoProportion).视频视图的额外部分将通过电话视图进行裁剪.
private void setDimension() {
// Adjust the size of the video
// so it fits on the screen
float videoProportion = getVideoProportion();
int screenWidth = getResources().getDisplayMetrics().widthPixels;
int screenHeight = getResources().getDisplayMetrics().heightPixels;
float screenProportion = (float) screenHeight / (float) screenWidth;
android.view.ViewGroup.LayoutParams lp = videoView.getLayoutParams();
if (videoProportion < screenProportion) {
lp.height= screenHeight;
lp.width = (int) ((float) screenHeight / videoProportion);
} else {
lp.width = screenWidth;
lp.height = (int) ((float) screenWidth * videoProportion);
}
videoView.setLayoutParams(lp);
}
// This method gets the proportion of the video that you want to display.
// I already know this ratio since my video is hardcoded, you can get the
// height and width of your video and appropriately generate the proportion
// as :height/width
private float getVideoProportion(){
return 1.5f;
}
Run Code Online (Sandbox Code Playgroud)
在Android的VideoView中,这里有一个简单易行的方法来实现与 ImageView.ScaleType.CENTER_CROP
XML
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<VideoView
android:id="@+id/videoView"
android:layout_width="@dimen/dimen_0dp"
android:layout_height="@dimen/dimen_0dp"
android:visibility="gone"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)
在科特林:
videoView.setOnPreparedListener { mediaPlayer ->
val videoRatio = mediaPlayer.videoWidth / mediaPlayer.videoHeight.toFloat()
val screenRatio = videoView.width / videoView.height.toFloat()
val scaleX = videoRatio / screenRatio
if (scaleX >= 1f) {
videoView.scaleX = scaleX
} else {
videoView.scaleY = 1f / scaleX
}
}
Run Code Online (Sandbox Code Playgroud)
在 Java 中:
videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
float videoRatio = mp.getVideoWidth() / (float) mp.getVideoHeight();
float screenRatio = videoView.getWidth() / (float)
videoView.getHeight();
float scaleX = videoRatio / screenRatio;
if (scaleX >= 1f) {
videoView.setScaleX(scaleX);
} else {
videoView.setScaleY(1f / scale);
}
}
});
Run Code Online (Sandbox Code Playgroud)
这对我有用。希望这会帮助某人。
| 归档时间: |
|
| 查看次数: |
6606 次 |
| 最近记录: |