0

我目前正在開發一個Android應用程序,使用個人SurfaceView和雙緩衝。但是我的代碼遇到了一些問題。
一方面,我有一個基於LinearLayout層次結構的xml視圖。當我實例化我的活動時,我在此xml上設置了我的contentView。問題是,我的雙緩衝不再工作了。線程正在運行,但沒有顯示。
另一方面,我將我的contentView設置爲新的個人SurfaveView元素,並且顯示效果良好。但是,當然,我不能訪問我的LinearLayout的其他元素。

其實,我想設置我的contentView在我的xml視圖保持我的顯示工作。

我希望我已經夠清楚了,謝謝你的回答!LinearLayout上使用個人SurfaceView

這裏是我的活動:

public class MainController extends Activity { 
    @Override 
    protected void onCreate(final Bundle p_savedInstanceState) { 
     super.onCreate(p_savedInstanceState); 
     setContentView(R.layout.main_controller_activity); 

     this.drawableView = (MCustomDrawableView) findViewById(R.id.drawingBox); 

     ... 
    } 
    ... 
} 

我的面視圖:

public class MCustomDrawableView extends SurfaceView { 
    public MCustomDrawableView(Context p_context, AttributeSet p_attributes) { 
     super(p_context, p_attributes); 

     this.getHolder().addCallback(new SurfaceHolder.Callback() { 
      @Override 
      public void surfaceCreated(final SurfaceHolder p_holder) { 
       try { 
        // Instantiating a new thread 
        thread = new MBufferingThread(getInstance()); 

        thread.start(); 

       } catch (Exception exception) { 
        exception.printStackTrace(); 
       } 
      } 

      @Override 
      public void surfaceChanged(final SurfaceHolder p_holder, int p_format, int p_width, int p_height) {...} 

      @Override 
      public void surfaceDestroyed(final SurfaceHolder p_holder) {...} 
     }); 

     // Setup drawing options 
     setupDrawing(); 
    } 
    ... 
} 

我的XML視圖:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:background="@color/app_background" 
    tools:context=".MainController"> 

    <com.iskn.calligraphy.models.draw.MCustomDrawableView 
     android:id="@+id/drawingBox" 
     style="@style/DrawingBox" /> 

    <SeekBar 
     android:id="@+id/brushSize" 
     style="@style/SizeSeekBar" /> 

    <SeekBar 
     android:id="@+id/brushOpacity" 
     style="@style/OpacitySeekBar" /> 

</LinearLayout> 

編輯:
經過進一步的研究和分析,它顯得清晰RLY是:

  • setContentView(R.layout.main_controller_activity):在這種情況下,我得到的所有從我的活動元素,但MCustomDrawableView就什麼都不顯示。
  • setContentView(new MCustomDrawableView(getApplicationContext())):在這種情況下,MCustomDrawableView運作良好(它顯示我想要的),但我不從我main_controller_activity

有其他View在兩種情況下:

  • 我線程正在運行並運行良好。
  • 我的繪圖功能也被調用,使用holder.lockCanvas()holder.unlockCanvasAndPost(bufferCanvas)方法。
+0

什麼是「個人」SurfaceView? SurfaceView已經是雙緩衝或三緩衝,爲什麼要添加更多的緩衝?如果問題出在緩衝區或繪圖上,則應顯示該代碼。 – fadden

+0

謝謝你的回答。我所謂的「個人」SurfaceView就是我的MCustomDrawableView類,它擴展了SurfaceView。在我的情況下,雙緩衝允許我在緩衝畫布上執行繪圖動作。然後,我顯示一個(主)的畫布。實際上這部分工作正常,我唯一的問題是我的主要活動和擴展SurfaceView的這個類之間的聯繫。 – Sierra

回答

0

好吧,我發現了一個functionnal解決方案:

我認爲這個問題是從MCustomDrawableView初始化到來。我從onCreate方法創建了該類的新實例,而不是從xml文件創建。然後,我將它設置爲:contentView

protected void onCreate(Bundle p_savedInstanceState) { 
     super.onCreate(p_savedInstanceState); 
     setContentView(R.layout.main_controller_activity); 

     // Instantiate drawable object & set style properties 
     this.drawableView = new MCustomDrawableView(getApplicationContext()); 
     FrameLayout.LayoutParams style = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, 
       FrameLayout.LayoutParams.MATCH_PARENT); 

     // Add the drawable view to the main_activity 
     addContentView(this.drawableView, style); 
    } 

但是這種方式引發了一個新問題。添加的視圖在頂部,這意味着所有其他人View被隱藏。我用bringToBack(this.drawableView)下面的方法解決了這個問題:

private void bringToBack(View p_view) { 
    // Get parent from the current view 
    ViewGroup viewGroup = ((ViewGroup) p_view.getParent()); 

    int childrenCount = viewGroup.indexOfChild(p_view); 
    for(int cpt = 0; cpt < childrenCount; cpt++) { 
     // Move the child to the top 
     viewGroup.bringChildToFront(viewGroup.getChildAt(cpt)); 
    } 
} 

它將提供的視圖帶回到背景位置。我還必須將LinearLayout的alpha設置爲0。

我仍然對其他解決方案開放!