I want to develop a image slideshow or animation like Gif animation for android. 3 or 4 images will be there and each image will display alternately.
I would use a ViewFlipper and create a thread that sleeps for the delay you want between frames, then shows the next image.
public class YourFlipperActivity extends Activity {
protected void yourFlipperForward() {
// Set animation
tflipper.setAnimation(AnimationUtils.loadAnimation(
YourFlipperActivity.this, R.anim.slide_in));
// Show next step
tflipper.showNext();
}
protected void yourFlipperBack() {
// Set animation
tflipper.setAnimation(AnimationUtils.loadAnimation(
YourFlipperActivity.this, android.R.anim.slide_in_left));
// Show next step
tflipper.showPrevious();
}
private ViewFlipper tflipper;
@Override
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.your_layout);
tflipper = (ViewFlipper) findViewById(R.id.your_flipperid);
}
public void close(View v) {
finish();
}
public void flipNext(View v) {
yourFlipperForward();
}
public void flipPrevious(View v) {
yourFlipperBack();
}
}
Next, you'll want to create an aSync task that loops your flipNext method.
Something like:
private class YourPollTask extends AsyncTask<Integer, Void, Integer> {
/**
* The system calls this to perform work in a worker thread and delivers
* it the parameters given to AsyncTask.execute()
*/
protected Integer doInBackground(Integer... millis) {
try {
int waited = 0;
int duration = millis[0].intValue();
while (waited < duration) {
Thread.sleep(1000);
waited += 1000;
flipNext();
}
}
} catch (InterruptedException e) {
// do nothing
}
return 1;
}
Good luck with that.
Alternately, if you want to use an actual animated gif, you could use GifSplitter, which is free. I don't recommend using animated gifs, though, because they are pretty old-school.
Please take a look at Android APIDemo.
精彩评论