在android中如何处理Runnable Handler类

时间:2015-02-27 08:11:31

标签: java android

在我的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);

        }
    }


}

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

1 个答案:

答案 0 :(得分:1)

不需要处理程序和线程来实现您想要实现的目标。动画有一个监听器Animation.AnimationListener,有三个回调。

  1. onAnimationStart
  2. onAnimationEnd
  3. onAnimationRepeat
  4. 您可以为动画注册听众,当调用onAnimationEnd时,您可以开始您的活动。 E.g

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