Question

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. 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();
    }
Was it helpful?

Solution

Use The FrameAnimation for this

and add frameanimatin.setOneShot(false);

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top