Blinking effect on an Image

In this tutorial, I am going to explain blinking an image using Handler. Actually, this is an extension of SplashScreen.

In nutshell, what this tutorial is going to explain is,
An Image will be visible for one second and for the next second, it will be invisible. I am doing this using two handlers. One for making the image visible and another for making the image invisible.

First in onCreate() method, I am calling the first handler called "splashHandler" using sendMessageDelayed() method. I am passing an object of Message and variable of data type "long", its for how long the image has to invisible. Then from "splashHandler", I am calling another handler called "resumeHandler" for making the image visible. In this also, I am passing an object of Message and a variable of long type to the sendMessageDelayed() function.

Then from "resumeHandler", again calling "splashHandler with the message object and a variable of long type. So this will run continously and have a blinking effect on the image.

The source code is,


public class SplashScreen extends Activity {

ImageView img;
private final int STOPSPLASH = 0;
private final int RESUMESPLASH = 1;
private final int SPLASHTIME = 1000;
private Message stopMessage, resumeMessage;

private Handler splashHandler = new Handler() {

public void handleMessage(Message msg) {
switch(msg.what) {
case STOPSPLASH:
img.setVisibility(ImageView.GONE);
resumeMessage = new Message();
resumeMessage.what = RESUMESPLASH;
resumeHandler.sendMessageDelayed(resumeMessage, SPLASHTIME);
}
}
};

private Handler resumeHandler = new Handler() {

public void handleMessage(Message msg) {
switch(msg.what) {
case RESUMESPLASH:
img.setVisibility(ImageView.VISIBLE);
stopMessage.what = STOPSPLASH;
splashHandler.sendMessageDelayed(stopMessage, SPLASHTIME);
}
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
img = (ImageView) findViewById(R.id.img);
stopMessage = new Message();
stopMessage.what = STOPSPLASH;
splashHandler.sendMessageDelayed(stopMessage, SPLASHTIME);
}

}


I am just taken an image and explaining the flow of the application. The screen shot is just putting for your reference,



For one second, the image will be visible and in the next second, will disappear, so, will have a blinking effect on the image.

Comments

Popular posts from this blog

AlarmManager and Notification in Android

Swipe Action and ViewFlipper in Android

Difference between SurfaceView and View in Android