每隔1秒在android中隐藏imageview并显示1秒

时间:2012-12-16 19:25:45

标签: android android-imageview

如何每1秒在Android中显示ImageView,然后将其隐藏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); 

上面的代码只会使图像不可见而且不会连续运行。

2 个答案:

答案 0 :(得分: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);

答案 1 :(得分: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();
}