Program 15
Program 15
xml
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Image Switcher Example"
android:id="@+id/textView"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<ImageSwitcher
android:id="@+id/imageSwitcher"
android:layout_width="match_parent"
android:layout_height="250dp"
android:layout_marginBottom="28dp"
android:layout_marginTop="40dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Next"
android:id="@+id/button"
android:layout_marginBottom="47dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
After design xml file download images and store in drawable folder in android studio.
Mainactivity.java
package com.example.program15;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.ViewSwitcher;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageSwitcher = (ImageSwitcher) findViewById(R.id.imageSwitcher);
nextButton = (Button) findViewById(R.id.button);
imageSwitcher.setFactory(new ViewSwitcher.ViewFactory() {
@Override
public View makeView() {
ImageView switcherImageView = new ImageView(getApplicationContext());
switcherImageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
switcherImageView.setImageResource(R.drawable.jsp);
return switcherImageView;
}
});
imageSwitcher.setOutAnimation(aniOut);
imageSwitcher.setInAnimation(aniIn);
nextButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
counter++;
if (counter == switcherImageLength){
counter = 0;
imageSwitcher.setImageResource(imageSwitcherImages[counter]);
}
else{
imageSwitcher.setImageResource(imageSwitcherImages[counter]);
}
}
});
}
}