使用线程移动视图

时间:2014-10-07 13:24:54

标签: java android multithreading

我无法使用此代码移动自定义视图: Ball.java

public class Ball extends View {
int x, y;
public Ball(Context context) {
    super(context);
}

public void setSizes( int x, int y) {
    this.x = x;
    this.y = y;
}

protected void onDraw(Canvas c) {
    Paint paint = new Paint();
    paint.setColor(Color.BLACK);
    int radius = 50;
    c.drawCircle(x,y,radius,paint);
}

}

BounceLoop.java

public class BounceLoop extends Thread {
int width, height, x, y;
boolean jumping = false;

public void setSizes(int width, int height) {
    this.width = width;
    this.height = height;
}
public void run() {
    jumping = true;
    x = 0;
    y = 0;
    while(jumping) {

    }
}

}

和MyActivity.java

public class MyActivity extends Activity {
RelativeLayout content;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_my);
    content = (RelativeLayout) findViewById(R.id.content);
    BounceLoop thread = new BounceLoop();
    thread.setSizes(content.getWidth(), content.getHeight());
    thread.start();
}



@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.my, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

}

如何从BounceLoop线程移动球?我不想用AsyncTask做,我是否必须使用处理程序或?

2 个答案:

答案 0 :(得分:0)

您的线程可能需要引用您的自定义视图,并且在run()中我会期望类似

public void run() {
    jumping = true;
    x = 0;
    y = 0;
    while(jumping) {
        mCircleView.setPosition(newX, newY);
        mCircleView.postInvalidate();
    }
}

请注意,您无法通过其他线程触摸UI,这是我调用postInvalidate()而不是invalidate()的方式。前者将在UI线程上重新安排绘制事件

答案 1 :(得分:0)

最后我这样做了:

public class MyActivity extends Activity {
RelativeLayout content;
Handler myHandler;
Ball view;
Thread loop;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_my);
    content = (RelativeLayout) findViewById(R.id.content);
    view = new Ball(this);
    view.setPosition(50, 50);

    content.addView(view);

    myHandler = new Handler() {
        public void handleMessage(Message msg) {
            int x = (int) msg.arg1;
            int y = (int) msg.arg2;
            Log.d("ss", String.valueOf(x));
            view.setPosition(x, y);
        }
    };


}

public void onWindowFocusChanged(boolean hasChanged) {
    super.onWindowFocusChanged(hasChanged);
    loop = new Thread(new Runnable() {
        @Override
        public void run() {
            int x = 0;
            int y = 0;
            Log.d("width", String.valueOf(view.getWidth()));
            while (x < content.getWidth() - view.radius) {
                x += 5;
                y += 7;
                Message msg = new Message();
                msg.arg1 = x;
                msg.arg2 = y;
                myHandler.sendMessage(msg);
                try {
                    Thread.sleep(25);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    });
    loop.start();
}

}

但是我觉得在活动中有线程是不舒服的,我怎样才能将它导出到另一个类......