草庐IT

Android VideoView crop_center

coder 2023-11-22 原文

我有一个 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
    }
}

这对我有用。希望这会对某人有所帮助。

关于Android VideoView crop_center,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19429811/

有关Android VideoView crop_center的更多相关文章

  1. javascript - Photoswipe 无法读取 photoswipe.js 第 1070 行未定义的属性 'center' - 2

    我只是涉足photoswipe,到目前为止,我没有做任何更高级的事情,只是通过一些非常小的(理论上微不足道的调整)简单地实现入门演示的副本。我的图片库显示得很好,我总共有4个项目,因为我只是想测试一下。第一张图片将缩放和平移,所有这些都很棒。但是,在我切换图片的那一刻,我在这篇文章的标题中收到了javascript错误。我使用以下作为我的元素:varitems=[{"src":"/Images/Portfolio/Pieces/PhoenixFury.jpg","thumbnail":"/Images/Portfolio/Thumbs/PhoenixFury.jpg","msrc":"

  2. javascript - Google map "center_changed"不止一次触发我的功能 - 2

    正如标题所暗示的,我只是在我的map上添加了一个“center_changed”监听器并且该函数运行了不止一次。我假设这是因为map的中心在map停止之前改变了很多次,但我认为这就是“拖动”的目的,而“center_changed只在它停止后触发一次???我知道它多次触发的唯一原因是因为我在图标上有一个阴影,它在完全黑色之前的大约两秒内变得越来越暗。如果有人需要我的代码,请在下面。google.maps.event.addListener(map,'center_changed',function(){varzoomLevel=map.getZoom();if(zoomLevel>7)

  3. javascript - 谷歌地图圈 : how to trigger an event when moved and how to obtain the new center - 2

    所以我能够在我的谷歌地图v3上制作一个圆形对象作为叠加层。我将其可编辑属性设置为true。接下来我想做的是在用户移动圆圈时获取圆心的坐标。为此,我需要某种响应事件而触发的方法。我以为我已经在初始化函数中设置了这一切,如下所示。但是,我没有收到任何警告框。所以我假设这个响应事件的函数没有被触发。functioninitialize(){cityCenterLatLng=newgoogle.maps.LatLng(cLat,cLong);options={center:cityCenterLatLng,zoom:15,mapTypeId:google.maps.MapTypeId.ROAD

  4. javascript - TinyMCE 使用 <div align ="center"> 而不是 <div style ="text-align: center">? - 2

    我使用的是TinyMCE3.2.5,默认情况下,当您单击居中对齐按钮时,它使用内联样式.我想使用tinyMCE而不是内联样式。我知道在配置中设置inline_styles:false是有效的。但我想对除对齐以外的所有内容使用内联样式。我将如何改变它? 最佳答案 tinyMCE.init({...'formats':{'alignleft':{'selector':'p,h1,h2,h3,h4,h5,h6,td,th,div,ul,ol,li,table,img',attributes:{"align":'left'}},'align

  5. javascript - react native map : center marker onPress before Callout is displayed - 2

    当用户按下标记时,我调用animateToCoordinate使标记居中于屏幕中间。问题是标注在animateToCoordinate完成其动画之前呈现到区域的位置,因此标注显示在屏幕外。无论如何要延迟标注显示直到animateToRegion完成?还是有另一种我没有看到的完全解决这个问题的方法?{_mapView=mapView;}}style={styles.map}region={this.state.mapRegion}showsUserLocation={true}showsMyLocationButton={true}followUserLocation={true}onR

  6. javascript - 卡在 Slick Carousel "Center mode" - 2

    我已经研究了Slickcarousel好几个小时了,我真的不知道如何实现Slick网站上的“中心模式”:http://kenwheeler.github.io/slick/看起来像这样:我已经准备好当前代码,但它仍然没有给我想要的东西:$(document).ready(function(){$('.center').slick({centerMode:true,centerPadding:'60px',slidesToShow:3,responsive:[{breakpoint:768,settings:{arrows:false,centerMode:true,centerPadd

  7. android - 相对布局-android :layout_centerHorizontal ="true" AND android:gravity ="center_horizontal" showing unexpected results - 2

    我试图将一些TextViews对齐到RelativeLayout中的水平中心,但在使用某些属性时得到了一些意想不到的结果:为什么第二个'SpringSummerAutumnWinter'TextView和第一个'HelloWorld'TextView移动到水平中心(即使它们都使用不同的水平文本对齐属性)?对顶部的TextView使用android:layout_centerInParent="true"将其移动到屏幕中央(这不是我想要的)是否有可用于将所有TextViews对齐到水平中心的属性? 最佳答案 layout_center

  8. java - Android - 相对布局的ImageView中的Center Textview - 2

    我有一个带有ImageView的相对布局,我想在Activity布局xml文件中将TextView置于ImageView的中心。这是ImageView,然后是我为TextView尝试的内容这是相对布局的内部:我认为它看起来不错,但是marginTop是65不应该是imageView高度的一半吗?提前感谢您的帮助。 最佳答案 它不起作用,因为您告诉TextView位于父级RelativeLayout的中心,但ImageView附加到父级的左上角。一种方法是使用RelativeLayout的alignXyz属性强制TextView的大小

  9. java - 有没有办法用java打开 "Windows Mobility Center"? - 2

    我想你们中的大多数人都听说过“Windows移动中心”。这是windows的应用程序,可以改变音量、亮度等。我想问你有没有办法打开那个“Windows移动中心”使用Java代码?感谢您的回答,抱歉,如果我不具体。​​ 最佳答案 Windows移动中心位于C:\Windows\System32\所以你只需要运行这个cmd命令:C:\Windows\System32\mblctr.exe它应该像这样工作:Runtime.getRuntime().exec("C:\\Windows\\System32\\mblctr.exe")

  10. c# - Pad Left & Pad Right (Pad Center) 字符串 - 2

    String有PadLeft和PadRight。我需要左右填充(居中对齐)。是否有执行此操作的标准化方法,或者更好的是,是否有实现相同目标的内置方法? 最佳答案 据我所知没有。如果您发现自己经常使用它,则可以创建一个扩展方法。假设您希望您的字符串在中心结束,请使用如下内容publicstringPadBoth(stringsource,intlength){intspaces=length-source.Length;intpadLeft=spaces/2+source.Length;returnsource.PadLeft(pad

随机推荐