0

I'm currently developing an Android App, using a personal SurfaceView and double buffering. However I'm facing a little problem with my code.
In one hand I have an xml view, based on LinearLayout hierarchy. When I instantiate my activity, I set my contentView on this xml. The problem is then that my double buffering don't works anymore. Thread is running but nothing is displayed.
In the other hand, I set my contentView with a new personal SurfaveView element and display works fine. But of course, I cannot access anymore to the other elements of my LinearLayout.

Actually, I would like to set my contentView on my xml view AND keep my display working.

I hope I was clear enough, thank you for your answers!

Here is my activity:

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);

        ...
    }
    ...
}

My surface view:

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();
    }
    ...
}

And my xml view:

<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>

EDIT:
After further researches and analyses, it appears clearly that:

  • setContentView(R.layout.main_controller_activity): in this case I get all the elements from my activity, but the MCustomDrawableView display nothing.
  • setContentView(new MCustomDrawableView(getApplicationContext())): on that case, MCustomDrawableView is working well (it displays what I want), but I don't have the others View from my main_controller_activity

In both cases:

  • my thread is running and works well.
  • my drawing function is called as well, with the holder.lockCanvas() and holder.unlockCanvasAndPost(bufferCanvas) methods.
4

1 回答 1

0

好吧,我找到了一个功能解决方案:

我认为问题来自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。

我仍然对其他解决方案持开放态度!

于 2015-01-08T11:12:12.787 回答