如何停止自定义视图刷新?

时间:2012-01-11 12:48:55

标签: android android-canvas android-view

我在扩展View类的类中使用onDraw(Canvas canvas)方法创建了一个自定义视图。我使用invalidate()方法每2秒刷新一次视图。但是现在我想停止刷新视图一段时间后说120秒后。如何停止invalidate()方法。任何建议或帮助将受到高度赞赏。

修改

这是我的代码: -

public GameView(Context context){ 
super(context);

Display display = ((WindowManager)context.getSystemService(
             Context.WINDOW_SERVICE)).getDefaultDisplay();
}

@Override
    public void draw(Canvas canvas) {
        // TODO Auto-generated method stub
        super.draw(canvas);

        int x = 0;
        int y = 0;

        bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
        int imageWidth = bmp.getWidth();
        int imageHeight = bmp.getHeight();

        int width = display.getWidth();
        System.out.println("Width = " +width);
        int height = display.getHeight();
        System.out.println("Height = " +height);
        Random randomX,randomY;
        randomX = new Random();
        randomY = new Random();

        x = randomX.nextInt(width - imageWidth);
        System.out.println("X = " +x);
        y = randomY.nextInt(height - imageHeight);
        System.out.println("Y = " +y);

        Rect dst = new Rect(x , y , x + imageWidth , y + imageHeight);
        canvas.drawBitmap(bmp, null , dst , null);
        System.out.println("dst = " +dst);
        try{
        Thread.sleep(1000)
        }
        catch(Exception e){

        }
        invalidate();

    }  

2 个答案:

答案 0 :(得分:0)

你只需要使用一个计数器(例如int myCounter)并在每个循环中将其值增加2。然后,您可以在调试指令中包含调用线程和invalidate()的代码,以便仅在变量myCounter小于120时执行此程序块。

答案 1 :(得分:0)

好吧,我不会通过在draw方法中调用Thread.sleep(1000)来亲自阻止UI线程。相反,我会使用一个Handler对象,然后我会调用递归的postDelayed调用来安排重复的invalidate()调用。最后,我编写了2个特殊函数来启动“动画”并在需要时中断它(以下代码未经过测试......只是我在几秒钟内编写的一个片段来说明我的观点。请注意警告。)。请注意,Aderito Brinca所描述的内容也是准确的,尽管它没有解决上面提到的UI线程阻塞问题。

private Handler mHandler = new Handler();
private boolean isAnimStarted=false;
final private long refreshIntervalMsec = 2000;     //2 seconds refresh rate
final private long maxRefreshTimeMsec = 120000; //120 seconds deadline
private long systemTimeMillisOnStart = -1L;
private Display display = null; //within a local variable declared in GameView
//constructor this variable was useless. Now that it is a private
//class member, it can be used by overridden onDraw or Draw methods.

public GameView(Context context){ 
    super(context);
    display = ((WindowManager)context.getSystemService(
             Context.WINDOW_SERVICE)).getDefaultDisplay();
    //INSERT THE REST OF YOUR CONSTRUCTOR CODE HERE
}

public boolean isGameViewAnimStarted(){ //CALL this in order to check for running anim
    return isAnimStarted;
}

public void startGameViewAnim(){ //CALL this to start your anim thing
    isAnimStarted=true;
    systemTimeMillisOnStart = SystemClock.uptimeMillis();
    refreshAnim();
}

private void refreshAnim(){
    invalidate();
    if(  (SystemClock.uptimeMillis() - systemTimeMillisOnStart) < maxRefreshTimeMsec){
        mHandler.postDelayed(new Runnable(){
            public void run(){              
                refreshAnim();
            }
        }, refreshIntervalMsec);
    }
    else{
        stopGameViewAnim();
    }
}

public void stopGameViewAnim(){ //CALL this to interrupt your anim thing
    mHandler.removeCallbacksAndMessages(null);
    isAnimStarted=false;
}



@Override
public void draw(Canvas canvas) {
    // TODO Auto-generated method stub
    super.draw(canvas);
    //INSERT YOUR CODE HERE

}