在Android中轮流闪烁ImageView

时间:2014-12-24 23:45:17

标签: android

我有四个ImageViews,我希望通过开关和闪光灯来闪烁它们。我保留在列表中的转弯(例如:第一个ImageView变得可见,等待一段时间,变得不可见,等待一段时间,第三个ImageView变得不可见,等待一段时间,变得可见等等)。我尝试了一些变体,但ImageViews总是在同一时间闪烁,有时主线程进入睡眠模式而不是我想要的线程。如何正确实现?

我的一次尝试:

...
//choose ImageView to flash
h = new Handler() {
        public void handleMessage(android.os.Message msg) {

            switch (msg.what) {
                case 0: flashing(CircleR); break;
                case 1: flashing(CircleY); break;
                case 2: flashing(CircleG); break;
                case 3: flashing(CircleB); break;
                default:
                    CircleR.setVisibility(View.VISIBLE);
                    CircleY.setVisibility(View.VISIBLE);
                    CircleG.setVisibility(View.VISIBLE);
                    CircleB.setVisibility(View.VISIBLE);
            }

        };
    };
...
Thread showList = new Thread (new Runnable() {
    @Override
    public void run() {



        for(int x: list){


            h.sendEmptyMessage(x);

            try {

                showList.sleep(2*PAUSE_DURATION);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }


        }
        h.sendEmptyMessage(4);


    }
});
...
//Here I start my thread
protected void SPStartTurn(){
    CircleR.setVisibility(View.INVISIBLE);
    CircleY.setVisibility(View.INVISIBLE);
    CircleG.setVisibility(View.INVISIBLE);
    CircleB.setVisibility(View.INVISIBLE);
    showList.run();

}
...
public void flashing(final View view) {
    view.setVisibility(View.VISIBLE);
    new Handler().postDelayed(new Runnable() {

        @Override
        public void run() {
            view.setVisibility(View.INVISIBLE);
            //next = true;
        }
    }, PAUSE_DURATION);
}   

1 个答案:

答案 0 :(得分:0)

使用计时器:

 static void ddd(Context context){

        CountDownTimer time = new CountDownTimer(60000, 1000) {//Now time is 1 minute
            public void onTick(long millisUntilFinished) {
            }
            public void onFinish() {
                ///////set the visibility
            }
        }.start ();
    }
    }

我认为这会有所帮助