Maison > Questions et réponses > le corps du texte
新手,看到视频播放的时候,动手弄了个最简单的布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<VideoView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/vv_show"/>
</LinearLayout>
在VideoVidw里面设置layout_width,layout_height,不管设置成match_parent还是wrap_content的,这个VideoView还是撑满了整个屏幕(程序就是一个简单的布局,除了自动代码以外,没有加一点代码),发现这里wrap_content就一点用都没有,请教前辈这个是什么问题
PHP中文网2017-04-17 17:48:35
Pour les problèmes de taille, veuillez vous référer à la méthode VideoView
de onMeasure()
VideoView
est liée à mVideoWidth
et mVideoHeight
. Vous pouvez voir à partir du nom de la variable qu'il s'agit de la taille réelle du média à lire. Avez-vous défini le média à lire après ? un aperçu grossier de onMeasure()
, la logique à l’intérieur n’est pas très compliquée. Vous pouvez y jeter un œil vous-même et vous saurez probablement quel est le problème
阿神2017-04-17 17:48:35
De manière générale, le lecteur doit définir sa propre taille car il sera mis à l'échelle et ne pourra pas utiliser la méthode de définition de la hauteur pour envelopper le contenu.
Comme mentionné au premier étage, pour comprendre le processus de rendu de mise en page de VideoView, vous pouvez jeter un œil au code source de VideoView
Le code source contient // no size yet, just adopt the given spec sizes
Si vous ne définissez pas la hauteur, il vous donnera la taille de l'espace par défaut, qui est la taille du conteneur parent. C'est en plein écran.
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
//Log.i("@@@@", "onMeasure(" + MeasureSpec.toString(widthMeasureSpec) + ", "
// + MeasureSpec.toString(heightMeasureSpec) + ")");
int width = getDefaultSize(mVideoWidth, widthMeasureSpec);
int height = getDefaultSize(mVideoHeight, heightMeasureSpec);
if (mVideoWidth > 0 && mVideoHeight > 0) {
int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);
int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec);
if (widthSpecMode == MeasureSpec.EXACTLY && heightSpecMode == MeasureSpec.EXACTLY) {
// the size is fixed
width = widthSpecSize;
height = heightSpecSize;
// for compatibility, we adjust size based on aspect ratio
if ( mVideoWidth * height < width * mVideoHeight ) {
//Log.i("@@@", "image too wide, correcting");
width = height * mVideoWidth / mVideoHeight;
} else if ( mVideoWidth * height > width * mVideoHeight ) {
//Log.i("@@@", "image too tall, correcting");
height = width * mVideoHeight / mVideoWidth;
}
} else if (widthSpecMode == MeasureSpec.EXACTLY) {
// only the width is fixed, adjust the height to match aspect ratio if possible
width = widthSpecSize;
height = width * mVideoHeight / mVideoWidth;
if (heightSpecMode == MeasureSpec.AT_MOST && height > heightSpecSize) {
// couldn't match aspect ratio within the constraints
height = heightSpecSize;
}
} else if (heightSpecMode == MeasureSpec.EXACTLY) {
// only the height is fixed, adjust the width to match aspect ratio if possible
height = heightSpecSize;
width = height * mVideoWidth / mVideoHeight;
if (widthSpecMode == MeasureSpec.AT_MOST && width > widthSpecSize) {
// couldn't match aspect ratio within the constraints
width = widthSpecSize;
}
} else {
// neither the width nor the height are fixed, try to use actual video size
width = mVideoWidth;
height = mVideoHeight;
if (heightSpecMode == MeasureSpec.AT_MOST && height > heightSpecSize) {
// too tall, decrease both width and height
height = heightSpecSize;
width = height * mVideoWidth / mVideoHeight;
}
if (widthSpecMode == MeasureSpec.AT_MOST && width > widthSpecSize) {
// too wide, decrease both width and height
width = widthSpecSize;
height = width * mVideoHeight / mVideoWidth;
}
}
} else {
// no size yet, just adopt the given spec sizes
}
setMeasuredDimension(width, height);
}