SurfaceView in Layout

So, ich habe eine Menge suchen, aber immer noch kann nicht scheinen, um herauszufinden, den genauen Grund, warum meine SurfaceView wird nicht angezeigt. Hier ist ein wenig hintergrund auf, was ich Tue:

Habe ich ein Linear Layout festgelegt ist Horizontal. Es enthält eine Bildansicht, dann eine vertikale Lineare Anordnung, und schließlich eine weitere Bildansicht. In der vertikalen Linearen Layout, es gibt wesentlich drei Dinge: eine andere Bildansicht an der Spitze, eine erweiterte SurfaceView (genannt MagnetView) und eine Bildansicht unten.

Hier die 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" />

Im wesentlichen, ich möchte die MagnetView zu zeigen, durch die (oder ein Loch), wo ich steckte es in das Layout ein. Aber es wird nicht angezeigt. In der Tat, das einzige mal, meine SurfaceView angezeigt wird, wenn ich die Aktivität setContentView() auf die SurfaceView ausdrücklich, Aufhebung alles andere.

Hier ist die tatsächlich Aktivität:

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

Soll ich die SurfaceView an der Spitze von meinem layout xml? Gibt es weitere Attribute, die ich einstellen muss?
Kann ich nur überlagern die SurfaceView über alles, machen es lichtdurchlässig und zeichnen, was ich brauche? Jede Hilfe wäre sehr geschätzt, wie ich kann nicht scheinen zu begreifen, wie SurfaceViews Arbeit. Es scheint, Sie sind abgesehen von meiner Sicht Hierarchie, aber dann die Dokumentation sagt mir, dass Sie etwas anderes völlig.

Danke.

BEARBEITEN 04/17/2012

Bieten ein wenig mehr info:
Meine xml-UI-Designer zeigt eine große box in der Mitte für meine erweiterte Klasse SurfaceView, genannt MagnetView. Ich umrissen habe es in rot. (Stackoverflow wird nicht lassen Sie mich zu Bilder hochladen noch)

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

Hier ist die MagnetView (SurfaceView) Klasse:

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

}

}
InformationsquelleAutor akappel | 2012-04-17
Schreibe einen Kommentar