Android Change picture every 10 seconds
Im trying to write a very basic android app that displays around 5 pictures one after each other on the screen. I want it to d开发者_如何学Cisplay a different picture after about 10 seconds. Can anyone advise me on how I would go around this. Below i have outlined what i would be looking for.
Picture 1
Picture 2 Picture 3 Picture 4 Picture 5display full screen Picture 1
wait 10 seconds Remove Picture 1 and Display Picture 2 Wait 10 seconds Remove Picture 2 and Display Picture 3 Wait 10 seconds Remove Picture 3 and Display Picture 4 Wait 10 seconds Remove Picture 4 and Display Picture 5 Wait 10 secondsStart again
Have you considered using Frame Animations?
You can specify an xml in your anim folder that contains a frame-by-frame animation, specifing each image duration, and other settings, check it out
UPDATE
You can also build a frame animation programmatically of course:
AnimationDrawable animation = new AnimationDrawable();
animation.addFrame(getResources().getDrawable(R.drawable.image1), 100);
animation.addFrame(getResources().getDrawable(R.drawable.image2), 500);
animation.addFrame(getResources().getDrawable(R.drawable.image3), 300);
animation.setOneShot(false);
ImageView imageAnim = (ImageView) findViewById(R.id.img);
imageAnim.setBackgroundDrawable(animation);
// start the animation!
animation.start()
you can use the CountDownTimer
: follow these steps :
1) declare an array
which will contain the identifients of your pictures
2 ) declare the CountDownTimer
like this :
int i=0;
new CountDownTimer(10000,1000) {
@Override
public void onTick(long millisUntilFinished) {}
@Override
public void onFinish() {
imgView.setImageDrawable(sdk.getContext().getResources().getDrawable(array[i]));
i++;
if(i== array.length-1) i=0;
start();
}
}.start();
create blink.xml
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/selected" android:oneshot="false">
<item android:drawable="@drawable/Picture_1" android:duration="10000" />
<item android:drawable="@drawable/Picture_2" android:duration="10000" />
<item android:drawable="@drawable/Picture_3" android:duration="10000" />
<item android:drawable="@drawable/Picture_4" android:duration="10000" />
<item android:drawable="@drawable/Picture_5" android:duration="10000" />
</animation-list>
put blink.xml in drawable folder and in activity Code write this.
ImageView mImageView ;
mImageView = (ImageView)findViewById(R.id.imageView); //this is your imageView
mImageView .setImageDrawable(getResources().getDrawable( R.drawable.blink));
then you will get what you want.!
Create a Runnable
that executes the change you want (I suppose it will be changing an ImageView
's bitmap/drawable), and post them with delay to the main thread loop, using a Handler
and its postDelayed()
method.
To make it a loop, you could have the runnable post itself.
精彩评论