首先,我使用动画来隐藏和显示TextView。我看到使用动画成本计算内存。所以我用另一种方式:
SetVisibility(VISIBLE)
和SetVisibility(INVISIBLE)
与TaskTimer
它运行良好,考虑到内存效果更好。
主要问题是多次重启计时器后,TextView消失。
我需要重新启动应用才能重新启用它!
这是代码段:
myTimerForAnimation = new Timer();
myTimerForAnimation.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
runOnUiThread(new Runnable() // run on ui thread
{
public void run() {
counter++;
if (counter < 7) {
if (counter % 2 == 1) {
list_textView[x].setVisibility(View.INVISIBLE);
} else {
list_textView[x].setVisibility(View.VISIBLE);
}
} else {
myTimerForAnimation.cancel();
myTimerForAnimation.purge();
list_textView[x].setVisibility(View.VISIBLE);
}
}
});
}
}, 1000, 600);
答案 0 :(得分:1)
不要使用Timer使用处理程序,如下所示:
// init the runnables
// the runnable should be members
Handler hanlder = new Handler();//If you arent on the UI thread pass a correct looper
for (int i=1; i<7 ; i++){
long delay = i * 1000;
if (i%2==0)
{
handler.postDelayed(mVisibleRunnable,delay);
}else{
handler.postDelayed(mInVisibleRunnable,delay);
}
}
get getnablebs应该是memebers,因为如果你选择取消回调,那么请打电话
handler.removeCallbacks(runnable);
玩它。它应该解决你的问题。
答案 1 :(得分:0)
感谢@ EE66的循环理念,我用这段代码来解决我的问题:
private void animateView(final View view){
for (int counter = 0; counter < 7; counter++) {
long delay = counter * 1000;
if (counter % 2 == 0) {
view.postDelayed(new Runnable() {
public void run() {
view.setVisibility(View.VISIBLE);
}
;
}, delay);
} else {
view.postDelayed(new Runnable() {
public void run() {
view.setVisibility(View.INVISIBLE);
}
;
}, delay);
}
}
}