如何从主要活动更改第二个活动的属性?

时间:2014-01-20 23:55:10

标签: android android-intent android-activity attributes

在主要活动中,我有一个与蓝牙服务交换消息的处理程序。这很好用。 现在,我想要启动第二个活动(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));
        }
}

1 个答案:

答案 0 :(得分:0)

如果您想在 SurfaceViewAnimation正在运行时更改SurfaceViewAnimation 中的数据,那么最好创建一个自己的处理程序。

除此之外,您可以使用Handler,静态变量或Observer模式回退到基本的Inter(流程/活动)通信。