android button animation. How to get animation fir

2019-09-14 04:08发布

I made a button for Android that rotates on click, but when I set a button and new activity, when I click it's just set me to new activity.

I need just this: when I click on that button, first to do animation e.g. rotate, then to execute a new activity. Here is my code:

ImageButton pandaButton2 = (ImageButton) findViewById(R.id.pandaButton2);

   pandaButton2.setOnClickListener(new OnClickListener(){
       public void onClick(View v){
           v.startAnimation(pandarotate);
            startActivity(new Intent("com.example.threepandas.MENU"));
}
});

3条回答
beautiful°
2楼-- · 2019-09-14 04:14

You can set animation listener, when finish animation start your activity. Please refer this link

Example code (must update based on your purpose):

ImageButton pandaButton2 = (ImageButton) findViewById(R.id.pandaButton2);

   pandaButton2.setOnClickListener(new OnClickListener(){
       public void onClick(View v){
           v.startAnimation(pandarotate);
}
});
pandarotate.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                startActivity(new Intent("com.example.threepandas.MENU"));
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
查看更多
我命由我不由天
3楼-- · 2019-09-14 04:14

Here is the solution I use for this problem (got it from the Google I/O App Source Code on Github)

private static final int DELAY = 250;
private Handler mHandler;

@Override
        public void onClick(final View view) {
            switch (view.getId()) {
                case R.id.button:
                    mHandler.postDelayed(new Runnable() {
                        @Override
                        public void run() {

                        }
                    }, DELAY);
                    break;
            }
        }
查看更多
趁早两清
4楼-- · 2019-09-14 04:33

insert a delay for the length of the animation after the animation starts and before the activity starts

try {
    Thread.sleep(1000);                 //1000 milliseconds is one second.
} catch(InterruptedException ex) {
    Thread.currentThread().interrupt();
}
查看更多
登录 后发表回答