2016-02-26 59 views
0

好的,我从https://github.com/hongyangAndroid/Android-CircleMenu得到了以下代码。在这里,从打印轨迹中,我发现构造函数只被调用一次,而run()方法被递归调用,直到某些条件。Android:view的postDelayed(runnable,milliSeconds)如何与递归运行?

我的问题是,为什么只有run()方法递归调用postDelayed(),为什么不是构造函数?以及变角anglePerSecond如何保留该值?我想了解它的流程。谢谢。

//Automatic scrolling tasks 
    private class AutoFlingRunnable implements Runnable{ 
    private float anglePerSecond; 
    public AutoFlingRunnable(float velocity){ 
     this.anglePerSecond = velocity; 
    } 
    public void run(){ 
     if((int)Math.abs(anglePerSecond) < 20){ 
      isFling = false; 
      return; 
     } 
     isFling = true; 

     //anglePerSecond/30 in order to avoid rolling too fast 
     mStartAngle += (anglePerSecond/30); 
     //Gradually reduce this value 
     anglePerSecond /= 1.0666F; 
     postDelayed(this, 30); 
     //gradually reduce this value 
     requestLayout(); //re-layout views 
    } 
} 

回答

1

当你想更新活动中的TextView的,例如,与countdowntimer一个TextView,你通常用下面的代码

final int time = 50; 
    final TextView textView = new TextView(this); 
    textView.postDelayed(new Runnable() { 
     @Override 
     public void run() { 
      // TODO Auto-generated method stub 
      textView.setText(String.format("Remaing Time : %d s", time)); 
     } 
     /* 
     * Update it after 1 second 
     * 1秒後更新 
     */ 
    },1000); 

如果你理解了上面的代码更新的话,尝试了解以下内容

textView.postDelayed(new Runnable() { 


     @Override 
     public void run() { 
      // TODO Auto-generated method stub 
      textView.setText(String.format("Remaing Time : %d s", time)); 
      /* 
      * Update it after 1 second 
      * 1秒後更新 
      */ 
      textView.postDelayed(this, 1000); 
     } 
     /* 
     * Update it after 1 second 
     * 1秒後更新 
     */ 
    },1000); 

实际上,它与您提供的库内部的进度相同。 这是在这样的剂量。

1.call AutoFlingRunnable consturctor来创建线

2.Method呼叫366

3.run的AutoFlingRunnable run()方法

4.run同一可运行run()的方法574后0.03秒的方法

5.back to step 3