如果object到达画布的边缘,则打开一个新活动。我该怎么做呢?

时间:2014-08-01 01:58:18

标签: android android-canvas

此代码来自纽约波士顿网站的Bucky教程128.请根据需要参考。 https://buckysroom.org/videos.php?cat=6&video=16849

如果绿球击中画布边缘,我希望它打开一个新活动。我该怎么写这个过程?

public class Accelerate extends Activity实现SensorEventListener {

float x, y, sensorX, sensorY;
float alpha;
Bitmap ball;
SensorManager sm;
MyBringBackSurface ourSurfaceView;

public class MyBringBackSurface extends SurfaceView implements Runnable {
    SurfaceHolder ourHolder;
    Thread ourThread = null;
    boolean isRunning = false;

    public MyBringBackSurface(Context context) {
        // TODO Auto-generated constructor stub
        super(context);
        ourHolder = getHolder();

    }

    public void pause(){
        isRunning = false;
        while(true){
            try {
                ourThread.join();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            break;
        }
        ourThread = null;
    }

    public void resume(){
        isRunning = true;
        ourThread = new Thread(this);
        ourThread.start();
    }



    @Override
    public void run() {
        // TODO Auto-generated method stub
        while(isRunning){
            if(!ourHolder.getSurface().isValid())
            continue;

            Canvas canvas = ourHolder.lockCanvas();
            //canvas.drawRGB(2, 2, 100); blue
            canvas.drawRGB(227, 168, 105); //Mango peach
            //float centerX = canvas.getWidth()/2;
            //float centerY = canvas.getHeight()/2;

            float centerX = (canvas.getWidth()-ball.getWidth())/2;
            float centerY = (canvas.getHeight()-ball.getHeight())/2;

            canvas.drawBitmap(ball, centerX + sensorX*20, centerY + sensorY*20, null);
            ourHolder.unlockCanvasAndPost(canvas);



        }
    }

}

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);

    sm = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
    if (sm.getSensorList(Sensor.TYPE_ACCELEROMETER).size() != 0){
        Sensor s = sm.getSensorList(Sensor.TYPE_ACCELEROMETER).get(0);
        sm.registerListener(this, s, SensorManager.SENSOR_DELAY_NORMAL);

    }
    ball = BitmapFactory.decodeResource(getResources(), R.drawable.greenball);
    x = y = sensorX = sensorY = 0;
    ourSurfaceView = new MyBringBackSurface(this);
    ourSurfaceView.resume();
    setContentView(ourSurfaceView);
}


@Override
protected void onPause() {
    // TODO Auto-generated method stub
    sm.unregisterListener(this);
    super.onPause();
}

@Override
public void onSensorChanged(SensorEvent e) {
    // TODO Auto-generated method stub
    try {
        Thread.sleep(16);
    } catch (InterruptedException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

    alpha = (float) 0.8;

    sensorX = alpha*sensorX + (1-alpha)*e.values[0];
    sensorY = alpha*sensorY + (1-alpha)*e.values[1];


    //sensorX = e.values[0];
    //sensorY = e.values[1];

}

@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
    // TODO Auto-generated method stub

}

}

1 个答案:

答案 0 :(得分:0)

我假设您已完成计算并检查球是否击中画布的边缘。你只需要访问你的活动。

Intent intent = new Intent(youmainactivity, yournewactivityclass.class);
    startActivity(intent);

或如果您有上下文

Intent intent = new Intent((Activity) context,yournewactivityclass.class);
        startActivity(intent);