Android Change picture every 10 seconds

前端 未结 4 590
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-24 09:14

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 display a different picture after about 10 secon

相关标签:
4条回答
  • 2020-12-24 09:19

    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.

    0 讨论(0)
  • 2020-12-24 09:24

    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()
    
    0 讨论(0)
  • 2020-12-24 09:24

    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.!

    0 讨论(0)
  • 2020-12-24 09:26

    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();
    
    0 讨论(0)
提交回复
热议问题