我在項目中遇到如題的問題,其實講起來很簡單,但是當時面對我沒有接觸過的代碼和上萬行的代碼,一看就無從下手,當時就想寫個定時器類讓在滿足的條件下觸發,後來想想其時沒有那麼多麻煩,寫個線程使其在內容超過組建的長度時移動坐標。至於如何把它繪到標題欄上面的這裡不說了,有空再整理。
class MoveItemContentTask /*extends TimerTask*/ implements Runnable {
// private Timer timer;
private boolean ismoving;
private int moveX, tempX;
private int left, right;
public MoveItemContentTask() {
/// = new Timer();
MoveItemContentTask beigin = new MoveItemContentTask();
new Thread(beigin).start();
}
public void movingPain(int x, int y, int leftBorder, int rightBorder, boolean isFocused, Graphics g) {
moveX = x;
tempX = x;
ismoving = isFocused;
left = leftBorder;
right = rightBorder;
//while (ismoving) {
paintContent(moveX, y, leftBorder, rightBorder, g);
// }
}
public void run() {
// if (timer != null) {
changeX(moveX);
// }
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public void changeX(int x) {
if (ismoving) {
if (x < left || x > right) {
x = tempX;
} else {
x += 1;
}
}
}
}