需要帮助使用 onDraw() 为移动线设置动画 - JAVA

Need help to animate a moving line with onDraw() - JAVA

我通过 drawLine 方法制作了一条从底部开始到屏幕尺寸一半的线。 到目前为止,一切都很好。现在我想 "tilt" 按下屏幕时向左或向右的行。 图片中是这样的:

再次按下时,它会向另一侧倾斜,依此类推。 我现在遇到的问题是,当我倾斜这条线时,它的 "trail" 是可见的,因为我多次调用了 drawLine 方法。

这是我的代码:

public class MainGamePanel extends SurfaceView implements SurfaceHolder.Callback {
private static final String TAG = MainGamePanel.class.getSimpleName();

private MainThread thread;
private Paint paint;
private GameLine gameLine;

public MainGamePanel(Context context){
    super(context);

    getHolder().addCallback(this);

    setFocusable(true);
    paint = new Paint();
    paint.setStrokeWidth(50);
    paint.setColor(Color.WHITE);
    gameLine = new GameLine(getWidth()/2,getHeight(),getWidth()/2, getHeight()/2,paint);

    thread = new MainThread(getHolder(), this);
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {}
@Override
public void surfaceCreated(SurfaceHolder holder) {
    thread.setRunning(true);
    thread.start();
    gameLine = new GameLine(getWidth()/2,getHeight(),getWidth()/2, getHeight()/2,paint);

}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {
    ...}
@Override
public boolean onTouchEvent(MotionEvent event) {
    if(event.getAction() == MotionEvent.ACTION_DOWN){
        gameLine.setTapped(true);
    }
    return super.onTouchEvent(event);
}

public void render(Canvas canvas) {
    gameLine.drawTheLine(canvas);
}
public void update(){
    gameLine.update();
}}

public class MainThread extends Thread {

private boolean running;
private MainGamePanel gamePanel;
private SurfaceHolder surfaceHolder;

public MainThread(SurfaceHolder surfaceHolder, MainGamePanel gamePanel){
    super();
    this.gamePanel=gamePanel;
    this.surfaceHolder = surfaceHolder;
}

public void setRunning(boolean running){
    this.running = running;
}
@Override
public void run(){
    Canvas canvas;
    while (running){
        canvas = null;
        try {
            canvas = this.surfaceHolder.lockCanvas();
            synchronized (surfaceHolder){
                this.gamePanel.update();
                this.gamePanel.render(canvas);
            }
        }finally {
            if(canvas != null){
                surfaceHolder.unlockCanvasAndPost(canvas);
            }
        }
    }
}}
public class GameLine {

private float startX;
private float startY;
private float stopX;
private float stopY;
private Paint paint;
private boolean tapped = false;


public GameLine(float startX, float startY, float stopX, float stopY, Paint paint) {
    Normal Constructor
}
public boolean getTapped(){
    return tapped;
}
public void setTapped(boolean tapped){
    this.tapped = tapped;
}
public void update(){
    if(tapped){
        this.stopX -=1;
    }
}
public void drawTheLine(Canvas canvas){

    canvas.drawLine(startX, startY,stopX, stopY,paint);
}}

我正在寻找任何想法。我是否以错误的方式尝试过? 我怎样才能让踪迹不可见? 我很感激任何帮助。谢谢。

为了消除痕迹,您需要扔掉旧的 canvas(更准确地说是旧的位图)并创建一个新的。 在你的例子中我建议:

@Override
public void run(){
    Canvas canvas;
    while (running){
        canvas = null;
        try {
            canvas = this.surfaceHolder.lockCanvas();
            synchronized (surfaceHolder){
                this.gamePanel.update();
                canvas = gameLine.drawTheLine();
            }
        }finally {
            if(canvas != null){
                surfaceHolder.unlockCanvasAndPost(canvas);
            }
        }
    }
}

public Canvas drawTheLine(){
    Canvas canvas = new Canvas();
    canvas.drawLine(startX, startY,stopX, stopY,paint);
    return canvas;
}