Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/196.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 布局中的表面视图_Android_Android Layout_Layout_Surfaceview - Fatal编程技术网

Android 布局中的表面视图

Android 布局中的表面视图,android,android-layout,layout,surfaceview,Android,Android Layout,Layout,Surfaceview,所以我做了很多搜索,但似乎仍然找不到确切的原因来解释为什么我的SurfaceView无法显示。以下是我所做工作的一些背景: 我有一个水平设置的线性布局。它包含一个ImageView,然后是一个垂直线性布局,最后是另一个ImageView。在垂直线性布局中,有三个要素:顶部的另一个ImageView、扩展的SurfaceView(称为MagnetView)和下方的ImageView 以下是xml: <LinearLayout xmlns:android="http://schemas.and

所以我做了很多搜索,但似乎仍然找不到确切的原因来解释为什么我的SurfaceView无法显示。以下是我所做工作的一些背景:

我有一个水平设置的线性布局。它包含一个ImageView,然后是一个垂直线性布局,最后是另一个ImageView。在垂直线性布局中,有三个要素:顶部的另一个ImageView、扩展的SurfaceView(称为MagnetView)和下方的ImageView

以下是xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >

 <!-- Left Magnetrak -->

 <ImageView
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:src="@drawable/vectorparts_01"
    android:adjustViewBounds="true" />

<!-- Middle Linear Layout -->        
<LinearLayout 
    android:layout_width="0dip"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:layout_weight="1">

    <!-- Top Bar with Lifes and Score counter -->
    <RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:adjustViewBounds="true">

    <ImageView
    android:adjustViewBounds="true"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:src="@drawable/vectorparts_22"/>

    <!-- Score counter -->
    <TextView
    android:id="@+id/myImageViewText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_marginRight="10dp"
    android:layout_marginTop="5dp"
    android:gravity="center"
    android:text="000000000000"
    android:textColor="#000000" />

    <!-- Life1 -->
     <ImageView
        android:id = "@+id/life1"
        android:adjustViewBounds="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/vectorparts_13" 
        android:layout_alignParentLeft="true"
        android:layout_marginLeft="5dp"
        android:layout_marginTop="1dp"
        android:gravity="center" />

     <!-- Life2 -->
     <ImageView
        android:id = "@+id/life2"
        android:adjustViewBounds="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/vectorparts_13" 
        android:layout_toRightOf="@id/life1"
        android:layout_marginTop="1dp"
        android:layout_marginLeft="1dp"
        android:gravity="center" />

     <!-- Life3 -->
     <ImageView
        android:id = "@+id/life3"
        android:adjustViewBounds="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/vectorparts_13" 
        android:layout_toRightOf="@id/life2"
        android:layout_marginTop="1dp"
        android:layout_marginLeft="1dp"
        android:gravity="center" />

    </RelativeLayout>

    <!-- SurfaceView -->
    <LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/middlesurface">

    </LinearLayout>

     <!-- Bottom Bar -->   
    <RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:adjustViewBounds="true"> 

    <ImageView
    android:adjustViewBounds="true"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:src="@drawable/vectorparts_02"/>


    </RelativeLayout>


</LinearLayout>

<!-- Right Magnetrak -->
   <ImageView
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:src="@drawable/vectorparts_01"
android:adjustViewBounds="true" />
我应该将SurfaceView放在布局xml的顶部吗?我还需要设置更多属性吗? 我能把SurfaceView覆盖在其他所有东西上,使其半透明并画出我需要的东西吗?任何帮助都将不胜感激,因为我似乎无法理解SurfaceView是如何工作的。看起来它们是我的视图层次结构的一部分,但是文档告诉我它们完全不同

多谢各位

2012年4月17日编辑

要提供更多信息,请执行以下操作: 我的XML UI设计器在我的扩展的Survivew类中显示了一个大的框,称为MyMVIEW。我用红色标出了它的轮廓。(Stackoverflow还不允许我发布图片)

UI设计器视图(http://24.media.tumblr.com/tumblr_m2mmqvBNwW1qcreoco1_500.jpg)

以下是MagnetView(SurfaceView)类:


我将检查EclipseUI设计器中的布局,以查看是否所有内容都按照您的意愿放置。曲面视图只是布局方面的普通视图

如果我没有错的话,我对您的查询的理解是:您希望在曲面视图的顶部显示/呈现一些内容,以及xml中提到的其他视图元素

如果您想在曲面视图的顶部创建视图,则需要对其进行扩展(如果没有其他方法的话)。然后需要重写onDraw()方法,以显示曲面视图上所需的所有内容。您还需要获取一个SurfaceHolder对象,它可以获取画布

下面是一个很好的链接,将imageview放在曲面视图的顶部:

  • 从xml中删除曲面视图,并放置一个linearlayout来代替该视图,并将id与其关联
  • 获取linearlayout的实例
  • 使用以下命令将子视图(即磁铁视图)添加到线性布局中:

    addView()


  • 这就解决了问题

    我只是把变化记下来

  • 将xml中的曲面视图替换为LinearLayout

        <LinearLayout 
           android:id="@+id/middleSurface"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
        />
    
    
    
  • 获取线性布局的实例

    LinearLayout曲面=(LinearLayout)findViewById(R.id.surface)

  • 将曲面视图的实例添加到LinearLayout

    addView(新MagnetView(this))


  • setContentView(R.layout.main)之后应执行2,3个步骤

    您是否设置了SurfaceView的背景?我发现如果您设置SurfaceView的背景,它将不会显示您在辅助线程上绘制的内容。

    您可以尝试另一种方法,移动
    setContentView(R.layout.main)
    after
    super.onCreate(savedInstanceState)


    我已经编辑了我的帖子,其中包括了UI设计器的截图,我在其中概述了SurfaceView。它肯定在布局中,但它不会画出我想要的东西。我还发布了扩展的SurfaceView类(MagnetView)。它现在的功能是用黄色填充曲面。您确定调用了surfaceCreated()并且只有在调用它之后才开始绘图吗。您可以遵循LunarLander示例,它们在回调接口中添加的sdkI中以及surfaceCreated()(在OP中更新),但仍然无效。我在活动中设置内容视图的方式可能会有问题吗?我是否将我主要活动中的SurfaceView与我在xml中列出的SurfaceView正确链接?我已经更新了帖子,提供了更多信息。有一个屏幕截图,显示布局中的SurfaceView。这就是我想画的地方。MagnetView(SurfaceView)实现可运行,以便能够在单独的线程上运行。因此,您希望曲面视图与其他布局元素视图一起显示。。对吧?遗憾的是,它似乎并没有解决我的问题。根据您的指示,我更新了代码以反映我所做的事情。我错过了什么吗?如果你能,我将不胜感激。你能不能把它添加到我原来问题的底部,这样我就可以进行比较和对比?我不知道为什么它以前不起作用,但现在起作用了。谢谢柴坦尼亚的帮助和耐心!
    import android.content.Context;
    import android.graphics.Canvas;
    import android.graphics.PixelFormat;
    import android.view.SurfaceHolder;
    import android.view.SurfaceView;
    
    public class MagnetView extends SurfaceView implements Runnable, SurfaceHolder.Callback{
    Thread mThread;
    SurfaceHolder mSurfaceHolder;
    volatile boolean running = false;
    
    //Creates new surface view as well as a new surfaceholder, which allows access to the surface
    public MagnetView (Context context){
        super(context);
    
        mSurfaceHolder = getHolder();
                mSurfaceHolder.addCallback(this);
        //this.setZOrderOnTop(true);
        //getHolder().setFormat(PixelFormat.TRANSLUCENT);
    
    
    }
    
    public void resume(){
        running = true;
        mThread = new Thread(this);
        mThread.start();
    }
    
    public void pause(){
           boolean retry = true;
           running = false;
           while(retry){
            try {
             mThread.join();
             retry = false;
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
           }
    }
    
    @Override
    public void run() {
      // TODO Auto-generated method stub
        while(running){
            if(mSurfaceHolder.getSurface().isValid()){
                Canvas canvas = mSurfaceHolder.lockCanvas();
                //... actual drawing on canvas
    
                canvas.drawARGB(100, 255, 255, 80);
    
                mSurfaceHolder.unlockCanvasAndPost(canvas);
            }
        }
    }
    
    
    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width,
            int height) {
        // TODO Auto-generated method stub
    
    }
    
    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        // TODO Auto-generated method stub
        this.resume();
    
    }
    
    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        // TODO Auto-generated method stub
    
    }
    
    }
    
        <LinearLayout 
           android:id="@+id/middleSurface"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
        />
    
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    midSurf = new MagnetView(this);
    LinearLayout midLL = new LinearLayout(this);
    
    midLL.findViewById(R.id.middlesurface);
    midLL.addView(midSurf);