0

如何ImageView在Android中每1秒显示一次,然后将其隐藏1秒并再次显示1秒,无限重复?我尝试使用以下技术。我只是一个新手。

ImageView v=(ImageView)findViewbyID(R.id.mypic);
Date d=new Date();
SimpleDateFormat sdf=new SimpleDateFormat("ss");
if(sdf.format(d)%2==0)
    v.setVisibility(View.INVISIBLE); 

上面的代码只是使图像不可见,并不会连续运行。

4

2 回答 2

1

尝试使用倒数计时器

new CountDownTimer(300000, 1000) {

     boolean visible = true;
     public void onTick(long millisUntilFinished) {
           ImageView v=(ImageView)findViewbyID(R.id.mypic);
           if(visible)
               v.setVisibility(View.INVISIBLE); 
           else
               v.setVisibility(View.VISIBLE); 

            visible = !visible;
     }

     public void onFinish() {

     }
  }.start();

或者你甚至可以使用处理程序

Handler handler=new Handler()
    {
        boolean visible = true;
        @Override
        public void handleMessage(Message msg)
        {
               ImageView v=(ImageView)findViewbyID(R.id.mypic);
               if(visible)
                    v.setVisibility(View.INVISIBLE); 
               else
                    v.setVisibility(View.VISIBLE); 

            visible = !visible;
        handler.sendEmptyMessageDelayed(0, 1000);

      }
    };
    handler.sendEmptyMessage(0);
于 2012-12-16T19:29:21.853 回答
0
public Void Handle {
    new Handler().postDelayed(new Runnable() {
    boolean visible = true;
       public void run() {
      final ImageView v = (ImageView)findViewById(R.id.mypic);

      if(visible)
           v.setVisibility(View.INVISIBLE); 
       else
           v.setVisibility(View.VISIBLE); 

              }
  }, 100);

      Handle();
}
于 2012-12-16T19:38:18.393 回答