所以我的问题是,经过一定时间后,我无法让处理程序发布。由于某种原因,它会导致程序中的每个按钮都出错并闪烁两种颜色。我愿意提出建议,如果有什么事情比处理程序更好,我愿意切换代码。但是,我想避免使用AsyncTask,因为我对如何使用它一无所知,并且它通常会使我生气。尽管我绝对愿意使用它。
@Override
public void run() {
random = new Random();
random2 = new Random();
try{
Thread.sleep(1000);
while(running){
ran = random.nextInt(9);
type = random2.nextInt(3);
if(lives > 0){
if((type == 0 || type == 2)){
runOnUiThread(new Runnable() {
@Override
public void run() {
buttons[ran].setImageResource(R.drawable.button_red);
buttons[ran].setTag("Red");
}
});
handler.postDelayed(new Runnable() {
@Override
public void run() {
if(buttons[ran].getTag().equals("Red")){
buttons[ran].setTag("Black");
buttons[ran].setImageResource(R.drawable.button_null);
lifeLost();
}
}
}, 1000);
}else{
runOnUiThread(new Runnable() {
@Override
public void run() {
buttons[ran].setImageResource(R.drawable.button_white);
buttons[ran].setTag("White");
}
});
handler.postDelayed(new Runnable() {
@Override
public void run() {
if(buttons[ran].getTag().equals("White")){
buttons[ran].setTag("Black");
buttons[ran].setImageResource(R.drawable.button_null);
}
}
}, 1000);
}
if(score == 10){
duration -= 50;
}else if(score == 20){
duration -= 50;
}else if(score == 30){
duration -= 40;
}else if(score == 40){
duration -= 15;
}
}else{
runOnUiThread(new Runnable() {
@Override
public void run() {
finished.setVisibility(View.VISIBLE);
}
});
try{
Thread.sleep(1000);
Intent overIntent = new Intent(this, OverActivity.class);
overIntent.putExtra("point", score);
startActivity(overIntent);
overridePendingTransition(0, 0);
}catch (Exception e){}
}
}
}catch(Exception e){
e.printStackTrace();
}
}
答案 0 :(得分:0)
动画师正常工作。如有必要,您可以使用延迟代替广告。
protected ArrayList<ValueAnimator> mAnimators = new ArrayList<>();
protected Map<ValueAnimator, ValueAnimator.AnimatorUpdateListener> mUpdateListeners = new HashMap<>();
private void initAnim(){
final int[] delays = new int[]{0, 500};
for (int i = 0; i < delays.length; i++) {
final ValueAnimator animator = ValueAnimator.ofInt(/*REd*/0xFFFF8080,/*Blue*/0xFF8080FF);
animator.setDuration(800);
animator.setEvaluator(new ArgbEvaluator());
animator.setRepeatCount(ValueAnimator.INFINITE);
animator.setTarget(i);
animator.setRepeatMode(ValueAnimator.REVERSE);
animator.setStartDelay(delays[i]);//if necessary,you can use Delay;
final int index = i;
mUpdateListeners.put(animator, new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
btns[index].setBackgroundColor((Integer) animation.getAnimatedValue());
}
});
mAnimators.add(animator);
}
}
public void onStartAnimator() {
if (mAnimators == null){
initAnim();
}
for (int i = 0; i < mAnimators.size(); i++) {
ValueAnimator animator = mAnimators.get(i);
//when the animator restart , add the updateListener again because they was removed by animator stop .
ValueAnimator.AnimatorUpdateListener updateListener = mUpdateListeners.get(animator);
if (updateListener != null) {
animator.addUpdateListener(updateListener);
}
animator.start();
}
}
记住要在正确的时间完成动画,例如onDestroy
或onDetachedFromWindow
!