2017-03-18 96 views
1

我试图将VideoView从背面带到前面,但没有任何工作。在下面,我将首先介绍代码,然后我将介绍我尝试的内容,并希望有人能够解决我的问题:Android VideoView BringToFront在Xamarin中不能正常工作

这是使用Xamarin编写的Android应用程序的简化版本。

事不宜迟,这里是我的代码:

using Android.App; 
using Android.Widget; 
using Android.OS; 
using Android.Media; 
using System; 

namespace Experiment 
{ 
    public class Listener : Java.Lang.Object, MediaPlayer.IOnCompletionListener 
    { 
     private Action action; 

     public Listener(Action action) 
     { 
      this.action = action; 
     } 

     public void OnCompletion(MediaPlayer unused) 
     { 
      this.action(); 
     } 
    } 

    [Activity(Label = "Experiment", MainLauncher = true, Icon = "@mipmap/icon")] 
    public class MainActivity : Activity 
    { 
     private VideoView foreView; 
     private VideoView backView; 

     protected override void OnCreate(Bundle savedInstanceState) 
     { 
      base.OnCreate(savedInstanceState); 

      // Set our view from the "main" layout resource 
      SetContentView(Resource.Layout.Main); 

      this.foreView = this.FindViewById<VideoView>(Resource.Id.foreView); 
      this.backView = this.FindViewById<VideoView>(Resource.Id.backView); 

      this.foreView.SetVideoURI(Android.Net.Uri.Parse("...")); 
      this.foreView.Start(); 
      this.foreView.SetOnCompletionListener(new Listener(this.OnForeViewCompleted1)); 

      this.backView.SetVideoURI(Android.Net.Uri.Parse("...")); 
      this.backView.SetOnCompletionListener(new Listener(this.OnBackViewCompleted1)); 
     } 

     private void OnForeViewCompleted1() 
     { 
      this.backView.Start(); 
      this.foreView.SetVideoURI(Android.Net.Uri.Parse("...")); 
      this.backView.BringToFront(); 
     } 

     private void OnBackViewCompleted1() 
     { 
      this.foreView.SetOnCompletionListener(null); 
      this.foreView.Start(); 
      this.foreView.BringToFront(); 

     } 
    } 
} 

出于保护隐私的原因 - 实际视频网址被替换成“...”,这可以起到MP4文件将工作的任何URL。

这里是与活动

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
    <VideoView 
     android:id="@+id/backView" 
     android:layout_width="200px" 
     android:layout_height="200px" 
     android:layout_marginTop="0px" 
     android:layout_marginLeft="0px" /> 
    <VideoView 
     android:id="@+id/foreView" 
     android:layout_width="200px" 
     android:layout_height="200px" 
     android:layout_marginTop="0px" 
     android:layout_marginLeft="50px" /> 
</RelativeLayout> 

相关联的布局我打算有foreView完全闭塞backView,在这个例子中,然而,我故意foreView 50像素转移到以显示正确的效果。

完整的,可运行Xamarin的解决方案可以在我的GitHub repository

当我们运行程序时,它首先会起到对foreView的视频,在建成后,将发挥对backView视频可以发现,在完成它试图再次调用foreView并播放foreView,但它不会显示出来遮挡backView,而是backView遮挡了frontView,这是我需要解决的问题。

上述问题,这里是我尝试:

foreView.Invalidate() does not work. 
foreView.RequestLayout() does not work. 
foreView.Parent.RequestLayout() does not work. 

上拉请求解决这一问题将是真棒,非常感谢事先!

回答

0

我试过一些方法将VideoView放在前面,但它不像你所说的那样工作。

但是,您可以找到VideoView Z顺序级别取决于添加的顺序。

因此,作为一种变通方法,我改变了另外两个VideoView的顺序:

private void OnForeViewCompleted1() 
    { 
     //backView.BringToFront(); 
     //backView.SetZOrderOnTop(true); 
     //myfatherLayout.UpdateViewLayout(backView, backView.LayoutParameters); 
     myfatherLayout.RemoveView(foreView); 
     myfatherLayout.AddView(foreView); 
     img1.BringToFront(); 
     this.backView.Start(); 
    } 

    private void OnBackViewCompleted1() 
    { 
     //foreView.BringToFront(); 
     // foreView.SetZOrderOnTop(true); 
     //myfatherLayout.UpdateViewLayout(foreView, foreView.LayoutParameters); 
     myfatherLayout.RemoveView(backView); 
     myfatherLayout.AddView(backView); 
     img2.BringToFront(); 
     this.foreView.Start(); 
    } 

myfatherLayout是视频视图容器(您RelativeLayout)。

你可以看到视频观看将显示为您的要求:

enter image description here