gpt4 book ai didi

java - Android for循环在主线程中不起作用

转载 作者:太空狗 更新时间:2023-10-29 15:01:12 26 4
gpt4 key购买 nike

我尝试旋转图像。我用 runnable 来旋转图像,我写了循环,我想在循环内旋转图像这是我的来源

public void rotateimages(final View myView) {
myHandler = new Handler();
runnable = new Runnable() {

@Override
public void run() {

double k = 30;

double speed = Math.PI / k;
for (alpa = 0; alpa < (Math.PI * 6 + res * 2 * Math.PI / 5); alpa += speed) {

myView.setRotation((float) (myView.getRotation() - alpa));

Log.e("alpa values", String.valueOf(alpa));

k = Math.min(k + 0.2, 240);
speed = Math.PI / k;
myHandler.postDelayed(this, 100);
}
// rotateimages();
}

};

myHandler.postDelayed(runnable, 180);
}

当我运行我的应用程序时,我只能旋转一次。我的代码有什么问题吗?如果有人知道解决方案请帮助我

最佳答案

如果你想循环,你需要自己重新定义这个'runnable'循环 将您的代码编辑为:

myHandler = new Handler();
count = 0;
public void rotateImage(final View myView, final int size) {


runnable = new Runnable() {

@Override
public void run() {

count++;
myView.setRotation(myView.getRotation() + size);
if(count == 10){
myHandler.removeCallBack(runnable );
}
myHandler.postDelayed(this, 1000); // 1000 means 1 second duration
}
};
myHandler.postDelayed(runnable, 180); // 180 is the delay after new runnable is going to called

}

如何循环自身,这意味着一行代码,或者一条语句再次调用事件。一些例子:

 void nothing(int a){
if(a> 0)
nothing(a-1);
}

然后什么都不调用(10)。仅此而已,并避免以下行为:

void nothing(int a){
for(int i =0; i< a;i++)
nothing(a-1); //BIG WRONG
}
Do you know your solution?

关于java - Android for循环在主线程中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26543686/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com