2014-10-31 72 views
1

我很新的android和我想動畫的按鈕。我要做的是改變它的背景之間100毫秒快速2種顏色。但它不能很好地工作.button的背景得到改變,但隨着時間的它卡住至少5 6秒就卡住後,我不能看到動畫和UI凍結..安卓定時器動畫掛斷一段時間後

這是我的相關代碼..

int c = Color.parseColor("#00FF00"); 
    int c2 = Color.parseColor("#FFFF00"); 

    EditText t1; 
    Button b5; 
    Button b6; 
    boolean btf=false; 


    final Handler handler = new Handler(); 
    Runnable runable = new Runnable() { 

     @Override 
     public void run() { 
      try{ 
       //do your code here 
       //also call the same runnable 

       if (btf){ 
        b6.setBackgroundColor(c); 
        btf=false; 
       }else { 
        b6.setBackgroundColor(c2); 
        btf=true; 
       } 
       handler.postDelayed(this, 100); 
      } 
      catch (Exception e) { 
       // TODO: handle exception 
      } 
      finally{ 
       //also call the same runnable 
       handler.postDelayed(this, 100); 
      } 
     } 
    }; 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.boad); 

     b5= (Button) findViewById(R.id.button5); 
     b6= (Button) findViewById(R.id.button6); 

      b5.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
        handler.postDelayed(runable, 100); 
      } 
     }); 
    } 

回答

2

請通過此代碼更新您的處理程序代碼,

final Handler handler = new Handler(); 
    Runnable runable = new Runnable() { 

     @Override 
     public void run() { 
      try{ 
       //do your code here 
       //also call the same runnable 

       if (btf){ 
        b6.setBackgroundColor(c); 
        btf=false; 
       }else { 
        b6.setBackgroundColor(c2); 
        btf=true; 
       } 
      } 
      catch (Exception e) { 
       // TODO: handle exception 
      } 

    }; 
+0

thanks.it似乎工作 – 2014-10-31 12:54:16