animation-list旋转的地球以及Timer的使用

用到animation-list其实就是帧叠加 ,通常要注意两个问题

1.一般布局要用在frameLayout

2.它呢一般作为背景 然后在取出来

 

public class EarthAnimationActivity extends Activity {

    private static final String TAG = "EarthAnimationActivity";

	protected static final int FORWARD = 0;
	protected static final int REVERSE = 1;

	private Button earthButton;

	private AnimationDrawable earthButtonAnimation;

	protected int direction;

	
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.main);

        earthButton = (Button) findViewById(R.id.earth_button);
        earthButtonAnimation = (AnimationDrawable) earthButton.getBackground();
        direction = FORWARD;
        
        earthButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if ( ! earthButtonAnimation.isRunning() ) {
					earthButtonAnimation.start();
					earthButton.setText(R.string.click_me_to_stop);
				}
				else {
					earthButtonAnimation.stop();
					int resId = R.anim.earth_animation_rev;
					if ( direction == FORWARD ) {
						direction = REVERSE;
					}
					else {
						resId = R.anim.earth_animation;
						direction = FORWARD;
					}
					earthButton.setBackgroundResource(resId);
					earthButtonAnimation = (AnimationDrawable) earthButton.getBackground();
					earthButton.setText(R.string.click_me_to_start);
				}
			}
        	
        });
    }

	/* (non-Javadoc)
	 * @see android.app.Activity#onResume()
	 */
	@Override
	protected void onResume() {
		super.onResume();

		(new Timer(false)).schedule(new AnimationTimer(earthButtonAnimation), 100);
	}

	/* (non-Javadoc)
	 * @see android.app.Activity#onPause()
	 */
	@Override
	protected void onPause() {
		super.onPause();
		earthButtonAnimation.stop();
	}

	private static class AnimationTimer extends TimerTask {
		AnimationDrawable animation;
		
		public AnimationTimer(AnimationDrawable animation) {
			this.animation = animation;
		}

		@Override
		public void run() {
			animation.start();
			this.cancel();
		}
		
	}
    
}

 




	


 上面主vxml

下面是地球的正反旋转


	
	

	
	
	
	
	
	
	
	
	
	

 


	
	

	
	
	
	
	
	
	
	
	
	

  

 

你可能感兴趣的:(android)