2014-01-20 47 views
0

在主要活動中,我有一個處理程序與藍牙服務交換消息。這工作正常。 現在,我想發起第二個活動(SurfaceViewAnimation)。我這樣做:如何從主要活動更改第二個活動的屬性?

startActivity(new Intent(this,SurfaceViewAnimation.class));

但我想從主要活動中改變SufaceViewAnimation類的一些屬性,當它收到一個藍牙命令。

我該怎麼做?

SufaceViewAnimation類的代碼是:

class BouncingBallView extends SurfaceView implements SurfaceHolder.Callback { 

    private BouncingBallAnimationThread bbThread = null; 

    private int xPosition = getWidth()/2; 
    private int yPosition = getHeight()/2; 
    private int xDirection = 20; 
    private int yDirection = 40; 

    private static int radius = 20; 
    private static int ballColor = Color.RED; 

    public BouncingBallView(Context ctx, AttributeSet attrs, int defStyle) { 
      super(ctx, attrs, defStyle); 
      getHolder().addCallback(this); 
    } 

    public void onDraw(Canvas canvas) { 
      super.onDraw(canvas); 
      Paint paint = new Paint(); 
      paint.setColor(Color.BLACK); 
      canvas.drawRect(0,0,getWidth(),getHeight(), paint); 
      paint.setColor(ballColor); 
      canvas.drawCircle(xPosition, yPosition, radius, paint); 
    } 

    public void surfaceCreated(SurfaceHolder holder) { 
      if (bbThread!=null) return; 
      bbThread = new BouncingBallAnimationThread(getHolder()); 
      bbThread.start(); 
    } 

    public void surfaceChanged(SurfaceHolder holder,int format, int width, int height) { } 

    public void surfaceDestroyed(SurfaceHolder holder) { 
      bbThread.stop = true; 
    } 

    private class BouncingBallAnimationThread extends Thread { 

      public boolean stop = false; 
      private SurfaceHolder surfaceHolder; 

      public BouncingBallAnimationThread(SurfaceHolder surfaceHolder) { 
        this.surfaceHolder = surfaceHolder; 
      } 

      public void run() { 
        while (!stop) { 
          xPosition += xDirection; 
          yPosition += yDirection; 
          if (xPosition<0) { 
            xDirection = -xDirection; 
            xPosition = radius; } 
          if (xPosition>getWidth()-radius) { 
            xDirection = -xDirection; 
            xPosition = getWidth()-radius; } 
          if (yPosition<0) { 
            yDirection = -yDirection; 
            yPosition = radius; } 
          if (yPosition>getHeight()-radius) { 
            yDirection = -yDirection; 
            yPosition = getHeight()-radius-1; } 

          Canvas c = null; 
          try { 
            c = surfaceHolder.lockCanvas(null); 
            synchronized (surfaceHolder) { 
              onDraw(c); 
            } 
          } finally { 
            if (c != null) surfaceHolder.unlockCanvasAndPost(c); 
          } 
        } 
      } 

    } 

    public boolean onTouchEvent(MotionEvent event) { 

     if (event.getAction() != MotionEvent.ACTION_DOWN) return false;  
     if (xDirection!=0 || yDirection!=0)  
      xDirection = yDirection = 0;  
     else {  
      xDirection = (int) event.getX() - xPosition;  
      yDirection = (int) event.getY() - yPosition; 
     }  
     if(ballColor==Color.RED) 
      ballColor=Color.GREEN; 
     else 
      ballColor=Color.RED; 
     return true;  
    } 

    public void setxDirection(int xDirection) { 
     this.xDirection = xDirection; 
    } 

    public void setyDirection(int yDirection) { 
     this.yDirection = yDirection; 
    } 

    public void setballColor(int ballColor) { 
     this.ballColor = ballColor; 
    } 

    public void setradius(int radius) { 
     this.radius = radius; 
    } 
} 

public class SurfaceViewAnimation extends Activity { 
     public void onCreate(Bundle savedInstanceState) { 
       super.onCreate(savedInstanceState); 
       setContentView(new BouncingBallView(this,null,0)); 
     } 
} 
+0

你想改變什麼屬性?你可以使用intent.putextra()。 –

+0

例如,更改ballColor。但是,我認爲只有在該活動創建或「啓動」(startActivity)之前,纔可以通過putextra()更改它。如果我想在活動運行時更改顏色,我不認爲這種方式有效 – crossmax

回答

0

如果你想改變SurfaceViewAnimationSurfaceViewAnimation運行數據,你最好與創建自己的處理程序。

除此之外,您可以使用Handler s,靜態變量或Observer模式回退到基本的Inter(過程/活動)通信。

+0

您可以更具體地解釋靜態變量或Observer模式嗎?我尋找簡單的解決方案,我不在乎解決方案不是最好的(更高效或「良好的實踐」) – crossmax

+0

我現在正在嘗試在主要活動(我得到消息)和SurfaceViewAnimation類之間添加Handler。但是,如果我不直接調用SurfaceVoewAnimation構造函數,而是通過startActivity(new Intent(this,SurfaceViewAnimation.class)),我如何添加相同的處理函數? – crossmax

相關問題