aspect-ratio 相关问题

元素的纵横比描述了其宽度与高度之间的比例关系。

Android 视频视图crop_center

我有一个RelativeLayout 我有一个RelativeLayout <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_gravity="center" android:foregroundGravity="center" android:gravity="center" android:orientation="horizontal" > <VideoView android:id="@+id/videoViewPanel" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_gravity="center" android:layout_centerInParent="true"/> </RelativeLayout> 我需要的是全屏显示裁剪后的视频。如果我可以与 ImageView 进行比较,我需要将其显示为crop_center。 如何使 VideoView 不自动调整视频大小以适合中心,而是裁剪中心? 在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> 在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 / scaleX); } } }); 在 Kotlin 中: 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 } } 这对我有用。希望这会对某人有所帮助。 解决方案是使用 TextureView 代替 VideoView(SurfaceView)。 TextureView 不会对内容进行任何操作以使其适合屏幕。 这是解决方案的代码示例: //store the SurfaceTexture to set surface for MediaPlayer mTextureView.setSurfaceTextureListener(new SurfaceTextureListener() { @Override public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) { FullScreenActivity.this.mSurface = surface; } .... Surface s = new Surface(mSurface); mPlayer = mp; mp.setSurface(s); scaleVideo(mp);//<-- this function scales video to run cropped .... private void scaleVideo(MediaPlayer mPlayer) { LayoutParams videoParams = (LayoutParams) mTextureView .getLayoutParams(); DisplayMetrics dm = new DisplayMetrics(); FullScreenActivity.this.getWindowManager().getDefaultDisplay() .getMetrics(dm); final int height = dm.heightPixels; final int width = dm.widthPixels; int videoHeight = mPlayer.getVideoHeight(); int videoWidth = mPlayer.getVideoWidth(); double hRatio = 1; hRatio = (height * 1.0 / videoHeight) / (width * 1.0 / videoWidth); videoParams.x = (int) (hRatio <= 1 ? 0 : Math.round((-(hRatio - 1) / 2) * width)); videoParams.y = (int) (hRatio >= 1 ? 0 : Math .round((((-1 / hRatio) + 1) / 2) * height)); videoParams.width = width - videoParams.x - videoParams.x; videoParams.height = height - videoParams.y - videoParams.y; Log.e(TAG, "x:" + videoParams.x + " y:" + videoParams.y); mTextureView.setScaleX(1.00001f);//<-- this line enables smoothing of the picture in TextureView. mTextureView.requestLayout(); mTextureView.invalidate(); } 要全屏裁剪中心,您仍然可以使用 VideoView。设置VideoView的宽度和高度以匹配RelativeLayout中的父级,并将其调整为大于屏幕并设置其位置。 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/rootLayout" tools:context="com.example.Activity"> <RelativeLayout android:layout_width="match_parent" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:layout_height="match_parent"> <VideoView android:id="@+id/video_view" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_centerInParent="true" android:layout_centerVertical="true" /> </RelativeLayout> </RelativeLayout> 然后在onCreate中: RelativeLayout rootView=(RelativeLayout) findViewById(R.id.rootLayout); Display display=getWindowManager().getDefaultDisplay(); Point size=new Point(); display.getSize(size); FrameLayout.LayoutParams rootViewParams = (FrameLayout.LayoutParams) rootView.getLayoutParams(); int videoWidth=864; int videoHeight=1280; if ((float)videoWidth/(float)videoHeight<(float)size.x/(float)size.y) { rootViewParams.width=size.x; rootViewParams.height=videoHeight*size.x/videoWidth; rootView.setX(0); rootView.setY((rootViewParams.height-size.y)/2*-1); } else { rootViewParams.width=videoWidth*size.y/videoHeight; rootViewParams.height=size.y; rootView.setX((rootViewParams.width-size.x)/2*-1); rootView.setY(0); } rootView.setLayoutParams(rootViewParams); final VideoView mVideoView=(VideoView)findViewById(R.id.video_view); mVideoView.setVideoURI(Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.splash)); mVideoView.requestFocus(); mVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() { @Override public void onPrepared(MediaPlayer mediaPlayer) { mVideoView.start(); } }); 我只是将视频放入ConstraintLayout中并带有这样的参数。这有助于拉伸视频并实现android:scaleType="centerCrop"效果。 <VideoView android:id="@+id/video_view" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintBottom_toBottomOf="parent" android:layout_gravity="center_horizontal" android:layout_width="0dp" android:layout_height="0dp" /> 我找到了一个解决方案:默认行为就像 fitCenter ,所以我计算视频比例(宽度/高度)和屏幕比例,然后将 VideoView 缩放到全屏。结果就像centerCrop一样。 添加@Nabin 的回答。如果您在暂停时遇到问题。 如果视频暂停导致视频视图高度发生变化并显示下方黑屏。用这个。 public class CustomVideoView extends VideoView { private int originalWidth = 0; private int originalHeight = 0; public CustomVideoView(Context context) { super(context); } public CustomVideoView(Context context, AttributeSet attrs) { super(context, attrs); } public CustomVideoView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { if (originalWidth == 0 || originalHeight == 0) { originalWidth = MeasureSpec.getSize(widthMeasureSpec); originalHeight = MeasureSpec.getSize(heightMeasureSpec); } setMeasuredDimension(originalWidth, originalHeight); } } 就我而言,我知道 screenRatio > videoRatio 并且我需要制作圆角。我的解决方案只是设置高度wrap_content和宽度match_parent: <com.google.android.material.card.MaterialCardView android:id="@+id/mcv_video" android:layout_width="0dp" android:layout_height="130dp" app:cardCornerRadius="12dp" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent"> <VideoView android:id="@+id/video_view" android:layout_width="match_parent" android:layout_height="wrap_content" /> </com.google.android.material.card.MaterialCardView>

回答 7 投票 0

SwiftUI 中的图像在尝试设置宽高比时在底部占用额外空间

我想让图像占据其容器的整个宽度,高度将基于明确提供的宽高比。此外,图像应该覆盖其所有框架,并且无论其内容是...

回答 1 投票 0

将具有纵横比的元素居中[关闭]

当尝试将应用了纵横比的元素居中时,我遇到了一个奇怪的问题。我认为它的行为与图像居中的工作方式类似,但我总是以......

回答 2 投票 0

CSS操作的计算顺序

这是我的问题的最小示例: 这是我的问题的最小示例: <div class="container"> <div class="dependent__height"></div> <div class="source__height"></div> </div> .container { display: flex; height: 500px; /* should not be there */ } .dependent__height { aspect-ratio: 16 / 9; background-color: yellow; } .source__height { width: 10px; height: 500px; /* can change arbitrary */ background-color: tomato; } 我想要实现的是.container不会设置高度,高度的唯一来源是.source__height。它会在运行时发生变化,一切都应该正常工作。我没有附加到弹性盒或其他东西,我只是想得到想要的行为(宽度将根据长宽比和.source__height的高度计算) 我尝试了很多跳跃,还有 JavaScript。如果使用 MutationObserver 和同步高度,JavaScript 可能会工作,但非常笨拙,对于我的用例来说,编写和维护会非常痛苦。 如果 .container 设置了高度,则可以正常工作: 但如果 height 属性被删除则不然: 注意:在现实场景中,我使用 <img> ,问题大致相同,可以通过该问题的解决方案来解决。如果你想要更多真实世界的例子,我也可以添加。 这是一种通过网格实现您想要的效果的方法: .container { display: grid; grid-template-columns: 100% max-content; } .dependent__height { aspect-ratio: 16 / 9; background-color: yellow; } .source__height { width: 10px; height: 500px; /* can change arbitrary */ background-color: tomato; } <div class="container"> <div class="dependent__height"></div> <div class="source__height"></div> </div> 但是,如果宽度不够,则 16/9 的依赖高度将无法在该纵横比下增长到所需的完整高度。一种解决方案是将高度设置为 100%,但这会导致其溢出容器(添加轮廓以说明这一点): .container { display: grid; grid-template-columns: 100% max-content; outline: 4px solid skyblue; } .dependent__height { aspect-ratio: 16 / 9; height: 100%; background-color: yellow; } .source__height { width: 10px; height: 500px; /* can change arbitrary */ background-color: tomato; } <div class="container"> <div class="dependent__height"></div> <div class="source__height"></div> </div> 希望这足以让您玩并获得您想要的东西,如果这不能完全回答所有问题,请通过编辑为您的问题添加详细信息,我可以更新我的答案

回答 1 投票 0

为什么在这个简单的测试用例网页中,flex子元素没有收缩?

我有一个运行完美的网页,在 ResizeObserver 的帮助下调整了子画布元素(带有兔子的棋盘)的大小,同时保持其 1:1 的纵横比,当浏览器...

回答 1 投票 0

是否可以使用纵横比作为 CSS 中 calc() 的一部分?或者使用元素自己的宽度作为 calc() 的一部分?

我知道我可以使用宽高比使元素的高度等于其自身宽度的特定折叠/部分,例如宽高比:1 / 2 将使元素的高度是 m 的两倍...

回答 1 投票 0

当子项是使用宽高比的按钮时,Flexbox 问题

我在数字输入组件中使用 Flex 来在输入本身旁边呈现自定义“+”和“-”按钮。按钮使用宽高比:1/1;确保它们始终是正方形,但我似乎无法理解...

回答 1 投票 0

如何将位图放入圆角正方形中?

我正在使用 Android Studio,我希望能够从图库中拾取图像,调整其大小并通过保持正确的宽高比使其适合圆角正方形(约 300x300px)...

回答 2 投票 0

为什么纵向模式 480 x 640 的分辨率被描述为 4 : 3 而不是 3 : 4?

正如我所读到的: 图像的纵横比是其宽度与高度的比率。例如,如果矩形的长宽比为 2:1,则其宽度是高度的两倍。方面...

回答 1 投票 0

固定比例 - 最大“尺寸”

新手需要一些帮助。 如何保留一个 div,以固定的纵横比填充动态大小的容器的高度。 我有一个第一个全页网格设置为 1 2 6 和 2 fr 行,1 列...

回答 1 投票 0

删除纵横比并使用 padding-top:56.25%;相反

如何将其写入代码中,我不知道。 我在这里该怎么做? https://jsfiddle.net/37qob51s/ 我尝试询问 AI,但没有帮助。 有谁知道这将如何完成? 有人告诉我...

回答 1 投票 0

如何用CSS计算当前视口的宽高比? [重复]

我需要以微型方式表示当前视口形状,根据纵横比适合 100% 的包含元素的宽度或高度。我想预览一下 HTML

回答 1 投票 0

第一次使用 CSS 网格并在浏览器上遇到右侧间隙问题

我是 html 和 CSS 新手,想为自己制作一个小网站来展示我的照片。 我有点努力让网格正确,特别是当改变浏览器窗口的纵横比时。 我是

回答 1 投票 0

伸缩容器内的纵横比无法正常工作

所以我在弹性容器中有一堆链接,并希望它们是正方形。我将它们的纵横比设置为 1 / 1,并添加了 1rem 的填充块。 。柔性 { 显示:柔性; 对齐它...

回答 1 投票 0

如何缩放 SVG 图像以填充浏览器窗口?

这看起来应该很容易,但我只是没有得到什么。 我想制作一个包含单个 SVG 图像的 HTML 页面,该图像会自动缩放以适合浏览器窗口,而不需要任何

回答 2 投票 0

Flutter:如何在其父级中调整长宽比的小部件?

我想在一个容器中放置两个小部件。 长宽比为 1:1 且适合容器的小部件 A。 小部件 B 是一个文本,其高度适合内容,其宽度填充父级。 例如...

回答 2 投票 0

如何在Flutter中获得正确且相同的手机横向和纵向模式的宽高比?

我正在使用此代码来获取纵横比 最终 deviceAspectRatio = View.of(context).physicalSize.aspectRatio; 它在纵向上给出了正确的结果,但是当我改变横向上的方向时......

回答 1 投票 0

CSS 网格内奇怪的不需要的溢出

我正在尝试制作一个网格,其中图像输入字段是一个长宽比为 1:1 的大正方形。我需要字段高度与网格中的其他输入字段对齐。所以第二个网格...

回答 1 投票 0

保持视口宽度和高度的纵横比的流体 div 元素

我正在尝试为图像创建一个 div 容器,该容器在页面(flexbox)上水平和垂直居中,宽高比为 2:3。我希望它能够根据宽度和

回答 1 投票 0

在设置了宽高比的 div 内使用 height:100% - 某些浏览器中存在错误?

注意:这个问题是关于 CSS 中的一个新功能:宽高比。该网站上具有相似关键字的大多数问题都涉及非常不同的技术。 好的,有一个带有以下内容的 div

回答 1 投票 0

© www.soinside.com 2019 - 2024. All rights reserved.