当我调用.invalidate方法时,矩形不会绘制?

时间:2015-02-19 04:56:59

标签: java android

我正在尝试在屏幕上绘制矩形,我稍后会添加代码以使它们在屏幕上移动,因为我正在制作的游戏的目标是通过矩形中的间隙倾斜球以防止它们推动球离开了屏幕。以下是游戏活动中的一些代码:

public class GameScreen extends ActionBarActivity {

BallView mBallView = null;
Rectangle rect1 = null
        , rect2= null
        , rect3 = null
        , rect4= null
        , rect5 = null
        , rect6= null;
Handler RedrawHandler = new Handler(); //so redraw occurs in main thread
Timer mTmr = null;
TimerTask mTsk = null;
int mScrWidth, mScrHeight;
android.graphics.PointF mBallPos, mBallSpd;




@Override
protected void onCreate(Bundle savedInstanceState) {
    requestWindowFeature(Window.FEATURE_NO_TITLE); //hide title bar
    //set app to full screen and keep screen on
    getWindow().setFlags(0xFFFFFFFF, LayoutParams.FLAG_FULLSCREEN | LayoutParams.FLAG_KEEP_SCREEN_ON);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_game_screen);
    //create pointer to main screen
    final FrameLayout mainView = (android.widget.FrameLayout) findViewById(R.id.main_view);

    //get screen dimensions
    Display display = getWindowManager().getDefaultDisplay();
    mScrWidth = display.getWidth();
    mScrHeight = display.getHeight();
    mBallPos = new android.graphics.PointF();
    mBallSpd = new android.graphics.PointF();




    //create variables for ball position and speed
    mBallPos.x = mScrWidth / 5;
    mBallPos.y = mScrHeight / 2;
    mBallSpd.x = 0;
    mBallSpd.y = 0;

    //create initial ball
    mBallView = new BallView(this, mBallPos.x, mBallPos.y, 75);

    //**************Create initial rectangle****************
    rect1 = new Rectangle(this, 1000, 500, 1000, 500);
    rect1.invalidate();



    mainView.addView(mBallView); //add ball to main screen
    mBallView.invalidate(); //call onDraw in BallView

    //listener for accelerometer, use anonymous class for simplicity
    ((SensorManager) getSystemService(Context.SENSOR_SERVICE)).registerListener(
            new SensorEventListener() {
                @Override
                public void onSensorChanged(SensorEvent event) {
                    //set ball speed based on phone tilt (ignore Z axis)
                    mBallSpd.x = -event.values[0] * (30/10);
                    mBallSpd.y = event.values[1] * (30/10);
                    //timer event will redraw ball
                }

                @Override
                public void onAccuracyChanged(Sensor sensor, int accuracy) {
                } //ignore
            },
            ((SensorManager) getSystemService(Context.SENSOR_SERVICE))
                    .getSensorList(Sensor.TYPE_ACCELEROMETER).get(0),
            SensorManager.SENSOR_DELAY_NORMAL);


} //OnCreate

//listener for menu button on phone
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    menu.add("Exit"); //only one menu item
    return super.onCreateOptionsMenu(menu);
}

//listener for menu item clicked
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle item selection
    if (item.getTitle() == "Exit") //user clicked Exit
        finish(); //will call onPause
    return super.onOptionsItemSelected(item);
}

//For state flow see http://developer.android.com/reference/android/app/Activity.html
@Override
public void onPause() //app moved to background, stop background threads
{
    mTmr.cancel(); //kill\release timer (our only background thread)
    mTmr = null;
    mTsk = null;
    super.onPause();
}


@Override
public void onResume() //app moved to foreground (also occurs at app startup)
{
    //create timer to move ball to new position
    mTmr = new Timer();
    mTsk = new TimerTask() {
        public void run() {

            //if debugging with external device,
//  a log cat viewer will be needed on the device
            Log.d("TiltBall", "Timer Hit - " + mBallPos.x + ":" + mBallPos.y);

//move ball based on current speed
            mBallPos.x += mBallSpd.y;
            mBallPos.y += -mBallSpd.x;

            //if ball goes off screen, reposition to opposite side of screen
            if (mBallPos.x > mScrWidth) mBallPos.x = 0;
            if (mBallPos.y > mScrHeight) mBallPos.y = 0;
            if (mBallPos.x < 0) mBallPos.x = mScrWidth;
            if (mBallPos.y < 0) mBallPos.y = mScrHeight;

            //update ball class instance
            mBallView.x = mBallPos.x;
            mBallView.y = mBallPos.y;
//redraw ball. Must run in background thread to prevent thread lock.
            RedrawHandler.post(new Runnable() {
                public void run() {
                    mBallView.invalidate();
                }
            });




            //If ball hits sides it goes to the redirect screen, YOU LOSE!
            if (mBallPos.y  == mScrHeight || mBallPos.y == 0 || mBallPos.x == mScrWidth|| mBallPos.x == 0 ) {

                goToRedirectScreen();
            }




        } //run
    }; // TimerTask

    mTmr.schedule(mTsk, 10, 10); //start timer
    super.onResume();
} // onResume

这是我制作的Rectangle类:

public class Rectangle extends View {

public float left;
public float top;
public float right;
public float bottom;
private final Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);


//construct new rectangle object
public Rectangle(Context context, float left, float top, float right, float bottom) {
    super(context);

    mPaint.setColor(0xFFFFFF);  //not transparent. color is white
    this.left = left;
    this.top = top;
    this.right = right;
    this.bottom = bottom;
}

//qcalled by invalidate()
@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    canvas.drawRect(left, top, right, bottom, mPaint);
} }

每当我调用.invalidate方法来使用我的Rectangle类的.onDraw时屏幕上都没有出现,我看不出哪里出错了。我也试过使用.postInvalidate,但也不起作用。

0 个答案:

没有答案