2015-06-20 34 views
0

我试图每隔5秒画一个红圈。我有一个绘制和一个运行方法,run方法调用绘制圆的绘制方法。但是,当从run方法调用draw方法时,表面无效,所以我无法绘制任何内容,并且屏幕是黑色的。我如何使表面有效,或者如果我不能,我该怎么做?这里是代码和logcat。Android - Surface无效

public class RandomCircles extends Activity { 

    MySurfaceView mySurfaceView; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     mySurfaceView = new MySurfaceView(this); 
     setContentView(mySurfaceView); 
    } 


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

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

    class MySurfaceView extends SurfaceView implements Runnable { 

     Thread thread = null; 
     SurfaceHolder surfaceHolder; 
     volatile boolean running = false; 
     Handler handler = new Handler(); 

     private Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); 
     Random random; 

     public MySurfaceView(Context context) { 
      super(context); 
      // TODO Auto-generated constructor stub 
      surfaceHolder = getHolder(); 
      random = new Random(); 
     } 

     public void onResumeMySurfaceView() { 
      running = true; 
      thread = new Thread(this); 
      thread.start(); 
     } 

     public void onPauseMySurfaceView() { 
      boolean retry = true; 
      running = false; 
      while (retry) { 
       try { 
        thread.join(); 
        retry = false; 
       } catch (InterruptedException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 
      } 
     } 

     @Override 
     public void run() { 
      System.out.println("The run method is running"); 
      // TODO Auto-generated method stub 
      Draw(); 
     } 

     public void Draw() { 
      System.out.println("The draw method is running"); 
      if (surfaceHolder.getSurface().isValid()) { 
       System.out.println("The surface is valid"); 
       Canvas canvas = surfaceHolder.lockCanvas(); 
       //... actual drawing on canvas 

       int x = random.nextInt(getWidth()); 

       if (getWidth() - x < 100) 
        x -= 100; 
       else if (getWidth() - x > getWidth() - 100) 
        x += 100; 

       int y = random.nextInt(getHeight()); 

       if (getHeight() - y < 100) 
        y -= 100; 
       else if (getHeight() - x > getHeight() - 100) 
        y += 100; 

       int radius; 
       radius = 100; 
       Paint paint = new Paint(); 
       paint.setStyle(Paint.Style.FILL); 
       paint.setColor(Color.WHITE); 
       canvas.drawPaint(paint); 
       // Use Color.parseColor to define HTML colors 
       paint.setColor(Color.parseColor("#CD5C5C")); 
       canvas.drawCircle(x, y, radius, paint); 

       surfaceHolder.unlockCanvasAndPost(canvas); 
      } 
     } 
    } 
} 

There are indicators when the run and draw methods are called, but the indicator after the check for if the screen is valid does not display.

+0

的代码来调用Draw方法每隔5秒尚未添加尚未造成这部分甚至没有工作尚未 – Sygnerical

回答

2

实施SurfaceHolder.Callback,当你收到surfaceCreated(SurfaceHolder holder)回调仅在Surface渲染。例如:

public MySurfaceView(Context context) { 
    super(context); 
    surfaceHolder = getHolder(); 
    surfaceHolder.addCallback(new SurfaceHolder.Callback() { 
       @Override 
       public void surfaceDestroyed(SurfaceHolder holder) { 
         //stop render thread here 
       } 

       @Override 
       public void surfaceCreated(SurfaceHolder holder) { 
         //start render thread here 
       } 
       @Override 
       public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {} 

     }); 
} 
+0

究竟将这些方法做,并提供? – Sygnerical

+0

当Surface的创建完成时(因此它将会生效)调用'surfaceCreated()'并且您可以开始绘制它。答案中的链接是非常自我解释的:'当与SurfaceView一起使用时,被保持的Surface只能在调用surfaceCreated()和surfaceDestroyed()之间使用。' – MadEqua

+0

哦,所以如果我将draw方法中的代码移动到表面的方法,它应该工作吗?但是在表面被破坏之后,你能看到你早先画的东西吗? – Sygnerical