0

I currently have (among others) these classes:

public class Main extends Activity {

Panel p;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    p = new Panel(this);
    setContentView(p);
    }

@Override
public boolean onTouchEvent(MotionEvent event) {
    p.onTouchEvent(event);
    // Make your UI thread sleep.
    try {
        Thread.sleep(30);
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return true;


}

and

public class Panel extends SurfaceView implements SurfaceHolder.Callback {

private ViewThread mThread;
private ArrayList<GraphicsElement> mElements = new ArrayList<GraphicsElement>();
public static int panelHeight;
public static int panelWidth;
private int numberOfElements = 0;
private Paint mPaint;

public Panel(Context context) {
    super(context);
    getHolder().addCallback(this);
    mThread = new ViewThread(this);
    mPaint = new Paint();
    mPaint.setColor(Color.CYAN);
    mPaint.setTextSize(20);
}

public void doDraw(long elapsed, Canvas canvas) {
    canvas.drawColor(Color.parseColor("#003045"));
    if (!(mElements.size() > 15)) {
        synchronized (mElements) {
            for (GraphicsElement element : mElements) {
                element.doDraw(canvas);
            }
            canvas.drawText("FPS: " + Math.round(1000f / elapsed) + " Elements: " + numberOfElements, 10, 30, mPaint);
        }
    } else {
        mElements.clear();
        numberOfElements = 0;
    }
}

public void animate(long elapsedTime) {
    synchronized (mElements) {
        for (GraphicsElement element : mElements) {
            element.animate(elapsedTime);
        }
    }
}

@Override
public boolean onTouchEvent(MotionEvent event) {
    int action = event.getAction();
    int xspeed = 0;
    int yspeed = 0;

    if (action == MotionEvent.ACTION_DOWN || action == MotionEvent.ACTION_MOVE) {

        if (event.getX() > panelWidth / 2) {
            xspeed = 5;
        } else if (event.getX() < panelWidth / 2) {
            xspeed = -5;
        }

        synchronized (mElements) {
            for (GraphicsElement element : mElements) {
                element.changeSpeed(xspeed, yspeed);
            }
        }
    }
    return true;
}

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

@Override
public void surfaceCreated(SurfaceHolder holder) {
    if (!mThread.isAlive()) {
        mThread = new ViewThread(this);
        mThread.setRunning(true);
        mThread.start();
    }
    mElements.add(new GraphicsElement(getResources(), 80, 300));
    numberOfElements += 1;
}

public void surfaceDestroyed(SurfaceHolder holder) {

I also have ViewThread, just my animation thread, and GraphicsElement, which defines the moving object. My animation is going very slow(on touch), and I think it has something to do with my .sleep() method. Could anyone please help me ?

Edit: I'm using .sleep() because I don't want to flood TouchEvents. i'm trying to get it like: Check for TouchEvent, Sleep, Check for TouchEvent, Sleep.. etc...

4

2 回答 2

0

Firstly, you should initialize your Panel inside your onCreate() method.

Panel p = null;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    p = new Panel(this);
    setContentView(p);
}

Secondly, I do not understand why you are doing a sleep in your onTouchEvent() handler. Try taking that out, and things should speed up, especially if you think that may be the cause!

于 2011-06-14T16:16:19.190 回答
0

I've had the same issues with touch slowing things down and ended up with a similar approach to yours (sleeping the UI thread on touch events, as recommended by a Google game developer). The thing that seemed to help me was playing with the length of the sleep time. Try increasing it to 70 ms or even more and see if that helps. Most apps don't need a super high sample rate for touch input.

于 2011-06-14T17:09:01.877 回答