【问题标题】:Using personal SurfaceView on LinearLayout在 LinearLayout 上使用个人 SurfaceView
【发布时间】:2015-03-02 17:50:32
【问题描述】:

我目前正在开发一个 Android 应用程序,使用个人 SurfaceView 和双缓冲。但是,我的代码遇到了一点问题。
一方面,我有一个基于LinearLayout 层次结构的xml 视图。当我实例化我的活动时,我在这个 xml 上设置了我的 contentView。那么问题是我的双缓冲不再起作用了。线程正在运行,但没有显示任何内容。
另一方面,我将我的contentView 设置为一个新的个人SurfaveView 元素并且显示效果很好。但当然,我无法再访问我的 LinearLayout 的其他元素。

实际上,我想在我的 xml 视图上设置我的contentView并且保持我的显示正常工作。

我希望我已经足够清楚了,谢谢你的回答!

这是我的活动:

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>

编辑:
经过进一步的研究和分析,清楚地表明:

  • setContentView(R.layout.main_controller_activity):在这种情况下,我从我的活动中获取了所有元素,但 MCustomDrawableView 什么也不显示。
  • setContentView(new MCustomDrawableView(getApplicationContext())):在这种情况下,MCustomDrawableView 运行良好(它显示了我想要的),但我的 main_controller_activity 没有其他 View

在这两种情况下:

  • 我的线程正在运行并且运行良好。
  • 我的绘图函数也被调用,使用 holder.lockCanvas()holder.unlockCanvasAndPost(bufferCanvas) 方法。

【问题讨论】:

  • 什么是“个人”SurfaceView? SurfaceView 已经是双缓冲或三缓冲的,为什么还要添加更多缓冲?如果问题出在缓冲或绘图上,您应该显示该代码。
  • 感谢您的回答。我所谓的“个人”SurfaceView 只是我的 MCustomDrawableView 类,它扩展了 SurfaceView。就我而言,双缓冲允许我在缓冲的画布上执行绘图操作。然后,我将此画布发布在显示的一个(主)上。实际上这部分效果很好,我唯一的问题是我的主要活动和扩展 SurfaceView 的类之间的链接。

标签: android android-linearlayout surfaceview double-buffering setcontentview


【解决方案1】:

好吧,我找到了一个实用的解决方案:

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

我仍然愿意接受其他解决方案!

【讨论】:

    猜你喜欢
    • 2012-01-28
    • 1970-01-01
    • 1970-01-01
    • 2015-05-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多