SurfaceView的布局 [英] SurfaceView in Layout

查看:295
本文介绍了SurfaceView的布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我已经做了很多搜索,但似乎仍然无法找到确切的原因,为什么我的SurfaceView将不显示。这里有一个小的背景,以我在做什么:

我有一个线性布局设置水平。它包含一个ImageView的,会出现一个垂直线性布局,最后又ImageView的。在垂直线性布局中,有必要三件事:另一ImageView的上方,一个扩展SurfaceView(称为MagnetView)和低于一个ImageView的

这里的XML:

 < LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT
机器人:方向=横向>

 <! - 左Magnetrak  - >

 < ImageView的
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =FILL_PARENT
    机器人:SRC =@可绘制/ vectorparts_01
    机器人:adjustViewBounds =真/>

<! - 中东线性规划 - >
<的LinearLayout
    机器人:layout_width =0dip
    机器人:layout_height =FILL_PARENT
    机器人:方向=垂直
    机器人:layout_weight =1>

    <! - 顶酒吧与静物和比分反超 - >
    < RelativeLayout的
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:adjustViewBounds =真正的>

    < ImageView的
    机器人:adjustViewBounds =真
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =WRAP_CONTENT
    机器人:SRC =@可绘制/ vectorparts_22/>

    <! - 比分反超 - >
    <的TextView
    机器人:ID =@ + ID / myImageViewText
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_alignParentRight =真
    机器人:layout_marginRight =10dp
    机器人:layout_marginTop =5DP
    机器人:重力=中心
    机器人:文本=000000000000
    机器人:文字颜色=#000000/>

    <! - 一家Life1  - >
     < ImageView的
        机器人:ID =@ + ID /寿命1
        机器人:adjustViewBounds =真
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:SRC =@可绘制/ vectorparts_13
        机器人:layout_alignParentLeft =真
        机器人:layout_marginLeft =5DP
        机器人:layout_marginTop =1DP
        机器人:重力=中心/>

     &所述;! -  Life2  - >
     < ImageView的
        机器人:ID =@ + ID / life2
        机器人:adjustViewBounds =真
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:SRC =@可绘制/ vectorparts_13
        机器人:layout_toRightOf =@ ID /寿命1
        机器人:layout_marginTop =1DP
        机器人:layout_marginLeft =1DP
        机器人:重力=中心/>

     &所述;! -  Life3  - >
     < ImageView的
        机器人:ID =@ + ID / life3
        机器人:adjustViewBounds =真
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:SRC =@可绘制/ vectorparts_13
        机器人:layout_toRightOf =@ ID / life2
        机器人:layout_marginTop =1DP
        机器人:layout_marginLeft =1DP
        机器人:重力=中心/>

    < / RelativeLayout的>

    &所述;! -  SurfaceView  - >
    <的LinearLayout
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT
    机器人:ID =@ + ID / middlesurface>

    < / LinearLayout中>

     <! - 底酒吧 - >
    < RelativeLayout的
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:adjustViewBounds =真正的>

    < ImageView的
    机器人:adjustViewBounds =真
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =WRAP_CONTENT
    机器人:SRC =@可绘制/ vectorparts_02/>


    < / RelativeLayout的>


< / LinearLayout中>

<! - 右Magnetrak  - >
   < ImageView的
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =FILL_PARENT
机器人:SRC =@可绘制/ vectorparts_01
机器人:adjustViewBounds =真/>
 

从本质上讲,我想MagnetView显示通过(或打眼),我把它放在布局。但它并不显示。事实上,唯一一次我SurfaceView显示是当我设置活动的的setContentView()到SurfaceView明确,取消了一切。

下面是实际活动:

 公共类Magnetraks延伸活动{

MagnetView midSurf;

/ **第一次创建活动时调用。 * /
@覆盖
公共无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    midSurf =新MagnetView(本);
    的LinearLayout midLL =新的LinearLayout(本);

    midLL.findViewById(R.id.middlesurface);
    midLL.addView(midSurf);

    的setContentView(R.layout.main);

    //调试目的:跑,看看是否middleSurface视图显示自己的时候了。
    //的setContentView(midSurf);


}

@覆盖
保护无效onResume(){
 // TODO自动生成方法存根
 super.onResume();
 midSurf.resume();
}

@覆盖
保护无效的onPause(){
 // TODO自动生成方法存根
 super.onPause();
 midSurf.pause();
}
 

我应该将SurfaceView在我的布局XML的顶部?是否有更多的属性,我必须设置? 我可以只覆盖了SurfaceView了一切,使之透明,并绘制我需要什么?任何帮助将大大AP preciated,因为我似乎无法掌握如何SurfaceViews工作。看来他们是除了我的视图层次,但随后的文档告诉我,他们是不同的东西完全。

感谢你。

编辑04/17/2012

要提供一些更多的信息: 我的XML UI设计师显示在中间的我的大SurfaceView类,称为MagnetView一个大箱子。我已经用红色标出它。 (#1不会让我张贴图片还)

UI设计视图(http://24.media.tumblr.com/tumblr_m2mmqvBNwW1qcreoco1_500.jpg) <一href="http://24.media.tumblr.com/tumblr_m2mmqvBNwW1qcreoco1_500.jpg">http://24.media.tumblr.com/tumblr_m2mmqvBNwW1qcreoco1_500.jpg

下面是MagnetView(SurfaceView)类:

 进口android.content.Context;
进口android.graphics.Canvas;
进口android.graphics.PixelFormat;
进口android.view.SurfaceHolder;
进口android.view.SurfaceView;

公共类MagnetView扩展了SurfaceView实现Runnable,SurfaceHolder.Callback {
螺纹mThread;
SurfaceHolder mSurfaceHolder;
挥发性布尔运行= FALSE;

//创建新的表面视图,以及一个新的surfaceholder,这使得表面访问
公共MagnetView(上下文的背景下){
    超(上下文);

    mSurfaceHolder = getHolder();
            mSurfaceHolder.addCallback(本);
    //this.setZOrderOnTop(true);
    //getHolder().setFormat(PixelFormat.TRANSLUCENT);


}

公共无效简历(){
    运行=真;
    mThread =新主题(本);
    mThread.start();
}

公共无效暂停(){
       布尔重试= TRUE;
       运行= FALSE;
       而(重试){
        尝试 {
         mThread.join();
         重试= FALSE;
            }赶上(InterruptedException异常E){
                // TODO自动生成的catch块
                e.printStackTrace();
            }
       }
}

@覆盖
公共无效的run(){
  // TODO自动生成方法存根
    而(运行){
        如果(mSurfaceHolder.getSurface()。参考isValid()){
            帆布帆布= mSurfaceHolder.lockCanvas();
            // ...在画布上实际绘制

            canvas.drawARGB(100,255,255,80);

            mSurfaceHolder.unlockCanvasAndPost(画布);
        }
    }
}


@覆盖
公共无效surfaceChanged(SurfaceHolder持有人,INT格式,诠释的宽度,
        INT高度){
    // TODO自动生成方法存根

}

@覆盖
公共无效surfaceCreated(SurfaceHolder持有者){
    // TODO自动生成方法存根
    this.resume();

}

@覆盖
公共无效surfaceDestroyed(SurfaceHolder持有者){
    // TODO自动生成方法存根

}

}
 

解决方案

我刚放下的变化。

  1. 替换表面观与LinearLayout中的XML。

     &LT;的LinearLayout
           机器人:ID =@ + ID / middleSurface
           机器人:layout_width =WRAP_CONTENT
           机器人:layout_height =WRAP_CONTENT
        /&GT;
     

  2. 获取的线性布局实例

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

  3. 添加表面视图的实例的LinearLayout

    surface.addView(新MagnetView(本));

应的setContentView(R.layout.main)后进行2,3步骤;

So I've done a lot of searching but still can't seem to find the exact reason as to why my SurfaceView won't display. Here's a little background as to what I'm doing:

I have a Linear Layout that is set Horizontally. It contains an ImageView, then a vertical Linear Layout, and finally another ImageView. In the vertical Linear Layout, there are essential three things: another ImageView at the top, an extended SurfaceView (called MagnetView) and an ImageView below that.

Here's the 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" />

Essentially, I want the MagnetView to show through (or punch a hole) where I put it in the Layout. But it does not display. In fact, the only time my SurfaceView displays is when I set the activity's setContentView() to the SurfaceView explicitly, canceling out everything else.

Here is the actually Activity:

public class Magnetraks extends Activity {

MagnetView midSurf;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    midSurf = new MagnetView(this);
    LinearLayout midLL = new LinearLayout(this);

    midLL.findViewById(R.id.middlesurface);
    midLL.addView(midSurf);

    setContentView(R.layout.main);

    //Debugging purposes: run to see if middleSurface view is showing up when on its own.
    //setContentView(midSurf);


}

@Override
protected void onResume() {
 // TODO Auto-generated method stub
 super.onResume();
 midSurf.resume();
}

@Override
protected void onPause() {
 // TODO Auto-generated method stub
 super.onPause();
 midSurf.pause();
}

Should I place the SurfaceView at the top of my layout xml? Are there more attributes I must set? Can I just overlay the SurfaceView over everything else, make it translucent and draw what I need? Any help would be greatly appreciated, as I cannot seem to grasp how SurfaceViews work. It seems they are apart of my View hierarchy, but then documentation tells me they are something different entirely.

Thank you.

EDIT 04/17/2012

To offer a little more info: My xml UI Designer shows a big box in the middle for my extended SurfaceView class, called MagnetView. I've outlined it in red. (Stackoverflow won't allow me to post images yet)

UI Designer view(http://24.media.tumblr.com/tumblr_m2mmqvBNwW1qcreoco1_500.jpg) http://24.media.tumblr.com/tumblr_m2mmqvBNwW1qcreoco1_500.jpg

Here's the MagnetView (SurfaceView) class:

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

}

}

解决方案

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

这篇关于SurfaceView的布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆