0

我試圖創建一個自定義表面視圖,其中每次視圖在屏幕上視圖都將自行開始播放視頻。我想知道當用戶界面上顯示一個視圖並且用戶看到視圖時,通知哪個方法。我正在使用viewpager,所以SurfaceCreated不起作用,因爲視圖是在屏幕上顯示之前創建的。當在UI上顯示視圖時,通知視圖內的什麼方法

+0

您是否嘗試過三種大視圖佈局傳球之一(請先檢查以確保您只開始播放視頻一次) - 「繪製」,「佈局」和「測量」?由於(和* only * since)你使用了一個視圖尋呼機,你可能別無選擇,只能使用'draw'-不要忘記調用super。最後,如果這是一個視圖尋呼機,你有沒有考慮在動畫中設置一個'AnimationListener',它將你的視頻視圖放入並開始播放視頻'onAnimationEnd'-這就是我所要做的。 – Tom

+0

我試過,但onDraw似乎並沒有被調用。表面正在被創建和銷燬。 – Papajohn000

+0

我剛剛意識到我使用的是SurfaceView,而不是View。我不知道這是否會改變事情。 – Papajohn000

回答

1

如何在一個視圖尋呼機自動啓動視頻,當談到屏幕

上這是根本問題。 OP明智地想要嘗試和隔離它在某種意義上的「進入屏幕」的地步。問題是這可能意味着很多事情:

當我第一次聽到這個問題時,我認爲一個好的例子是onAttachedToWindow - 請參見docs。對於基於原始標題閱讀這個問題的人來說,這就是你想要的。

該視圖在活動的onCreate 中大多數情況下爲(例如,如果您使用過setContentView)創建並創建。

OP沒有使用surfaceCreated回調的運氣。因此我們在上述評論中考慮了OP是否會對layoutmeasuredraw三個繪製階段感興趣。在android中實際上「在屏幕上放置視圖」有兩個階段 - 測量和佈局階段 - 見here

問題是,事實證明,這是OP是動畫他的觀點在屏幕上,所以問題變成了怎麼樣,當動畫後視圖「到達」在屏幕上。

重要的一點是:您實際上想要在繪圖過程中稍後檢測舞臺,這是可以理解的!動畫通過invalidate的許多調用工作,而該視圖的Canvas需要許多draw - 因此,在用戶界面中首次顯示視圖時,您想要播放視頻的階段決不是。

針對此特定場景的解決方案

ViewAnimator實例上使用動畫偵聽器(例如ViewPager)。爲了不必在德的活動與他們打擾,我會推出自己的觀點,然後用Adapter型圖案的Android這麼喜歡來管理不斷變化的數據:

非常倉促寫成的實現將是:

public class VideoStartingViewFliper extends ViewFlipper { 
private final Animation fromRight; 
private final Animation toLeft; 
private final Animation fromLeft; 
private final Animation toRight; 
private VideoViewAdapter mAdapter; 

public VideoStartingViewFliper(final Context context, final AttributeSet attrs) { 
    super(context, attrs); 
    fromRight = new YourChoiceOfAnimation(); 
    fromRight.setAnimationListener(videoStartingAnimationListener); 

    toLeft = new YourChoiceOfAnimation(); 
    toLeft.setAnimationListener(videoStartingAnimationListener); 

    fromLeft = new YourChoiceOfAnimation(); 
    fromLeft.setAnimationListener(videoStartingAnimationListener); 

    toRight = new YourChoiceOfAnimation(); 
    toRight.setAnimationListener(videoStartingAnimationListener); 
} 

static interface VideoViewAdapter { 

    public String getVideoPath(int childId); 

} 

public void setVideoViewAdapter(final VideoViewAdapter adapter) { 
    mAdapter = adapter; 
} 

// or even call this showNextVideo and don't override! 
@Override 
public void showNext() { 
    setInAnimation(fromRight); 
    setOutAnimation(toLeft); 
    super.showNext(); 
} 

@Override 
public void showPrevious() { 
    setInAnimation(fromLeft); 
    setOutAnimation(toRight); 
    super.showPrevious(); 

} 

private final AnimationListener videoStartingAnimationListener = new AnimationListener() { 

    @Override 
    public void onAnimationStart(final Animation animation) { 
     final VideoView video = ((VideoView) getCurrentView()); 
     video.stopPlayback(); 
    } 

    @Override 
    public void onAnimationRepeat(final Animation animation) { 

    } 

    @Override 
    public void onAnimationEnd(final Animation animation) { 
     final VideoView video = ((VideoView) getCurrentView()); 
     // check null here! 
     video.setVideoPath(mAdapter.getVideoPath(getCurrentView().getId())); 
     video.start(); 
    } 
}; 
} 

希望這會有所幫助。

相關問題