在我的android代码中,我的要求是:-
当我单击该按钮时,它将显示一个动画。此动画将运行4秒钟。然后它将转到另一个活动。

我的代码是:-

public class Animation_test extends Activity {

    private Handler handler;
    Context context;

    private TransparentProgressDialog pd;
    Button btnOpenPopup;

    /** Called when the activity is first created. */

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.animation_test);
        btnOpenPopup = (Button)findViewById(R.id.btn1);
         buttonperform();
        handler = new Handler();
        pd = new TransparentProgressDialog(this, R.drawable.uktrafficlights);


    }


    public void  buttonperform(){

        btnOpenPopup.setOnClickListener(new Button.OnClickListener(){

            @Override
            public void onClick(View arg0) {
                new Thread(new Task()).start();
                      }

                   });
                }


    class Task implements Runnable {
        @Override
        public void run() {

                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        pd.show();

                    }
                    Intent intent = new Intent(context, secondActivity.class);
                    startActivity(intent);
                });
            }
        }


    @Override
    protected void onDestroy() {
        //handler.removeCallbacks(new Ru);
        if (pd.isShowing() ) {
            pd.dismiss();
        }
        super.onDestroy();
    }

    private class TransparentProgressDialog extends Dialog {

        private ImageView iv;

        public TransparentProgressDialog(Context context, int resourceIdOfImage) {
            super(context, R.style.TransparentProgressDialog);
            WindowManager.LayoutParams wlmp = getWindow().getAttributes();
            wlmp.gravity = Gravity.CENTER_HORIZONTAL;
            getWindow().setAttributes(wlmp);
            setTitle(null);
            setCancelable(false);
            setOnCancelListener(null);
            LinearLayout layout = new LinearLayout(context);
            layout.setOrientation(LinearLayout.VERTICAL);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
            iv = new ImageView(context);
            iv.setImageResource(resourceIdOfImage);
            layout.addView(iv, params);
            addContentView(layout, params);
        }


        @Override
        public void show() {
            super.show();
            RotateAnimation anim = new RotateAnimation(0.0f, 360.0f , Animation.RELATIVE_TO_SELF, .5f, Animation.RELATIVE_TO_SELF, .5f);
            anim.setInterpolator(new LinearInterpolator());
            //anim.setRepeatCount(Animation.INFINITE);
            anim.setDuration(4000);
            iv.setAnimation(anim);
            iv.startAnimation(anim);

        }
    }


}


但是我的代码仅显示动画..问题出在哪里?

最佳答案

不需要处理程序和线程即可实现。动画有一个监听器Animation.AnimationListener,带有三个回调。


onAnimationStart
onAnimationEnd
onAnimationRepeat


您可以为动画注册侦听器,并在调用onAnimationEnd时开始活动。例如

anim.setAnimationListener(new Animation.AnimationListener() {
    @Override
    public void onAnimationStart(Animation animation) {

    }

    @Override
    public void onAnimationEnd(Animation animation) {
         Intent intent = new Intent(Animation_test.this, secondActivity.class);
         startActivity(intent);
    }

    @Override
    public void onAnimationRepeat(Animation animation) {

    }
});

关于java - 在Android中如何处理Runnable Handler类,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28760269/

10-14 18:49
查看更多