2017-03-18 38 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