我想每 1 秒更改一次 DialogFragment 内的 ImageView 图像;代码在 android 5 或 4 中运行正常。但在 android 2.3.7 (api 10) 中,它更改了一次 ui,然后不再更改 ui。同时线程到目前为止运行正常,我尝试了带有 Timer 的处理程序消息方法,处理程序正常方法,runOnUIThread。View.Post 方法但没有运气
Bitmap[] onItems = new Bitmap[3];
Bitmap[] offItems= new Bitmap[3];
ImageView[] cheatHoldrs = new ImageView[3];
public Dialog onCreateDialog(Bundle savedInstanceState) {
//all Bitmaps and ImageView has been inited
myMethod();
}
View.Post 方法:
for (int i = 0; i < 3; i++) {
final int j = i;
cheatHoldrs[j].post(new Runnable() {
@Override
public void run() {
random Random = new Random();
cheatHoldrs[j].setImageBitmap((random.nextInt(3) % 3 == 1) ? onItems[j]
: offItems[j]);
cheatHoldrs[j].postDelayed(this, 1000);
}
});
}
RunOnUIThread
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
Random random = new Random();
for (int j = 0; j < 3; j++)
cheatHoldrs[j]
.setImageBitmap((random.nextInt(3) % 3 == 1) ? onItems[j]
: offItems[j]);
new Handler().postDelayed(this, 1000);
}
});
Noraml 处理程序方法:
final Handler handler = new Handler();
handler.post(new Runnable() {
@Override
public void run() {
Random random = new Random();
for (int j = 0; j < 3; j++)
cheatHoldrs[j]
.setImageBitmap((random.nextInt(3) % 3 == 1) ? onItems[j]
: offItems[j]);
handler.postDelayed(this, 1000);
}
});
处理程序消息方法:{在全局范围内:
public Handler mHandler = new Handler() {
public void handleMessage(Message msg) {
Random random = new Random();
for (int j = 0; j < 3; j++)
cheatHoldrs[j]
.setImageBitmap((random.nextInt(3) % 3 == 1) ? onItems[j]
: offItems[j]);
}
};
在 OnCreateDialog 中:
Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
public void run() {
mHandler.obtainMessage(1).sendToTarget();
}
}, 0, 1000);
}
编辑:我终于知道问题是什么了。线程工作正常,如果我不使用动画,ui 更新。当对话框开始显示时。我开始动画并移动 ImageViews (cheatHoldrs) 。ImageViews 动画后图像没有变化。我像这样尝试了 9oldroid 动画和 TranslateAnimation,但在动画之后仍然改变 ui:对于 api > 11,我使用 ObjectAnimator,它工作正常,但对于 api < 11,我使用这种方法
TranslateAnimation animation = new TranslateAnimation(0, forLeft, 0, 0);
animation.setFillAfter(true);
animation.setDuration(1000);
cheatHoldrs[1].startAnimation(animation);
TranslateAnimation animation2 = new TranslateAnimation(0, forLeft, 0, 0);
animation2.setFillAfter(true);
animation2.setDuration(1000);
cheatHoldrs[2].startAnimation(animation2);
TranslateAnimation animation3 = new TranslateAnimation(0, forRight, 0, 0);
animation3.setFillAfter(true);
animation3.setDuration(1000);
cheatHoldrs[0].startAnimation(animation3);
和 Nineoldandroids 方法:
com.nineoldandroids.animation.ObjectAnimator transitionForFirst = com.nineoldandroids.animation.ObjectAnimator
.ofFloat(cheatHoldrs[0], "translationX", forRight);
transitionForFirst.setDuration(1000);
com.nineoldandroids.animation.ObjectAnimator transitionForSecound = com.nineoldandroids.animation.ObjectAnimator
.ofFloat(cheatHoldrs[1], "translationX", forLeft);
transitionForSecound.setDuration(1000);
com.nineoldandroids.animation.ObjectAnimator transitionForThird = com.nineoldandroids.animation.ObjectAnimator
.ofFloat(che
atHoldrs[2], "translationX", forLeft);
transitionForThird.setDuration(1000);
transitionForFirst.start();
transitionForSecound.start();
transitionForThird.start();