Initializing an Image Switcher Control

To initialize an ImageSwitcher control, you simply set its ViewFactory and then use one of the three methods to set the image. In this case, you use the setImageDrawable() method, like so:

ImageSwitcher questionImageSwitcher = (ImageSwitcher)

findViewById(; questionImageSwitcher.setFactory(new MyImageSwitcherFactory()); Drawable image = getQuestionImageDrawable(startingQuestionNumber); questionImageSwitcher.setImageDrawable(image);

Unfortunately, you cannot use the setImageURI() method with a remote (online) URL with ImageSwitcher at this time. Instead, you need to perform a bit of extra work to download the image from the supplied URL and save it into an image Drawable object. The implementation of the getQuestionImageDrawable() method does just that:

private Drawable getQuestionImageDrawable(int questionNumber) {

Drawable image;

URL imageUrl;

// Create a Drawable by decoding a stream from a remote URL imageUrl = new URL(getQuestionImageUrl(questionNumber)); Bitmap bitmap = BitmapFactory.decodeStream(imageUrl.openStream()); image = new BitmapDrawable(bitmap);

Log.e(DEBUG_TAG, "Decoding Bitmap stream failed. ); image = getResources().getDrawable(R.drawable.noquestion);

return image;

The getQuestionImageUrl() method is a simple helper method that retrieves the appropriate graphic web address for a given question. This information is stored in the Hashtable of questions. (We'll talk more about how we handle questions in a moment.) For the full implementation of the getQuestionImageUrl() method, see the code provided with this book.

You use the URL class to encapsulate the remote address to the PNG image file you want to load into ImageSwitcher. You then dump the data into BitmapDrawable. Finally, using the stream methods requires the android.per-mission.INTERNET permission, which needs to be added to the Android manifest file for the project.

Updating ImageSwitcher

When you want to update ImageSwitcher with a new ImageView control, you call the setImageDrawable() method:

ImageSwitcher questionImageSwitcher =

(ImageSwitcher) findViewById(; Drawable image = getQuestionImageDrawable(nextQuestionNumber); questionImageSwitcher.setImageDrawable(image);

Calling the setImageDrawable() method causes MyImageSwitcherFactory to generate a new ImageView control with the Drawable object supplied in the setImageDrawable() parameter.

Animating ViewSwitcher

To animate the transition between the child View controls of ViewSwitcher, you use the setInAnimation() and setOutAnimation() methods. For example, to add fade-in and fade-out animations to the TextSwitcher control, you could load and set the built-in Android fade animations as follows:

Animation in = AnimationUtils.IoadAnimation(this, android.R.anim.fade_in); Animation out = AnimationUtils.loadAnimation(this, android.R.anim.fade_out); TextSwitcher questionTextSwitcher =

(TextSwitcher) findViewById(; questionTextSwitcher.setInAnimation(in); questionTextSwitcher.setOutAnimation(out);

Now each time the setText() method or setCurrentText() method of TextSwitcher is called, this fade animation will run. You can now improve the question transition further by adding the same animations to the ImageSwitcher displaying the question images.

Was this article helpful?

0 0


  • barbara rondeau
    How to create drawable from remote url in android?
    8 years ago

Post a comment