简体   繁体   English

循环播放5张图片的幻灯片

[英]Creating a slideshow with 5 images in a loop

I want to create a slideshow of images in which I want to repeat 5 images to come in a loop until a button is pressed. 我想创建一个图像幻灯片,在其中我要重复5张图像以循环播放,直到按下按钮为止。 Also I want some effect in between the images so that the transition looks nice. 另外,我还希望图像之间具有某种效果,以便过渡效果很好。 I am able to create a slideshow, but the problem is that its not repeating. 我可以创建幻灯片,但问题是它没有重复。 here is my code: 这是我的代码:

public class Slides extends Activity implements OnClickListener {
    Button button;
    Boolean goingOn = false;
    AnimationDrawable animation;
    private TransitionDrawable trans;

    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.discolight);
        final ImageView lights = (ImageView) findViewById(R.id.img);

        button=(Button)findViewById(R.id.power);
        button.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                if(goingOn)
                {
                stopAnimation();
                goingOn=false;
                }
                else
                {
                startAnimation();
                goingOn=true;
                }
                }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu m) {
        MenuInflater inf = getMenuInflater();
        inf.inflate(R.menu.frontscreenmenu, m);
        return true;

    }

    class Starter implements Runnable {
        public void run() {
            animation.start();
        }
    }

    private void startAnimation() {
        animation = new AnimationDrawable();
        animation.addFrame(getResources().getDrawable(R.drawable.a), 1000);
        animation.addFrame(getResources().getDrawable(R.drawable.b), 1000);
        animation.addFrame(getResources().getDrawable(R.drawable.c), 1000);
        animation.addFrame(getResources().getDrawable(R.drawable.d), 1000);
        animation.addFrame(getResources().getDrawable(R.drawable.e), 1000);
        animation.addFrame(getResources().getDrawable(R.drawable.f), 1000);
        animation.addFrame(getResources().getDrawable(R.drawable.g), 1000);
        animation.addFrame(getResources().getDrawable(R.drawable.h), 1000);
        animation.addFrame(getResources().getDrawable(R.drawable.i), 1000);
        animation.addFrame(getResources().getDrawable(R.drawable.j), 1000);

        animation.setOneShot(true);


        ImageView imageView = (ImageView) findViewById(R.id.img);
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
                600, 800);
        params.alignWithParent = true;
        params.addRule(RelativeLayout.CENTER_IN_PARENT);

        imageView.setLayoutParams(params);
        imageView.setImageDrawable(animation);
        imageView.post(new Starter());

    }

    private void stopAnimation() {
        animation.stop();
    }

Use The FrameAnimation for this 为此使用FrameAnimation

and add frameanimatin.setOneShot(false); 并添加frameanimatin.setOneShot(false);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM