2013-07-25 33 views
0

下面的代碼使用閃爍的按鈕:按鈕閃爍在Android中沒有工作

public void blink() { 
    new Thread(new Runnable() { 
     public void run() { 
      while (true) { 
       int timeToBlink = 500; 

       try { Thread.sleep(timeToBlink); } 
       catch (Exception e) {} 

       runOnUiThread(new Runnable() { 

        @Override 
        public void run() { 
         if (btn.getVisibility() == View.VISIBLE) { 
          btn.setVisibility(View.INVISIBLE); 
         } else { 
          btn.setVisibility(View.VISIBLE); 
         } 
        } 
       }); 
      } 
     } 
    }).start(); 
} 
+0

什麼錯誤,你得到?你爲什麼說這件作品是錯的?在這裏發佈相關問題,其他明智的你會被拒絕投票,沒有人會麻煩回答。 –

回答

0

怎麼樣一個CountDownTimer?

final CountDownTimer blinkTimer = new CountDownTimer(120000 , 1000) { 
public void onTick(long millisUntilFinished) { 
    if (btn.getVisibility() == View.VISIBLE) { 
     btn.setVisibility(View.INVISIBLE); 
    } else { 
     btn.setVisibility(View.VISIBLE); 
    } 
} 

public void onFinish() { 
    blinkTimer.start(); 
} 
}; 
2

如何使用alpha動畫?

下面的代碼會幫助你。

fade.xml(保存在RES /動畫/ fade.xml)

<?xml version="1.0" encoding="utf-8"?> 
<alpha xmlns:android="http://schemas.android.com/apk/res/android" 
    android:duration="1000" 
    android:fromAlpha="0.0" 
    android:repeatCount="infinite" 
    android:repeatMode="reverse" 
    android:toAlpha="1.0" /> 

MainActivity.java

public class MainActivity extends ActionBarActivity { 

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

     Button btn= (Button) findViewById(R.id.btn); 
     Animation myFadeInAnimation = AnimationUtils.loadAnimation(MainActivity.this, 
       R.anim.fade); 
     btn.startAnimation(myFadeInAnimation); 

    } 

} 
0

這可能是因爲你在無限循環你的應用程序,所以runOnUIThread的runnable永遠不會被執行。嘗試使用奧飛動漫

0

您可以使用此:

Button button = (Button) findViewById(R.id.button1); 

Animation animation = new AlphaAnimation(0.0f, 1.0f); 
animation.setDuration(50); //You can manage the blinking time with this parameter 
animation.setStartOffset(20); 
animation.setRepeatMode(Animation.REVERSE); 
animation.setRepeatCount(Animation.INFINITE); 
button.startAnimation(animation);