The following program displays full screen images that animate automatically by using the loadAnimation() method. Here is is the code.
public class MainActivity extends ActionBarActivity {
/** Called when the activity is first created. */
public int currentimageindex=0;
Timer timer;
TimerTask task;
ImageView slidingimage;
private int[] IMAGE_IDS = {
R.drawable.picture1, R.drawable.picture2, R.drawable.picture3,
R.drawable.picture4
};
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
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 = 3000; // repeat every 4 sec.
Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
public void run() {
mHandler.post(mUpdateResults);
}
}, delay, period);
}
/**
* 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 leftToRight = AnimationUtils.loadAnimation(this, R.anim.left_to_right);
slidingimage.startAnimation(leftToRight);
}
}
As you can see the full sized picture change every 4 seconds. Now what i want to do is to stop the animation after the appearance of the last image(picture 4 in this case). After fixing this,I will try fetch images from a server using JSON.
Thank you,
Theo.
1)You have to load animation until only you have image in IMAGE_IDS array
2)Then stop your timer also
Add following lines in AnimateandSlideShow() below currentimageindex++
if(currentimageindex <= IMAGE_IDS.length)
{
Animation leftToRight = AnimationUtils.loadAnimation(this, R.anim.letf_anim);
slidingimage.startAnimation(leftToRight);
}
else
{
timer.cancel();
}
Also replace following line
Timer timer = new Timer();
To
timer = new Timer();
I have a problem with an handler that has to be executed every X seconds inside a Service. Basically the timing is not precise at all, some times the handler is called every X seconds, then nothing for 30 seconds and then many calls in a single second.
Handler handler = new Handler();
handler.postDelayed(runnable, 0);
private Runnable runnable = new Runnable() {
#Override
public void run() {
new postToCassandra().execute();
handler.postDelayed(this, CassandraPostBatch.TIME_INTERVAL);
}
};
It might be your asynchronous task causing the delay.
Just a note, your scheduling implementation looks a little strange. Maybe you should try something like this, could prove to be more effective.
private static final int TIMER_RATE = 30000;
private static final int TIMER_DELAY = 0;
private Timer timer;
private void startTimer() {
cancelTimer();
scheduleTimer();
}
private void scheduleTimer() {
timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
#Override
public void run() {
new postToCassandra().execute();
}
}, TIMER_DELAY, TIMER_RATE);
}
private void cancelTimer() {
if (timer != null)
timer.cancel();
}
and then just call startTimer(). You could also implement a listener in your PostToCassandra task that will notify the TimerTask when it is done so that it can start with the next post.
I have an image view. So in the image view i want to change the images after a certain time period. Images are coming from an array list. Now when the no of images are 3 or more than 3, it is working perfect. But when it is 2, my logic is not working. Second image is visible for a moment and then again changed to first image here is my code:
r = new Runnable(){
int i = 0;
public void run(){
iv.setImageBitmap(alBmps.get(i));
i++;
if(i >= alBmps.size()){
i = 0;
}
iv.postDelayed(r, 5000); //set to go off again in 5 seconds.
}
};
iv.postDelayed(r, 1000);
Can any one help me what changes i need on the above code?
Thanks.
Try this
declare variables
static int i=0;
private Timer myTimer;
in your onCreate or on button click where you want to call and start the methods
myTimer = new Timer();
myTimer.schedule(new TimerTask()
{
#Override
public void run()
{
TimerMethod();
}
}, 500, 5000);
add these methods to your class
private void TimerMethod()
{
this.runOnUiThread(Timer_Tick);
}
private Runnable Timer_Tick = new Runnable()
{
public void run()
{
if(i<alBmps.size())
{
iv.setImageBitmap(alBmps.get(i));
}
else
{
i=0;
iv.setImageBitmap(alBmps.get(i));
}
i++;
}
};
I am using an ImageSwitcher with a TouchListener to change images from an array. Its working fine but i want it to switch images every x seconds or so, so that I can add imageSwitcher.setImageResource(imageList[curIndex]); to it.
Any suggestions?
Try this,
imageSwitcher.postDelayed(new Runnable() {
int i = 0;
public void run() {
imageSwitcher.setImageResource(
i++ % 2 == 0 ?
R.drawable.image1 :
R.drawable.mage2);
imageSwitcher.postDelayed(this, 1000);
}
}, 1000);
I think it is possible via TimerTask and Timer. please Try this code. I think It help you.
private Handler mHandler;
private Runnable mUpdateResults;
private Timer timerAnimate;
private TimerTask timerTask;
mHandler = new Handler();
mUpdateResults = new Runnable() {
public void run() {
AnimateandSlideShow();
}
};
int delay = 0;
int period = 15000;
timerAnimate = new Timer();
timerTask = new TimerTask() {
public void run() {
mHandler.post(mUpdateResults);
}
};
timerAnimate.scheduleAtFixedRate(timerTask, delay, period);
Public void AnimateandSlideShow()
{
imageSwitcher.setImageResource(imageList[curIndex]);
///Here You need To handle curIndex position.
}
I want to display several images with varying intervals, for the time, i can show the pictures but at equal intervals, as may make it show at varying intervals? for example, the first image displayed 5 seconds, the second image 7 seconds, the third image 4 seconds, etc... any ideas and/or examples?
this is my activity that I'm showing the pictures, but in equal intervals:
public class WCourse extends Activity implements OnClickListener, OnTouchListener, OnCompletionListener, OnBufferingUpdateListener{
//mp3 player
private ImageButton buttonPlayPause;
private SeekBar seekBarProgress;
public EditText editTextSongURL;
public static TextView TextViewTopTitle;
//Slider
public int currentimageindex=0;
Timer timer;
TimerTask task;
ImageView slidingimage;
//images ids
private int[] IMAGE_IDS = {
R.drawable.c5_d1, R.drawable.c5_d2, R.drawable.c5_d3, R.drawable.c5_d4, R.drawable.c5_d5,
R.drawable.c5_d6, R.drawable.c5_d7, R.drawable.c5_d8, R.drawable.c5_d9, R.drawable.c5_d10,
R.drawable.c5_d11, R.drawable.c5_d12, R.drawable.c5_d13, R.drawable.c5_d14, R.drawable.c5_d15,
R.drawable.c5_d16, R.drawable.c5_d17, R.drawable.c5_d18, R.drawable.c5_d19, R.drawable.c5_d20,
R.drawable.c5_d21, R.drawable.c5_d22, R.drawable.c5_d23, R.drawable.c5_d24, R.drawable.c5_d25,
R.drawable.c5_d26, R.drawable.c5_d27, R.drawable.c5_d28, R.drawable.c5_d29, R.drawable.c5_d30,
R.drawable.c5_d31, R.drawable.c5_d32, R.drawable.c5_d33, R.drawable.c5_d34, R.drawable.c5_d35,
R.drawable.c5_d36, R.drawable.c5_d37, R.drawable.c5_d38, R.drawable.c5_d39, R.drawable.c5_d40,
R.drawable.c5_d41, R.drawable.c5_d42, R.drawable.c5_d43, R.drawable.c5_d44, R.drawable.c5_d45,
R.drawable.c5_d46
};
//player
private MediaPlayer mediaPlayer;
private int mediaFileLengthInMilliseconds; // this value contains the song duration in milliseconds. Look at getDuration() method in MediaPlayer class
//slider handler
private final Handler handler = new Handler();
/** Called when the activity is first created.
* #param <MyCount>*/
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.watch_course);
//intent
TextViewTopTitle = (TextView) findViewById(R.id.TextViewTopTitle);
Bundle bundle = getIntent().getExtras();
TextViewTopTitle.setText(bundle.getString("RESULT2")+" ");
getText(bundle.getString("RESULT2"));
final Handler mHandler = new Handler();
initView();
final Runnable mUpdateResults = new Runnable() {
public void run() {
AnimateandSlideShow();
}
};
final int delay = 5000; // delay for xx sec.
final long period = 11000;//repet every xx seconds
Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
public void run() {
mHandler.post(mUpdateResults);
}
}, delay, period);}
// 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++;
slidingimage.getAnimation();}
really would appreciate your help.
public void run(){
try{
Thread.sleep(4000);
}
catch(Exception ex){
Log.e("Welcome Exception :",ex.toString());
}
mHandler.sendEmptyMessage(0);
}
why not use a random generated number instead of the currently specific values?
i mean :
final int delay = 5000; // delay for xx sec.
final long period = 11000;//repet every xx seconds
? seems like an oversite or maybe im reading this wrong