2013-04-23 135 views
0

在我的應用程序中,我必須提供添加,移動,調整大小和刪除一些視圖的能力,就像啓動器一樣。它直到現在都運行良好,但現在我希望其中一些視圖能夠實時顯示視頻(如網絡攝像頭)。如何正確處理android:configChanges =「orientation | screenSize」

當我改變屏幕方向時,我希望我的視頻不要重新緩衝,所以我嘗試使用android:configChanges =「orientation | screenSize」屬性。

它適用於大多數情況,但是...問題是我必須在屏幕方向更改或onResume函數中獲取新的尺寸。這有點棘手,並不總是有效(例如,如果我處於橫向模式,並且我接到電話,Android會自動切換到縱向模式,當我回到我的應用程序時,它會保持縱向但給我的景觀屏幕尺寸...)。

我試着同時使用addOnLayoutChangeListener和getViewTreeObserver()。addOnGlobalLayoutListener,都失敗了。

我想這可能是在其他幾種情況下的錯誤,所以我認爲完美的行爲將是正常行爲(例如,如果方向改變,銷燬並在onResume中重新創建我的活動),並調整視圖的大小隻有在方向改變時手動。

所以我的問題是:

  • 你是否認爲這是一個好主意,試圖做得到這個行爲?

  • 什麼是銷燬和重新創建活動的最佳方式?如果方向改變,只需在onResume中調用onDestroy/onCreate?或手動設置我所有的指針爲null,並且非常小心內存泄漏...

預先感謝您

+0

我很遺憾沒有你的問題的答案,但是甚至不考慮手動調用系統生命週期回調。在'onResume()'中調用'onCreate()'是一個非常糟糕的主意。此外,在理想的世界中,您只需通過「NonConfigurationInstance」調用將玩家對象從被銷燬的活動傳遞給新出生的對象。但是,我似乎記得,對於視頻來說可能會有困難。 – 2013-04-23 11:28:05

+0

感謝您的回答。我已經嘗試通過NonConfigurationInstance傳遞媒體播放器,但是...我們並不處於理想的世界,並且MediaPlayer似乎不可能:/ – 2013-04-23 13:43:10

+0

是的,這就是我所記得的。不過,它幾乎可以與其他所有產品配合使用。 ;)無論如何,我不確定你的實際問題是什麼。什麼不適合你? - 關於很多事情,通常認爲應該在'onCreate()'中完成它們,當它們應該在'onResume()'中完成時。 ;) – 2013-04-23 13:51:45

回答

0

配置更改好吧,我通過創建自己的佈局類並通過在onDraw函數中獲取大小來解決它。有點矯枉過正,但它是我找到的唯一解決方案。

感謝@Sharad Mhaske和@Class Stacker爲您的時間!

編輯:我意識到,我沒有提到,我通過設置他們的X和Y位置,以及他們的寬度和高度手動將我的意見放在他們的容器內。這就是爲什麼他們沒有自動調整大小,而容器呢。

編輯:這是我現在使用的代碼片:

public class TilesContainer extends FrameLayout { 

    // ------------------------------------------------------------------------------------------ 
    // Public interface for on size change event : 
    public static interface OnSizeChangedListener { void onSizeChanged(int width, int height); } 
    // ------------------------------------------------------------------------------------------ 



    // ------------------------------------------------------------------------------------------ 
    // Private attributes : 
    private int _width = -1; 
    private int _height = -1; 
    private OnSizeChangedListener _listener = null; 
    // ------------------------------------------------------------------------------------------ 



    // ------------------------------------------------------------------------------------------ 
    // Constructors : 
    public TilesContainer(Context context, AttributeSet attrs, int defStyle) { 

     super(context, attrs, defStyle); 
     this.setWillNotDraw(false); 
    } 

    public TilesContainer(Context context, AttributeSet attrs) { 

     super(context, attrs); 
     this.setWillNotDraw(false); 
    } 

    public TilesContainer(Context context) { 

     super(context); 
     this.setWillNotDraw(false); 
    } 
    // ------------------------------------------------------------------------------------------ 


    // ------------------------------------------------------------------------------------------ 
    // Setters : 
    public void setOnSizeChangedListener(OnSizeChangedListener listener) { _listener = listener; } 
    // ------------------------------------------------------------------------------------------ 



    // ------------------------------------------------------------------------------------------ 
    // onDraw overload : 
    @Override 
    protected void onDraw(Canvas canvas) { 

     // Call super : 
     super.onDraw(canvas); 

     // If there is a listener and if the size changed : 
     if(_listener != null && (_width != this.getWidth() || _height != this.getHeight())) { 

      _width = this.getWidth(); 
      _height = this.getHeight(); 
      _listener.onSizeChanged(this.getWidth(), this.getHeight()); 
     } 
    } 
    // ------------------------------------------------------------------------------------------ 
} 

現在在活動中,中庸之道實施OnSizeChangedListener,使用setter和新增功能:

@Override 
public void onSizeChanged(int width, int height) { 

    // Do your stuff with the GOOD size ! 
}