繁体   English   中英

在Android中如何处理可运行处理程序类

[英]In android how to Handel Runnable Handler class

在我的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 ,带有三个回调。

  1. onAnimationStart
  2. onAnimationEnd
  3. 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) {

    }
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM