Now we will explore ImageSwitcher View. It is a
view useful to switch smoothly between two images and thus provides ways of
transitioning from one to another through appropriate animations.
We will implement the same concept of showing a gallery
of images that scrolls at the top of the android screen landscape and upon
selection of one image, it gets displayed as a larger image in the lower part
through the use of an ImageSwitcher. This is what I had done earlier in the
GalleryView tutorial but now instead of showing the selected picture through an
ImageView, I will show it using a ImageSwitcher. Though the output may seem very
similar, lot of other methods are available on the ImageSwitcher that can be
used, if required.
Here is how the output would look (NOTE that I
have not used the default gallery background provided by Android in the Gallery
images)
<Gallery
android:id="@+id/Gallery01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"></Gallery>
<ImageSwitcher
android:id="@+id/ImageSwitcher01"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</ImageSwitcher>
The next thing that we need to do is create a
class that not only extends Activity but also implements ViewFactory. The
ViewFactory is a Interface that creates views that need to be shown in the
ImageSwitcher. So it has one method makeView() which we need to implement. It
is here that we can set the attributes of the ImageView that would be shown
within the ImageSwitcher - like its
background, it scale, its layout parameters etc. – typically those attributes
that we would have otherwise statically set through a layout xml.
Here is the class declaration and the method
makeView():
public class ImageSwitcherView extends Activity implements ViewFactory {
and
@Override
public View makeView() {
ImageView iView = new ImageView(this);
iView.setScaleType(ImageView.ScaleType.FIT_CENTER);
iView.setLayoutParams(new
ImageSwitcher.LayoutParams(
LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
iView.setBackgroundColor(0xFF000000);
return iView;
}
This alone is the real difference from the Gallery
example.
iSwitcher = (ImageSwitcher) findViewById(R.id.ImageSwitcher01);
iSwitcher.setFactory(this);
iSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,
android.R.anim.fade_in));
iSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,
android.R.anim.fade_out));
Here we also set the animation on how the image
should fly in and fly out of the area. Then, we get a handle to the gallery and
set an ImageAdapter to it. The ImageAdpater is as described in my Gallery
Example. If you have not seen that, please go through that and then try this
example, as I would not want to repeat myself here.
Now on the click of a gallery image, we would want
to pass the selected image to the ImageSwitcher and this is what we do here:
gallery.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
iSwitcher.setImageResource(pics[arg2]);
}
});
}
This way we are done. You
may download the complete code here.
i saw yr profile Mmm very nice ..then congrat's to u..i m initial stage Android platform .so please help to me . how to get particular contact list from default layout to list layout madam..
ReplyDeleteThanks a lot Sai. That was very helpful as it was described nicely. Cheers.
ReplyDeleteExcellent post, thank you very much!
ReplyDeleteThanq u mam.
ReplyDeletereally , it's wonderful topic you have given. i have the same requirement but, i need scroll the images dynamically on top. i dont want click or touch the screen. some body help me.
ReplyDeletegood tute.......
DeleteSai, How would you implement something like this as a book? So the images 'flip' with a swipe of the finger. Similar to ebook readers changing pages. I'm just learning how to develop for Android and haven't been able to find a good example. Any help would be greatly appreciated.
ReplyDelete-Pav
The link to the Gallery Example points to www.blogger.com
ReplyDeleteIndeed. Should be to
Deletesaigeethamn.blogspot.com/2010/05/gallery-view-android-developer-tutorial.html
I think...
thanks i have requ to develop this
ReplyDeleteLaxmi
Worth reading article.
ReplyDeleteBTW, can you tell me how to make this ImageSwitcher transparent instead of black? I try to set its background to FF000000 but it doesn't work.
Hello :) I am newbie in android development and I read this article. I am also wondering how can I make the ImageSwitcher transparent. Can I ask if you already found a solution? Thanks! I really need this for a project. Thanks Again!
DeleteGood Example for beginner
ReplyDeletei suggest to add comments in your example that why particular class is used to implement particular feature/service in example so developer can also gain basic fundamental knowledge of various types of class and interface in android.
i suggest you because it is very painful to search for use of class/function/interface used in example
i have over 400 images. and this setup seems to work good but when i slide through the imageswitcher after a short while it shows all black
ReplyDeleteif i click through each individually it works fine
any suggestions?
hey thanks....it helped me
ReplyDeleteit will be much better if u add touch event for all the things
how about to use setImageURI ?
ReplyDeleteexample :
Uri imagePath = Uri.parse("android.resource://com.plugie.tpa/drawable/q1_1");
questionImageSwitcher.setImageURI(imagePath);
Hi, Thanks very much for your code snippets. Im concerned that every image is loaded as a new ImageView. Can you make an ammendment to your code to show how to clear old views or work more efficiently. I get out of memory issues with the above code. Thank you.
ReplyDeleteyou could reuse the ImageViews used in the gallery... something like this:
ReplyDelete@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView img;
if(convertView==null) {
img = new ImageView(mContext);
img.setImageResource(mImageIds[position]);
img.setLayoutParams(new Gallery.LayoutParams(150, 100));
img.setScaleType(ScaleType.FIT_XY);
img.setBackgroundResource(mGalleryItemBackground);
} else {
img = (ImageView) convertView;
}
return img;
}
Only four animations are working. they are: fade_in,fade_out, slide_in_left, slide_out_right remaining animations are not working. Emulator is displaying an error message to forceStop. Why is this happening and what is the solution
ReplyDeleteReally Nice post....
ReplyDeleteVery helpful, thanks for taking the time to post this.
ReplyDeleteHi... Thanks alot! :) It is very helpful...
ReplyDeleteGracias!
ReplyDeletenice one..but i have to set as wallpaper..how i will do that..please help me out
ReplyDelete@ganesh: The piece of code given below will set the currently viewed image as wallpaper.
ReplyDeleteBitmap temp = null;
Gallery gallery = (Gallery) findViewById(R.id.photogallery);
currentPos = gallery.getSelectedItemPosition();
temp = pics.get(currentPos);
WallpaperManager wm = WallpaperManager.getInstance(this);
if (temp != null) {
try {
wm.setBitmap(temp);
} catch (IOException e) {
e.printStackTrace();
}
Greetings Ma'am!
ReplyDeleteNice post from you..Can you help me if i want to use Button to changes the image rather than changing and selecting from Gallery view.
Thanks in advance
HIIII...Your tutorials really awesome .I am getting a problem in Gallery view because in your tutorial it is static when i am working in dynamic way that is rendering images from server how can i get images to Gallery ... from server their are string values when we call the image i need to use the string not int values for that what i need to do how can i change the string values to int values to render the images from server ?????if u know pls share it ....
ReplyDeleteThks in advance...
for setting wallpaper jst do:
ReplyDeletepublic void onItemClick(AdapterView arg0, View arg1, int arg2,
long arg3) {
iSwitcher.setImageResource(pics[arg2]);
Bitmap bmp=BitmapFactory.decodeStream(getResources().openRawResource(pics[arg2]));
try {
getApplicationContext().setWallpaper(bmp);
} catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
hi mam, the imageView is animating but in the meanwhile how to animate the gallery view images since it is static while the images are animating.! So can u provide the solution ?
ReplyDeleteHii ,, can you please help me out with an example on how to use AdapterViewFlipper in a widget
ReplyDeletehi Can any one help me! how to display SD card images in gallery view?
ReplyDeleteCopy pasted the Android SDK sample of ImageSwitcher, please bring out something new!!!
ReplyDeletehi sai i need a doubt to be clarified,
ReplyDeleteas while moving the images, is there any possible to put the background image like a person or thing in the place and the locations are to be moved, if so could please help me or mail me on this...
my mail id: bosekarthiks@gmail.com
Well done. Thank you.
ReplyDeletethis is a good example
ReplyDeleteMadam,I want take image from gallery and set image as wallpaper,how can be do this task,Please help me.
ReplyDeleteThank you verymuch. How could i give a outline for the image of the list at the bottom to show the actual selection..
ReplyDelete