DAr*_*rkO 52 layout android android-videoview
所以我扩展了VideoView的onMeasure以扩展视频以适应全屏视图.
这是如何:
public void setVideoAspect(int w,int h){
wVideo=w;
hVideo=h;
onMeasure(w, h);
}
@Override
protected void onMeasure (int widthMeasureSpec, int heightMeasureSpec)
{
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
if(wVideo!=0 && hVideo!=0)
setMeasuredDimension(wVideo,hVideo);
}
Run Code Online (Sandbox Code Playgroud)
我使用屏幕的显示指标(宽度,高度)调用setVideoAspect().问题是这种方法会拉伸视频以适应屏幕内部.我希望能够保持纵横比.(我有4:3的视频和3:2的屏幕尺寸.)我使用下面的代码给视图保留比率测量值:
int height = (int) (metrics.widthPixels*3/(float)4);
int width= metrics.widthPixels;
mVideoView.setVideoAspect(width,height);
Run Code Online (Sandbox Code Playgroud)
所以这可以完成这项工作,但是有一个问题:它给了我一个4:3的视频,屏幕的宽度和正确的比例,但它不会使视频居中.(它只是播放视频的底部而不是顶部和底部.)我有一个包含VideoView的相对布局,VideoView的重力设置为居中.
Cam*_*ron 126
尝试使用FrameLayout
替代品.我不确定为什么,但是如果我在我的代码中使用Linear
或者Relative
它不会居中,但FrameLayout
确实如此.这是适合我的视频到屏幕的XML,保留比率并使其居中:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/bg">
<!-- Video player -->
<VideoView
android:id="@+id/surface_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"/>
</FrameLayout>
Run Code Online (Sandbox Code Playgroud)
fro*_*sis 14
为了将视频置于RelativeLayout中心,我添加了两个layout_gravity="center"
广告layout_centerInParent="true"
.它适用于我的Android 4.3手机.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<VideoView android:id="@+id/surface_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_centerInParent="true" />
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
小智 9
Cameron以程序化的方式回答(如果像我这样的人需要它)这段代码在我的代码中的一个活动的创建内部('this'在下面指的是活动)
FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
FrameLayout fl = new FrameLayout(this);
fl.setLayoutParams(lp);
VideoView vv = new VideoView(this);
FrameLayout.LayoutParams lp2 = new FrameLayout.LayoutParams(lp);
lp2.gravity = Gravity.CENTER;
vv.setLayoutParams(lp2);
fl.addView(vv);
setContentView(fl);
Run Code Online (Sandbox Code Playgroud)
小智 5
这适用于任何保持视频长宽比的视频。它将视频放置在VideoView内部,并像ImageView一样执行“中心裁剪”或“内部中心”。
我正在使用VideoView覆盖整个ConstraintLayout。您可以将任何其他布局与match_parent一起用作宽度和高度。
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<VideoView
android:id="@+id/videoView"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)
在onCreate中:
Uri uri = //The uri of your video.
VideoView videoView = findViewById(R.id.videoView);
videoView.setVideoURI(uri);
videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
//Get your video's width and height
int videoWidth = mp.getVideoWidth();
int videoHeight = mp.getVideoHeight();
//Get VideoView's current width and height
int videoViewWidth = videoView.getWidth();
int videoViewHeight = videoView.getHeight();
float xScale = (float) videoViewWidth / videoWidth;
float yScale = (float) videoViewHeight / videoHeight;
//For Center Crop use the Math.max to calculate the scale
//float scale = Math.max(xScale, yScale);
//For Center Inside use the Math.min scale.
//I prefer Center Inside so I am using Math.min
float scale = Math.min(xScale, yScale);
float scaledWidth = scale * videoWidth;
float scaledHeight = scale * videoHeight;
//Set the new size for the VideoView based on the dimensions of the video
ViewGroup.LayoutParams layoutParams = videoView.getLayoutParams();
layoutParams.width = (int)scaledWidth;
layoutParams.height = (int)scaledHeight;
videoView.setLayoutParams(layoutParams);
}
});
Run Code Online (Sandbox Code Playgroud)
希望它能对某人有所帮助!
归档时间: |
|
查看次数: |
44631 次 |
最近记录: |