Android的postDelayed工作,但不能把它在一个循环? [英] Android postDelayed works but can't put it in a loop?
问题描述
对不起我是小白我读过无数的教程有关使一个简单的定时器,并想知道为什么它不工作,直到我发现它是while循环导致问题OO我已删除它,然后它的工作原理,但只有1次我需要使用循环,但这样的动作完成:C
继承人的code:
old_x是从onTouch事件的ImageView的和一个new_x协调,也许这个问题,因为我是他们铸造成int?我不知道我需要做的,因此作品请帮助○:
而(old_x!=的new_x)
{
timedMoveIV(100);
old_x =(int)的img.getX();
}
它调用该方法,它工作,如果我这样做,没有循环。
公共无效timedMoveIV(INT time_ms)
{
//睡眠time_ms毫秒
处理程序处理程序=新的处理程序();
handler.postDelayed(新的Runnable(){
公共无效的run(){
如果(一个new_x> img.getX())
{
img.setX(img.getX()+ 1);
}
其他
{
img.setX(img.getX() - 1);
}
}
},time_ms);
}
您的主要问题是,你的循环没有休息,所以它的不断的运行功能,发布一个极大可运行。
您想要做的是使可运行调用的自身的又一个100毫秒。看看这个例子:
如果(old_x!=的new_x)
timedMoveIV(100);
在这里,您只需调用该函数一次。在这之后,你让发布可运行决定是否需要再次搬迁:
公共无效timedMoveIV(最终诠释time_ms)
{
处理程序处理程序=新的处理程序();
handler.postDelayed(新的Runnable(){
公共无效的run()
{
如果(一个new_x> img.getX())
img.setX(img.getX()+ 1);
其他
img.setX(img.getX() - 1);
//如果不到位,再打电话
如果((int)的img.getX()!=一个new_x)
timedMoveIV(time_ms);
}
},time_ms);
}
它应该停止一旦 img.getX()==一个new_x
。注意要转换成 INT
,不过,因为如果你离开它,你可能会得到一些振荡,因为它的目的地的像素内获得。
这假定的new_x
是 INT
。如果它是一个浮动的好,您应该投两个为int进行比较,或者把它们比作一个最低门槛。举例来说,如果有小于 0.5
的区别,把它当作完成。
Sorry I am a noob I've read countless tutorials about making a simple timer and was wondering why it doesn't work until I noticed it is the while-loop causing the issue o.O I have removed it and then it works but only 1 time I need to use the loop though so the movement finishes :C
Heres the code:
old_x is the coordinate from an ImageView and new_x from the onTouch Event, maybe the problem because I am casting them as an int? I don't know what I need to do so it works please help O:
while(old_x != new_x)
{
timedMoveIV(100);
old_x = (int)img.getX();
}
It calls this method which works if I do it without the loop.
public void timedMoveIV(int time_ms)
{
//sleep for time_ms milliseconds
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
if(new_x > img.getX())
{
img.setX(img.getX() + 1);
}
else
{
img.setX(img.getX() - 1);
}
}
}, time_ms);
}
Your main problem is that your loop doesn't take a break, so it's constantly running the function, posting a gazillion runnables.
What you want to do is make the runnable call itself after another 100 ms. Take a look at this example:
if(old_x != new_x)
timedMoveIV(100);
Here you simply call the function once. After that, you let the posted runnable decide whether or not it needs to move again:
public void timedMoveIV(final int time_ms)
{
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run()
{
if(new_x > img.getX())
img.setX(img.getX() + 1);
else
img.setX(img.getX() - 1);
// if not in position, call again
if((int)img.getX() != new_x)
timedMoveIV(time_ms);
}
}, time_ms);
}
It should stop once img.getX() == new_x
. Notice the cast to int
, though, because if you leave it out, you might get some oscillation as it gets within a pixel of the destination.
This assumes that new_x
is an int
. If it's a float as well, you should either cast both to int to compare, or compare them to a minimum threshold. For instance, if there's less than 0.5
difference, treat it as "done".
这篇关于Android的postDelayed工作,但不能把它在一个循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!