解决Android帧动画在Oncreate中启动只显示第一帧

阅读更多
做了个简单的帧动画,在onCreate方法中start,发现只能看到第一帧



    
    
    
    
    
    
    
    
    





	



	ImageView fireImg = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		ImageView fireImg = (ImageView) findViewById(R.id.fire_img);
		final AnimationDrawable animDrawable = (AnimationDrawable) fireImg
				.getBackground();
		animDrawable.start();
	}


stackoverflow 上发现一个解决办法
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		ImageView fireImg = (ImageView) findViewById(R.id.fire_img);
		final AnimationDrawable animDrawable = (AnimationDrawable) fireImg
				.getBackground();
		fireImg.post(new Runnable() {
			@Override
			public void run() {
				animDrawable.start();
			}
		});
	}


或者在Activity中的onWindowFocusChanged方法中start
	@Override
	public void onWindowFocusChanged(boolean hasFocus) {
		super.onWindowFocusChanged(hasFocus);
		if (fireImg != null) {
			AnimationDrawable animDrawable = (AnimationDrawable) fireImg
					.getBackground();
			animDrawable.start();
		}
	}

你可能感兴趣的:(android)