SurfaceView in Layout

流过昼夜 提交于 2019-11-30 13:50:40

I am just putting down the changes.

  1. Replace surface view in the xml with LinearLayout.

        <LinearLayout 
           android:id="@+id/middleSurface"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
        />
    
  2. Get an instance of linear layout

    LinearLayout surface = (LinearLayout)findViewById(R.id.surface);

  3. Add the surface view's instance to LinearLayout

    surface.addView(new MagnetView(this));

2,3 steps should be performed after setContentView(R.layout.main);

I would examine the layout in the Eclipse UI designer to see if everything is placed as you want to. Surface Views are just ordinary views in regards to layout.

If I am not wrong, the way I perceived your query is: You want something to be displayed/rendered on top of the surface view along with the other view elements as you mentioned in the xml.

If you want a view on top of surface view, you need to extend it(AFAIK there is no other way). Then you need to override the onDraw() method to display what all things you want on the surface view. You also need to get an object of SurfaceHolder which gets the canvas.

Here is a good link showing, putting an imageview on top of surface view: http://www.droidnova.com/playing-with-graphics-in-android-part-ii,160.html

  1. Remove the surface view from the xml and place a linearlayout in place of that and associate an id with it.
  2. Get an instance of linearlayout.
  3. add child view i.e., magnet view in this case to the linearlayout using

    addView()

This solved the problem.

Have you set the background of the SurfaceView? I found that if you set the background of the SurfaceView, it will not display what you draw on the secondary thread.

Alv_quena

You could try this other way, move setContentView(R.layout.main); after super.onCreate(savedInstanceState);

super.onCreate(savedInstanceState);
setContentView(R.layout.main);
midSurf = new MagnetView(this);
LinearLayout midLL = new LinearLayout(this);

midLL.findViewById(R.id.middlesurface);
midLL.addView(midSurf);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!