AsyncTask可以实现AnimationListener吗?

时间:2014-05-05 22:09:26

标签: java android animation android-asynctask android-animation

我需要异步播放几个动画,所以我决定用AsyncTask实现AnimationListener。不幸的是,它在我开始制作动画时会抛出错误RuntimeException: An error acuured while executing doInBackground()。是否可以使用AsyncTask?

代码: 公共类游戏扩展活动{

private static final String TAG = Game.class.getSimpleName();

private Animation animation1;
private Animation animation2;
    ...
    ...
    ...
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.game);
    // animations for turning cards
    this.animation1 = AnimationUtils.loadAnimation(this, R.anim.to_middle);
    this.animation2 = AnimationUtils.loadAnimation(this, R.anim.from_middle);
        ...
        ...
    AsyncTurn turn1 = new AsyncTurn(Game.this, position);
    turn1.execute();
}

的AsyncTask:

public class AsyncTurn extends AsyncTask<Void,Void,Void> implements AnimationListener{

    private Context context;
    private int actPosition;

    public AsyncTurn(Context context, int position) {
        this.context = context;
        this.actPosition = position;
        animation1.setAnimationListener(this);
        animation2.setAnimationListener(this);
    }

    @Override
    protected Void doInBackground(Void... params) {
            ((ImageView) gridview.getChildAt(card1.getPosition())).clearAnimation();
            ((ImageView) gridview.getChildAt(card1.getPosition())).setAnimation(animation1);
                // ERROR AT NEXT LINE
            ((ImageView) gridview.getChildAt(card1.getPosition())).startAnimation(animation1);
    }

1 个答案:

答案 0 :(得分:0)

不知道为什么你需要AsyncTask但你可以使用AsyncTask函数

void onProgressUpdate(Progress... values)
doInBackground()中的

调用函数publishProgrss()

参考:AsyncTask

或使用runOnUIThread 示例:RunOnUiThread Example