Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/384.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/216.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java handler.postDelayed不工作_Java_Android_Handler - Fatal编程技术网

Java handler.postDelayed不工作

Java handler.postDelayed不工作,java,android,handler,Java,Android,Handler,我正在尝试一个接一个地运行2个handler.postDelayed,但它不起作用 MyDraw函数在画布上绘制 我改变了x变量,这会移动图片。 我的问题是它应该向右移动,然后停止,然后向左移动 但它并没有停止 这是我的代码: Handler handler = new Handler(); MyDraw(); handler.postDelayed((new Runnable() { @Override public void run() { // TODO Auto

我正在尝试一个接一个地运行2个handler.postDelayed,但它不起作用

MyDraw函数在画布上绘制 我改变了x变量,这会移动图片。 我的问题是它应该向右移动,然后停止,然后向左移动 但它并没有停止

这是我的代码:

Handler handler = new Handler(); 
MyDraw();
handler.postDelayed((new Runnable() {
    @Override
    public void run() {
    // TODO Auto-generated method stub
    while(x<=40)
    {
        x=x+5;
        MyDraw();
    }
}), 3000);

handler.postDelayed(new Runnable() { 
    @Override
    public void run() {
        while(x>=-200)
        {
            x=x-5;
            MyDraw();
        }
    }
}, 3000); 
这是onDraw:

受保护的空白画布{

    canvas.drawColor(Color.argb(255, 44, 171, 226));
    bitbattle=BitmapFactory.decodeResource(getResources(), R.drawable.battlesbackgroundice);
    //canvas.drawBitmap(bit, 0, 0, null);
    Rect src=new Rect(0, 0, bitbattle.getWidth(), bitbattle.getHeight());
    Rect dst=new Rect(0, 0, this.getWidth(), this.getHeight()-290);
    canvas.drawBitmap(bitbattle,src, dst, null);
    bitash = BitmapFactory.decodeResource(getResources(),R.drawable.trainer);
    Rect ashsrc=new Rect(0,0,bitash.getWidth(),bitash.getHeight()); 
    Rect ashdst=new Rect(x,y,(x+bitash.getWidth())*2,(y+bitash.getHeight())*2);
    canvas.drawBitmap(bitash, ashsrc, ashdst, null);
    bitkeypad = BitmapFactory.decodeResource(getResources(),R.drawable.keypad);
    Rect keysrc=new Rect(0,0,bitkeypad.getWidth(),bitkeypad.getHeight());
    Rect keydst=new Rect(20,this.getHeight()-270,190,this.getHeight()-100);
    canvas.drawBitmap(bitkeypad, keysrc, keydst, null);
    super.onDraw(canvas);
}

线程处理消息的时间对于两条消息3000都是相同的。post delayed函数向线程发送消息,但线程等待执行该函数。因此,本质上,您是在告诉主线程在三秒内一个接一个地执行这两个函数。如果要模拟暂停在这两段代码之间,则暂停的长度将等于传递给每个postDelayed函数的两个时间值之间的差值。

我不明白您在这两个函数之间创建差值是什么意思?我需要更改它将停止的时间?我指的是等待时间值的差值。Cur通常,它们都设置为3000ms。我试图创建一个差异,将2000放入最后一个,但它只是停止,没有执行其他功能。它没有向左移动。最后一个值应该比第一个值大。postDelayed方法的第二个值需要等待一定的时间。因此,如果您希望第二个函数执行af对于第一个函数,第二个函数应该有一个更大的等待时间值。我试着这样做,但当最后一个函数更大时,它根本没有停止
    canvas.drawColor(Color.argb(255, 44, 171, 226));
    bitbattle=BitmapFactory.decodeResource(getResources(), R.drawable.battlesbackgroundice);
    //canvas.drawBitmap(bit, 0, 0, null);
    Rect src=new Rect(0, 0, bitbattle.getWidth(), bitbattle.getHeight());
    Rect dst=new Rect(0, 0, this.getWidth(), this.getHeight()-290);
    canvas.drawBitmap(bitbattle,src, dst, null);
    bitash = BitmapFactory.decodeResource(getResources(),R.drawable.trainer);
    Rect ashsrc=new Rect(0,0,bitash.getWidth(),bitash.getHeight()); 
    Rect ashdst=new Rect(x,y,(x+bitash.getWidth())*2,(y+bitash.getHeight())*2);
    canvas.drawBitmap(bitash, ashsrc, ashdst, null);
    bitkeypad = BitmapFactory.decodeResource(getResources(),R.drawable.keypad);
    Rect keysrc=new Rect(0,0,bitkeypad.getWidth(),bitkeypad.getHeight());
    Rect keydst=new Rect(20,this.getHeight()-270,190,this.getHeight()-100);
    canvas.drawBitmap(bitkeypad, keysrc, keydst, null);
    super.onDraw(canvas);
}