0

我所拥有的: 我有 CustomView 类,我在其中打印Bitmap image;一个point position;

@Override
protected void onDraw(Canvas canvas) {
    // TODO Auto-generated method stub
    super.onDraw(canvas);
    canvas.drawBitmap(carSpriteImage, carspritePosition.x , carspritePosition.y, null);
}//end of OnDraw

我想 我想通过线程更新位置

public class AnimationHelperThread extends Thread{

CustomeView customeView;

public AnimationHelperThread(CustomeView customeView){
    // TODO Auto-generated constructor stub
    this.customeView=customeView;
}
@Override
public void run() {
    super.run();

    int x=0;
    int y=0;
    for(int i=0;i<1000;i++)
    {
        try {
            Thread.sleep(400);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        Point p=new Point();
        p.set(x,y);
        customeView.setCarspritePosition(p);
        x+=1;
        y+=2;
        Log.i("Helper Thread", "Call "+i);
    }
}

此方法无效,请给我建议

4

1 回答 1

0

您只能从 更改布局UIThread。试试这样的东西。-

Context变量添加到您的自定义线程。-

Context context;

public AnimationHelperThread(Context context, CustomeView customeView) {
    // TODO Please, remove nasty autogenerated TODO comments
    this.context = context;
    this.customeView = customeView;
}

然后,假设您要从 实例化线程Activity,您会这样做。-

AnimationHelperThread thread = new AnimationHelperThread(this, customView);

runOnUiThread最后,打电话context.-

context.runOnUiThread(new Runnable() {
    public void run() {
        Point p=new Point();
        p.set(x,y);
        customeView.setCarspritePosition(p);
    }
});

请注意,您需要将x和定义y为实例变量。

于 2013-10-13T13:10:43.197 回答