main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@android:color/black">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/TextViewTopTitle"
android:text="@string/app_logo_top"
android:textColor="@color/logo_color"
android:layout_gravity="center_vertical|center_horizontal"
android:gravity="top|center"
android:textSize="@dimen/logo_size"></TextView>
<Button android:id="@+id/close"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="Exit"
android:onClick="onClick" />
<ImageView
android:id="@+id/ImageView3_Left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|center_horizontal"
></ImageView>
</LinearLayout>
SliderActivity.java
package moor.android.slide;
import android.app.Activity;
import android.os.Bundle;
import android.view.*;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.os.Handler;
import java.util.Timer;
import java.util.TimerTask;
public class SliderActivity extends Activity {
public int currentimageindex=0;
// Timer timer;
// TimerTask task;
ImageView slidingimage;
private int[] IMAGE_IDS = {
R.drawable.splash0, R.drawable.splash1, R.drawable.splash2,
R.drawable.splash3
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mygame);
final Handler mHandler = new Handler();
// Create runnable for posting
final Runnable mUpdateResults = new Runnable() {
public void run() {
AnimateandSlideShow();
}
};
int delay = 1000; // delay for 1 sec.
int period = 15000; // repeat every 4 sec.
Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
public void run() {
mHandler.post(mUpdateResults);
}
}, delay, period);
}
public void onClick(View v) {
finish();
android.os.Process.killProcess(android.os.Process.myPid());
}
/**
* Helper method to start the animation on the splash screen
*/
private void AnimateandSlideShow() {
slidingimage = (ImageView)findViewById(R.id.ImageView3_Left);
slidingimage.setImageResource(IMAGE_IDS[currentimageindex%IMAGE_IDS.length]);
currentimageindex++;
Animation rotateimage = AnimationUtils.loadAnimation(this, R.anim.fade_in);
slidingimage.startAnimation(rotateimage);
}
}
Screen shot :
Download source...
Simplest Android Slideshow!! Thanks!
ReplyDeletethe download link is not working. The source file is corrupt. But, the code given here is fine.
ReplyDeleteSee the line -
Animation rotateimage = AnimationUtils.loadAnimation(this, R.anim.fade_in);
Here, what is "R.anim.fade_in"? how can i import fade-in into anim folder?
link is working keerthi try again please...
DeleteR.anim.fade_in is a XML file which is under anim folder in res,we can create different types of animation in this folder.
thanks for your feedback...
try android.R.anim.fade_in instead.
DeleteThis comment has been removed by the author.
ReplyDeleteHello Mr.Moorthy, the code is fine, but now I want to know, how images can be loaded from res/drawable folder into IMAGE_IDS array, with a reference of the file name stored in a column say COL_DATA? I mean, I have some files in drawable folder, I have the file names in the COL_DATA Column. Now how can I retrieve the path of those images into IMAGES_IDS array to make the animation?
ReplyDeleteyou can get the images from drawable folder by using R.drawable.imagename in java code.
DeleteThank you...:)
ReplyDeletehttp://www.techipost.com/2011/06/15/android-image-slider-with-auto-slide-code/
ReplyDeleteso who is copying the code and who is original author?
getting error with "mygame"
ReplyDeleteR.layout.mygame
It is your name of your main activity
DeleteReplace by your main activity name
It isn't a tutorial. You didn't explain anything. What did you think tutorial meant?
ReplyDeleteMoorthy Bhai,,,Excellent!!! Thanks alot...you have saved my day!!1
ReplyDeletehow to create the fade_in xml file..i mean can u just post that part of code would of great help
ReplyDeleteThis comment has been removed by the author.
Delete
Deletehow can I change the rotation animation into the right to left sliding animation.
ReplyDeleteThere are two methods in R.anim class named
DeleteR.anim.slide_in_left() and R.anim.slide_out_right()
Hope you'll get your answer
How can I present my photos in a fullscreen mode (without the top bar "exit")? Can I make a 24-hour presentation?
ReplyDeleteHey I want a code that will slide image automatically as well as manually.
ReplyDeleteI want same thing from Jason data.....
ReplyDeleteI am trying to use your image slide show code in one of my android app. It is working fine, but I see a log of heap allocation/freeing messages. Googling on it, I found we get these messages when we have a bad coding style leading to heap issues.
ReplyDeleteDo you know what is the issue and if you can suggest a better coding style?
The log has these:
04-24 18:51:10.041: D/dalvikvm(2377): GC_FOR_ALLOC freed 72K, 5% free 2657K/2780K, paused 45ms, total 50ms
04-24 18:51:10.210: D/dalvikvm(2377): GC_FOR_ALLOC freed 3K, 3% free 5959K/6088K, paused 143ms, total 144ms
04-24 18:51:13.030: D/dalvikvm(2377): GC_FOR_ALLOC freed 2K, 3% free 5960K/6088K, paused 55ms, total 56ms
This is one of the best and concise sites. How I wish every one knew about it!!
ReplyDeletehow can slide more then 2 images?
ReplyDeletewhat if we want to stop/pause animation not close activity?
ReplyDeleteusing more than 30 images app cannot load or cannot work....
ReplyDeletehow can slide more than 30 images...
Paint Free is best android app for making drawings.
ReplyDeletehttp://techpave.com
simple and quick understandable. Thank you :-)
ReplyDeletethanks moorthy... it was really helpful .and worked in the first time only.... unlike other :)
ReplyDeletethanks moorthy... it was really helpful .and worked in the first time only.... unlike other :)
ReplyDeleteThanks Neha.
DeleteHi Moorthy, The link did not work for me to download the code. Please indicate another link.
ReplyDeletei want to continuesly scroll my imge like as marqueeRepeat of text view.
ReplyDeleteplz help that.