在android中三次显示一个imageview(可见和不可见)

时间:2012-07-02 05:07:06

标签: android timer imageview

我想显示三个像开/关的图像视图,如果有人知道如何显示三次imageview。我正在使用下面的代码来实现这些

 totalTimeCountInMilliseconds = 180 * 1000;     
 timeBlinkInMilliseconds = 60 * 1000;
    private  boolean blink=true;
    countDownTimer = new CountDownTimer(totalTimeCountInMilliseconds,500) {
        @Override
        public void onTick(long leftTimeInMilliseconds) {
           if ( leftTimeInMilliseconds < timeBlinkInMilliseconds ) {
                if (blink) {
                    handImg.setVisibility(View.VISIBLE);
                } else {
                    handImg.setVisibility(View.INVISIBLE);
                }
                blink = !blink;     
                }
        }
        @Override
        public void onFinish() {
            handImg.setVisibility(View.INVISIBLE);
        }
    }.start();

请帮忙,提前谢谢:)

3 个答案:

答案 0 :(得分:2)

无需创建countDownTimer或其他任何内容。

Android为您提供了精彩的alpha动画。

你去吧

Java代码

ImageView myImageView = (ImageView) findViewById(R.id.imageview);
Animation myFadeInAnimation = AnimationUtils.loadAnimation(AbcActivity.this, R.anim.blink);
myImageView.startAnimation(myFadeInAnimation);

<强> blink.xml

<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="500"
    android:fromAlpha="0.0"
    android:repeatCount="3"  <---- Image will blink 3 times
    android:repeatMode="reverse"
    android:toAlpha="1.0" />

答案 1 :(得分:0)

您可以使用Timer类

    autoUpdate = new Timer();
    autoUpdate.schedule(new TimerTask() {
        @Override
        public void run() {
            runOnUiThread(new Runnable() {
                public void run() {
                    YourLogic();
                }
            });
        }
    }, 0, 180 * 1000); // updates each 3 min  

YourLogic功能如下:

private void YourLogic() {
if ( leftTimeInMilliseconds < timeBlinkInMilliseconds ) {   
if (blink) {   
handImg.setVisibility(View.VISIBLE);   
} else {    
handImg.setVisibility(View.INVISIBLE);   
}   
blink = !blink   
}
        } 

我希望这会对你有所帮助

答案 2 :(得分:0)

试试这个

final AlphaAnimation  blinkanimation=   new AlphaAnimation(1, 0); // Change alpha from fully visible to invisible
blinkanimation.setDuration(300); // duration - half a second
blinkanimation.setInterpolator(new LinearInterpolator()); // do not alter animation rate
blinkanimation.setRepeatCount(3); // Repeat animation infinitely
blinkanimation.setRepeatMode(Animation.REVERSE);

并将此动画设置为ur imageview,如下所示

imageview.setAnimation(blinkanimation2);  or 
imageview.startAnimation(blinkanimation2);  

根据您的要求更改动画的持续时间和重复次数