2011-06-17 157 views
2

我試圖在表面視圖上繪製,但我得到一個黑屏。我的XML佈局:Android SurfaceView顯示黑屏

<?xml version="1.0" encoding="utf-8"?> 
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" android:layout_height="match_parent"> 
    <com.example.surfaceview.MySurface android:id="@+id/padview" 
     android:layout_width="match_parent" android:layout_height="match_parent" /> 
</FrameLayout> 

我的代碼的重要部件:MySurface.java

公共類MySurface擴展SurfaceView實現SurfaceHolder.Callback {

public DrawingThread thread; 

public MySurface(Context context, AttributeSet attrs) { 
    super(context, attrs); 
    SurfaceHolder surfaceholder = getHolder(); 
    surfaceholder.addCallback(this); 

    thread = new DrawingThread(surfaceholder, context, new Handler() { 
     @Override 
     public void handleMessage(Message m) { 
      // Do some handle thing 
     } 
    }); 

    setFocusable(true); 
} 

@Override 
public void surfaceChanged(SurfaceHolder tholder, int format, int width, int height) { 
    thread.setSurfaceSize(width, height); 
    thread.holder = tholder; 
} 

@Override 
public void surfaceCreated(SurfaceHolder holder) { 
    thread.running = true; 
    thread.start(); 
} 

@Override 
public void surfaceDestroyed(SurfaceHolder holder) { 
    boolean retry = true; 
    thread.running = false; 
    while(retry) { 
     try { 
      thread.join(); 
      retry = false; 
     } catch (InterruptedException e) { 
     } 
    } 
} 

public Thread getThread() { 
    return thread; 
} 

public class DrawingThread extends Thread { 
    private SurfaceHolder holder; 
    private Context context; 
    private Handler handle; 
    private Paint background; 
    private Rect blank; 

    public boolean running; 
    public boolean created; 

    public int canvasheight; 
    public int canvaswidth; 

    public PadThread(SurfaceHolder tholder, Context tcontext, Handler thandler) { 
     holder = tholder; 
     context = tcontext; 
     handle = thandler; 

     // Temporary canvas dimentions 
     canvaswidth = 1; 
     canvasheight = 1; 

     running = false; 
     created = false; 

     background = new Paint(); 
     background.setColor(R.color.white); 
     blank = new Rect(0, 0, canvaswidth, canvasheight); 
    } 

    @Override 
    public void run() { 
     Log.d("SurfaceView Test", "Drawing thread run"); 
     while(running) { 
      Canvas canvas = null; 
      try { 
       canvas = holder.lockCanvas(); 
       synchronized(holder) { 
        // update object states 
        // get user input gestures 
        drawing(canvas); 
       } 
      } finally { 
       if(canvas != null) { 
        holder.unlockCanvasAndPost(canvas); 
       } 
      } 
     } 
    } 

    private void drawing(Canvas canvas) { 
     // Clear screen 
     canvas.drawRect(blank, background); 

     // Draw Things 
    } 

    public void setSurfaceSize(int width, int height) { 
     synchronized(holder) { 
      canvaswidth = width; 
      canvasheight = height; 

      // New background rect 
      blank.set(0, 0, canvaswidth, canvasheight); 
     } 
    } 
} 

}

的代碼是基於關閉Google的Lunar Landar SurfaceView示例http://developer.android.com/resources/samples/LunarLander/index.html

我知道所有的代碼都是通過日誌記錄到達的。

+1

它已經固定的,問題是,我不得不從提供的上下文,而不是R.因此,而不是'background.setColor(R.color加載顏色。白)',我不得不使用'background.setColor(context.getResources()。getColor(R.color.white)''。 – zim333311

回答

-1

更改繪圖功能

background.setColor(Color.RED); (New Rect(10,10,100,100),background); canvas.drawRect

(有對顏色和矩形測試值)