停止动画并返回第二个活动

时间:2013-10-30 07:40:34

标签: android gif

我有一个对话框,可以播放gif动画。我想在一段特定时间后停止动画并返回第二个活动。但是当我完成gif动画时,活动将回到我不想要的第一个活动。 这是我试过的代码。

        ImageButton apple = (ImageButton)findViewById(R.id.imageButton1);
    apple.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            AlertDialog.Builder builder = new AlertDialog.Builder(Fruits.this);
            WebView view = new WebView(Fruits.this);
            view.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
            builder.setView(view);
            builder.create().show();
            view.loadUrl("file:///android_asset/apple.gif");

            gifIsPlaying = true;

            new Handler().postDelayed(new Runnable() {
                public void run() {
                    gifIsPlaying = false;
                    finish();
                }
            }, PLAYING_TIME_OF_GIF);

        }

    });

1 个答案:

答案 0 :(得分:1)

这是因为finish()完成了Activity。您应该dismiss() Dialog这样:

ImageButton apple = (ImageButton) findViewById(R.id.imageButton1);
apple.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {

        AlertDialog.Builder builder = new AlertDialog.Builder(Fruits.this);
        WebView view = new WebView(Fruits.this);
        view.setLayoutParams(
                new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
        builder.setView(view);
        final Dialog d = builder.create(); // save a Dialog reference here
        d.show();
        view.loadUrl("file:///android_asset/apple.gif");

        gifIsPlaying = true;

        new Handler().postDelayed(new Runnable() {
            public void run() {
                gifIsPlaying = false;
                if (d.isShowing()) { // dismiss dialog if still showing
                    d.dismiss();
                }
            }
        }, PLAYING_TIME_OF_GIF);

    }
});