Android - Canvas显示/隐藏带倒计时的图片

时间:2012-04-12 20:19:18

标签: android canvas countdown

我还是Canvas的新手。我想要做的是创建一个倒计时,对于每个滴答,将生成一个随机布尔值。如果是,则show.png将显示,否则hide.png将显示。

基本上,我的游戏是Whack-A-Mole,我还在尝试一个单一的鼹鼠。这是我的代码到目前为止,我知道它不起作用,我仍然在我的"实验"阶段。我该如何改进?如果没有倒计时,画布会出现,但是当我添加倒计时和"如果出现= true / false"时,它就不起作用。

public class DrawingTheBall extends View{

    Bitmap show;
    Bitmap hide;
    int x;
    int y;

    public DrawingTheBall(Context context) {
        super(context);
        show = BitmapFactory.decodeResource(getResources(), R.drawable.show);
        hide = BitmapFactory.decodeResource(getResources(), R.drawable.hide);
        x = 0;
        y = 0;
    }

    @Override
    protected void onDraw(final Canvas canvas) {
        super.onDraw(canvas);

        final Random aRandom = new Random();

        new CountDownTimer(30000, 1000) {
             public void onTick(long msUntilFinished){

                 boolean appear = aRandom.nextBoolean();
                 if (appear){
                     Paint p = new Paint();
                     canvas.drawBitmap(show, x, y, p);
                 }else{
                     Paint p = new Paint();
                     canvas.drawBitmap(hide, x, y, p);
                 }
             }
             public void onFinish(){

             }
          }.start();

          invalidate();
    }

}

-------------------

@ user387184

我是否必须再次初始化变量?它表示MyCountDownTimer类型的方法myDrawRoutine(boolean)从不在本地使用。这是我的新CountDownTimer.java:

public class MyCountDownTimer extends CountDownTimer{

    Bitmap show;
    Bitmap hide;
    int x;
    int y;
    Canvas canvas;

    public MyCountDownTimer(int millisInFuture, int countDownInterval) {
        super(millisInFuture, countDownInterval);
        // TODO Auto-generated constructor stub
    }

    @Override
    public void onFinish() {
        // TODO Auto-generated method stub
    }

    @Override
    public void onTick(long millisUntilFinished) {
        // TODO Auto-generated method stub
    }

    private void myDrawRoutine(boolean appear) {
        if (appear){
            Paint p = new Paint();
            canvas.drawBitmap(show, x, y, p);
        }else{
            Paint p = new Paint();
            canvas.drawBitmap(hide, x, y, p);
        }
    }
}

1 个答案:

答案 0 :(得分:0)

试试这个......

在主代码中

创建并启动倒计时器:

MyCountDownTimer counter = new MyCountDownTimer(30000,1000);

counter.start();

然后单独创建CountDownTimer类:

public class MyCountDownTimer extends CountDownTimer{

public MyCountDownTimer(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}

@Override
public void onFinish() {
...whatever you want to do when finished...
}

@Override
public void onTick(long millisUntilFinished) {
  boolean appear = aRandom.nextBoolean();
  myDrawRoutine(appear);
}
}

private void myDrawRoutine(boolean appear) {
  if (appear){
                 Paint p = new Paint();
                 canvas.drawBitmap(show, x, y, p);
             }else{
                 Paint p = new Paint();
                 canvas.drawBitmap(hide, x, y, p);
             }

}