0

我正在尝试创建移动文本。我已经创建了用于循环它的表面视图和线程。但它没有向我显示移动的动作,而是像 ** * * *无限。

但我需要的是把这一点移到那个** -> ** 明白了吗?

这是我的代码

package com.CurvePackage.Curve;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.widget.Toast;

public class Origin extends SurfaceView implements SurfaceHolder.Callback {
    Context context1;
    private MainThread thread;
    private int x=0;
    private int y=0;


    public Origin(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
        setWillNotDraw(false);
        context1 = context;
        getHolder().addCallback(this);
        thread = new MainThread(getHolder(), this);

        setFocusable(true);

    }

    @Override
    protected void onDraw(Canvas canvas) {
        // TODO Auto-generated method stub
        super.onDraw(canvas);
        Paint paint = new Paint();
        paint.setTextSize(23);
        paint.setFakeBoldText(true);
        paint.setColor(Color.YELLOW);
        // int score=(10-sprites.size()*100);
        x=x+20;
        y=y+20;
        canvas.drawText("ewqewqe", x, y, paint);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        // TODO Auto-generated method stub
        return super.onTouchEvent(event);
        // Toast.makeText(getba, "Replay clicked!", Toast.LENGTH_SHORT).show();
    }

    public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
        // TODO Auto-generated method stub

    }

    public void surfaceCreated(SurfaceHolder holder) {
        // TODO Auto-generated method stub
        thread.setRunning(true);
        thread.start();

    }

    public void surfaceDestroyed(SurfaceHolder holder) {
        thread.setRunning(false);
        // TODO Auto-generated method stub
//      boolean retry = true;
//      while (retry) {
//          try {
//              thread.join();
//              retry = false;
//          } catch (InterruptedException e) {
//              // try again shutting down the thread
//          }
//      }

    }
}

主线程

package com.CurvePackage.Curve;

import android.graphics.Canvas;
import android.view.SurfaceHolder;

public class MainThread extends Thread {

    // flag to hold game state
    private boolean running;
    private SurfaceHolder surfaceHolder;
    private Origin origin;
     static final long FPS = 15;

    public void setRunning(boolean running) {
        this.running = running;
    }

    @Override
    public void run() {

        long ticksPS = 1000 / FPS;
        long startTime;
        long sleepTime;

        while (running) {
            Canvas c = null;
            startTime = System.currentTimeMillis();
            try {
                   c = origin.getHolder().lockCanvas();
                   synchronized (origin.getHolder()) {
                       origin.onDraw(c);
                   }
            } finally {
                   if (c != null) {
                       origin.getHolder().unlockCanvasAndPost(c);
                   }
            }


            sleepTime = ticksPS-(System.currentTimeMillis() - startTime);
            try {
                   if (sleepTime > 0)
                          sleep(sleepTime);
                   else
                          sleep(10);
            } catch (Exception e) {}
        }

    }

    public MainThread(SurfaceHolder surfaceHolder, Origin origin) {
        super();
        this.surfaceHolder = surfaceHolder;
        this.origin = origin;
    }

}
4

1 回答 1

0

在绘制文本之前,您需要在 onDraw 方法中绘制整个背景。

Paint p2 = new Paint();
p2.setStyle(Style.FILL);
canvas.drawRect(0, 0, screenWidth, screenHeight, p2);
//draw text here

这将完全覆盖上一次绘制画布时的文本,并删除此拖动效果。

于 2012-05-25T05:16:54.753 回答