在循环中创建包含5个图像的幻灯片

时间:2014-02-11 07:56:21

标签: java android animation transitions

我想创建一个图像幻灯片,我想重复5个图像循环,直到按下按钮。此外,我希望图像之间有一些效果,以便过渡看起来不错。我能够创建幻灯片,但问题是它不重复。 这是我的代码:

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();
    }

1 个答案:

答案 0 :(得分:0)

FrameAnimation 用于此

并添加 frameanimatin.setOneShot(false);