1 MOBILE APPLICATION DEVELOPMENT – IMAGE SWITCHER
Android Image Switcher
Android image switcher provides an animation over images to transition from one
image to another.
It is an element of transition widget which helps us to add transitions on the images.
It is mainly useful to animate an image on screen.
ImageSwitcher switches smoothly between two images and thus provides a way of
transitioning from one Image to another through appropriate animations.
Syntax:
<ImageSwitcher
android:id="@+id/simpleImageSwitcher"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
Methods Of ImageSwitcher:
setFactory(ViewFactory factory): This method is used to create a new view for
ImageSwitcher. By using this method we create a new ImageView and replace the old
view with that.
setImageDrawable(Drawable drawable): This method is used to set an image
in ImageSwitcher. In this we pass an image for Drawable.
setImageResource(int resid): This method is also used to set an image in image
switcher. The image is passed in the form of integer id.
setImageURI(Uri uri): This method is also used to set an image in image switcher. The
image is passed in the form of URI.
loadAnimation(Context context, int id): This method is used whenever we need to
define an object of Animation class through AnimationUtils class by calling a static
method loadAnimation.
setInAnimation(Animation inAnimation): This method is used to set the animation of
the appearance of the object on the screen.
setOutAnimation(Animation outAnimation): This method does the opposite of
setInAnimation().
Whenever we use set a new Image in ImageView, it first removes the old view by using
setOutAnimation() method, and then places the new one by using the
setInAnimation() method.
NITHIYAPRIYA PASAVARAJ
2 MOBILE APPLICATION DEVELOPMENT – IMAGE SWITCHER
Attributes of ImageSwitcher:
1. Id: id attribute is used to uniquely identify a ImageSwitcher.
2. padding: This attribute is used to set the padding from left, right, top or bottom side
of a ImageSwitcher.
paddingRight: This attribute is used to set the padding from the right side of a
ImageSwitcher.
paddingLeft: This attribute is used to set the padding from the left side of a
ImageSwitcher.
paddingTop: This attribute is used to set the padding from the top side of a
ImageSwitcher.
paddingBottom: This attribute is used to set the padding from the bottom side of a
ImageSwitcher.
Padding: This attribute is used to set the padding from the all the side’s of a
ImageSwitcher.
3. background: This attribute is used to set the background of a ImageSwitcher. We
can set a color or a drawable in the background of a ImageSwitcher.
Example:
Step 1: Create a new project and name it ImageSwitcherExample
Step 2: Open res -> layout ->activity_main.xml (or) [Link] and add following
code :
Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="[Link]
xmlns:app="[Link]
xmlns:tools="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:layout_width="235dp"
android:layout_height="46dp"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:text="IMAGE SWITCHER"/>
NITHIYAPRIYA PASAVARAJ
3 MOBILE APPLICATION DEVELOPMENT – IMAGE SWITCHER
<ImageSwitcher
android:id="@+id/imgSwitcher"
android:layout_width="355dp"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/BtnNext"
android:layout_width="159dp"
android:layout_height="64dp"
android:text="NEXT">
</RelativeLayout>
[Link]
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class MainActivity extends AppCompatActivity {
private ImageSwitcher simpleImageSwitcher;
Button btnNext;
int imageIds[] = {[Link].img1, [Link].img2, [Link].img3, [Link].img4,
[Link].img7};
int count = [Link];
int currentIndex = -1;
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
btnNext = (Button) findViewById([Link]);
simpleImageSwitcher = (ImageSwitcher) findViewById([Link]);
[Link](new [Link]() {
public View makeView() {
ImageView imageView = new ImageView(getApplicationContext());
[Link]([Link].FIT_CENTER);
[Link](new [Link]
([Link].FILL_PARENT,
[Link].FILL_PARENT));
NITHIYAPRIYA PASAVARAJ
4 MOBILE APPLICATION DEVELOPMENT – IMAGE SWITCHER
return imageView;
}
});
Animation in = [Link](this, [Link].slide_in_left);
Animation out = [Link](this, [Link].slide_out_right);
[Link](in);
[Link](out);
[Link](new [Link]() {
public void onClick(View v) {
currentIndex++;
if (currentIndex == count)
currentIndex = 0;
[Link](imageIds[currentIndex]);
}
});
}
}
NITHIYAPRIYA PASAVARAJ