作者热门文章
- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我尝试旋转图像。我用 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/
我是一名优秀的程序员,十分优秀!