元素的纵横比描述了其宽度与高度之间的比例关系。
这就是我想要的: 具有单个子元素的容器元素 容器元素已被指定宽度和高度 子元素已被赋予长宽比 子元素应该是...
我拼命尝试将图像的长宽比更改为 16:9 而不拉伸它。 .aspectRatio(16/9, contentMode: .fill 或 .fit) 不可用,因为它纯粹是垃圾(地球上没有人...
我正在尝试使用此处详细介绍的“符号”方法来使用 SVG 精灵表。 http://css-tricks.com/svg-sprites-use-better-icon-fonts/ 我的 HTML 非常简单。 我正在尝试使用此处详细介绍的“符号”方法来使用 SVG 精灵表。 http://css-tricks.com/svg-sprites-use-better-icon-fonts/ 我的 HTML 非常简单。 <svg><use xlink:href="/images/iconSprite.svg#camera"/></svg> 这是 SVG 文件中的示例符号 <symbol viewBox="0 0 24 24" id="clock"><g transform="translate(0 -1028.4)"><path d="M22.085 1035.955a10.997 10.997-23.5 1 1-20.17 8.77 10.997 10.997-23.5 1 1 20.17-8.77z" fill="#1abc9c"/><path d="M21 1040.335a9 9 0 1 1-18 0 9 9 0 1 1 18 0z" fill="#ecf0f1"/><path d="M1.034 1039.8c-.083 1.7.176 3.3.875 4.9 2.42 5.6 8.898 8.2 14.468 5.8 4.29-1.9 6.778-6.2 6.593-10.6-.202 4-2.63 7.8-6.592 9.6-5.57 2.4-12.047-.2-14.47-5.8-.556-1.2-.82-2.6-.874-3.9z" fill="#16a085"/><path d="M20 1040.4c0 .5-.448 1-1 1h-6v-2h6c.552 0 1 .4 1 1z" fill="#3498db"/><path d="M12 1033.4c-.552 0-1 .448-1 1v5h2v-5c0-.552-.448-1-1-1z" fill="#2c3e50"/><path fill="#c0392b" d="M6.017 1045.705l4.95-4.95.707.707-4.95 4.95z"/><path d="M12 1038.4c-1.105 0-2 .9-2 2s.895 2 2 2 2-.9 2-2-.895-2-2-2zm0 1c.552 0 1 .4 1 1 0 .5-.448 1-1 1s-1-.5-1-1c0-.6.448-1 1-1z" fill="#34495e"/></g></symbol> 我遇到的问题是,当我使用 CSS 将 SVG 元素的宽度设置为 64px 时,SVG 的高度会自动设置为 150 像素。我尝试过设置 height:auto;高度:100%;在 SVG 元素上,但这没有什么区别。让它工作的唯一方法是设置 height:64px;我不想这样做,因为我的图标的长宽比可能并不总是方形的。我想要它做的是自动缩放 SVG 的原始宽高比,因此如果我将宽度设置为 400px,4:3 图标(由视图框定义)将自动获得 300px 的高度。 我已经阅读了几本关于缩放 SVG 和保留纵横比的指南,其中一些指南在使用 IMG 元素时提供了解决方案,但我找不到用于内联 SVGS 或通过 USE 使用外部 SVG 的解决方案。 有人知道适用于所有浏览器(包括 IE9+ 和 Android 4.0+)的解决方案吗? 演示 您需要在链接到的每个元素上定义一个 viewBox。 因为一个 SVG 文档 中可以有不同的元素。 因此,请从链接到的元素中删除 viewBox。 在您链接的位置添加 viewBox。 我建议你将 viewBox 添加到 svg 元素中: <svg class="test" viewBox="0 0 25 25"> <use xlink:href="/images/iconSprite.svg#clock"/> </svg> 现在您可以缩放它: 如果定义了 css 宽度: .test { width: 50px; } 只要适合高度或宽度,它就会显示。 因此,如果您想要响应式设计,只需将其缩放%长度 .test { width:30%; } 缩放 SVG 一篇关于 CSS 技巧缩放 svg 的非常好的文章:) 在旧版本的 Safari 以及一些旧的 Android 浏览器中,设置视图框和宽度是不够的。 在我的例子中,我需要应用 height:100%;以及宽度。 我需要一个全宽的 svg(用作分隔线)。 viewBox 解决方案对我不起作用 - 没有显示图像。我使用 Illustrator 导出 svg。 对我有用的是使用 div (或者在我的例子中是 hr)并照常调整其大小。这实际上适用于任何图像类型,而不仅仅是 svg。 不确定这是否解决了 OP 精灵问题,但它确实解决了标题问题:“高度:SVG 上的自动不起作用”。 .svg-image { background-image: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='10' height='10'><linearGradient id='gradient'><stop offset='10%' stop-color='%23F00'/><stop offset='90%' stop-color='%23fcc'/> </linearGradient><rect fill='url(%23gradient)' x='0' y='0' width='100%' height='100%'/></svg>"); background-size: cover; width: 100vw; height: calc(100vw * 50 / 1000); /* viewport width * image height / image width */ height: 100px; } <hr class="svg-image"> 使用svg作为背景,然后设置
当保留相等的纵横比时,我面临着 matplotlib 的奇怪行为。 对于初学者来说,这是展示此行为的最小代码示例。首先是正确的行为,其次......
如何在 ExoPlayer-V2 中缩放视频 - 全屏播放视频
我正在 Exoplayer 上从 URL 播放视频,它在调整大小/使用 resize_mode 上拉伸视频,正如我在布局文件中提到的那样,使用此我无法保持视频的宽高比....
我需要找到一种方法将输入光栅图像(例如jpg)重新调整为指定的宽度/高度分辨率(以像素为单位)。如果 PyQt 在调整新图像大小时能够保留原始图像,那就太好了......
我对编程还比较陌生。我需要根据给定尺寸(例如 axb)计算纵横比(16:9 或 4:3)。我如何使用 C# 来实现这一点。任何帮助将不胜感激。 公共...
如何在 HTML 和 CSS 中创建包含两行文本的响应式圆圈?
身体{ 显示:柔性; 调整内容:居中; 对齐项目:居中; 间隙:15px; } 。圆圈 { 显示:柔性; 弯曲方向:列; 对齐项目:居中; 调整内容:居中;
我正在尝试将object-fit: contains 与aspect-ratio: 1.5 /*硬编码数字*/结合在img上。 然而,图像仍然是方形的: // Javascript 例如仅调整面板大小。 常量
如何在 matplotlib 中共享轴并仅使用一个相等的长宽比
我想生成一个图形,在左侧显示二维数据,在右侧显示所述数据的垂直切片。 我使用以下 MWE 成功完成了此操作: 将 matplotlib.pyplot 导入为 plt ...
需要设置16:9 / 4:3 / 1:1宽高比 我使用下面的代码来做到这一点 `cameraExecutor = Executors.newSingleThreadExecutor() imgCaptureExecutor = Executors.newSingleThreadExecutor()
我有一个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>
SwiftUI 中的图像在尝试设置宽高比时在底部占用额外空间
我想让图像占据其容器的整个宽度,高度将基于明确提供的宽高比。此外,图像应该覆盖其所有框架,并且无论其内容是...
当尝试将应用了纵横比的元素居中时,我遇到了一个奇怪的问题。我认为它的行为与图像居中的工作方式类似,但我总是以......
这是我的问题的最小示例: 这是我的问题的最小示例: <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> 希望这足以让您玩并获得您想要的东西,如果这不能完全回答所有问题,请通过编辑为您的问题添加详细信息,我可以更新我的答案
我有一个运行完美的网页,在 ResizeObserver 的帮助下调整了子画布元素(带有兔子的棋盘)的大小,同时保持其 1:1 的纵横比,当浏览器...
是否可以使用纵横比作为 CSS 中 calc() 的一部分?或者使用元素自己的宽度作为 calc() 的一部分?
我知道我可以使用宽高比使元素的高度等于其自身宽度的特定折叠/部分,例如宽高比:1 / 2 将使元素的高度是 m 的两倍...
我在数字输入组件中使用 Flex 来在输入本身旁边呈现自定义“+”和“-”按钮。按钮使用宽高比:1/1;确保它们始终是正方形,但我似乎无法理解...
我正在使用 Android Studio,我希望能够从图库中拾取图像,调整其大小并通过保持正确的宽高比使其适合圆角正方形(约 300x300px)...
为什么纵向模式 480 x 640 的分辨率被描述为 4 : 3 而不是 3 : 4?
正如我所读到的: 图像的纵横比是其宽度与高度的比率。例如,如果矩形的长宽比为 2:1,则其宽度是高度的两倍。方面...