2011-07-14 10 views
1

私はを非常に簡単にしようとしていますSurfaceViewに描画していますが、動作させることはできません。例外はありませんが、結果は表示されません。AndroidのSurfaceViewに描画する

もっと正確には、私はSurfaceViewを作成し、それを単一の色で塗りつぶしています。予想通り

public class SvetlinSurfaceViewTestActivity extends Activity { 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     SurfaceRenderer renderer = new SurfaceRenderer(this); 
     setContentView(renderer); 
    } 
} 

class SurfaceRenderer extends ViewGroup implements SurfaceHolder.Callback { 
    private final String TAG = "Svetlin Surface Renderer"; 

    SurfaceView mSurfaceView; 
    SurfaceHolder mHolder; 

    public SurfaceRenderer(Context context) { 
     super(context); 

     mSurfaceView = new SurfaceView(context); 
     addView(mSurfaceView); 

     // Install a SurfaceHolder.Callback so we get notified when the 
     // underlying surface is created and destroyed. 
     mHolder = mSurfaceView.getHolder(); 
     mHolder.addCallback(this); 
    } 

    @Override 
    protected void onLayout(boolean changed, int l, int t, int r, int b) {} 

    @Override 
    public void surfaceCreated(SurfaceHolder holder) { 
     tryDrawing(holder); 
    } 

    @Override 
    public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) { 
     tryDrawing(holder); 
    } 

    @Override 
    public void surfaceDestroyed(SurfaceHolder holder) {} 

    private void tryDrawing(SurfaceHolder holder) { 
     Log.i(TAG, "Trying to draw..."); 

     Canvas canvas = holder.lockCanvas(); 
     if (canvas == null) { 
      Log.e(TAG, "Cannot draw onto the canvas as it's null"); 
     } else { 
      drawMyStuff(canvas); 
      holder.unlockCanvasAndPost(canvas); 
     } 
    } 

    private void drawMyStuff(final Canvas canvas) { 
     Log.i(TAG, "Drawing..."); 
     canvas.drawRGB(255, 128, 128); 
    } 
} 

が、私は二回出力さ描画...を取得しています:

は、ここに私のコードを行きます。

なぜ私はSurfaceViewを引き出すことができないのでしょうか?

答えて

4

プロキシクラスSurfaceRendererを削除し、それをすべて自分のアクティビティに移動しました。それは今働いている。ここに固定コードがあります:

public class SvetlinSurfaceViewTestActivity 
    extends Activity 
    implements SurfaceHolder.Callback { 

    private static final String TAG = "Svetlin SurfaceView"; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     SurfaceView view = new SurfaceView(this); 
     setContentView(view); 
     view.getHolder().addCallback(this); 
    } 

    @Override 
    public void surfaceCreated(SurfaceHolder holder) { 
     tryDrawing(holder); 
    } 

    @Override 
    public void surfaceChanged(SurfaceHolder holder, int frmt, int w, int h) { 
     tryDrawing(holder); 
    } 

    @Override 
    public void surfaceDestroyed(SurfaceHolder holder) {} 

    private void tryDrawing(SurfaceHolder holder) { 
     Log.i(TAG, "Trying to draw..."); 

     Canvas canvas = holder.lockCanvas(); 
     if (canvas == null) { 
      Log.e(TAG, "Cannot draw onto the canvas as it's null"); 
     } else { 
      drawMyStuff(canvas); 
      holder.unlockCanvasAndPost(canvas); 
     } 
    } 

    private void drawMyStuff(final Canvas canvas) { 
     Random random = new Random(); 
     Log.i(TAG, "Drawing..."); 
     canvas.drawRGB(255, 128, 128); 
    } 
} 
関連する問題