由于今天需要在开启软件时运行帧动画,便将代码放在了onCreate方法里,结果运行卡在了第一帧。
原代码如下:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ImageView iv = findViewById(R.id.iv);
iv.setBackgroundResource(R.drawable.myan);
final AnimationDrawable animationDrawable= (AnimationDrawable) iv.getBackground();
new Thread(new Runnable() {
@Override
public void run() {
SystemClock.sleep(20);
animationDrawable.start();
}
}).start();
}
将new Thread()改成iv.post()即可,如下:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ImageView iv = findViewById(R.id.iv);
iv.setBackgroundResource(R.drawable.myan);
final AnimationDrawable animationDrawable= (AnimationDrawable) iv.getBackground();
//这里修改
iv.post(new Runnable() {
@Override
public void run() {
SystemClock.sleep(20);
animationDrawable.start();
}
});
}
成功运行。