3
import android.os.Bundle;
import android.app.Activity;
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.view.View;
import android.view.View.OnTouchListener;

public class Play extends Activity implements OnTouchListener {

MKZSurface ourSurfaceView;
float x, y, sx, sy, fx, fy;
float cW, cH;
float ballx, bally;
float blockx = 0;
int counter = 0;
int countx = 0, county = 0;
int ballc;
long startTime, currentTime, ballStartTime, ballTime;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ourSurfaceView = new MKZSurface(this);
    ourSurfaceView.setOnTouchListener(this);
    setContentView(ourSurfaceView);
    startTime = System.currentTimeMillis();
}

@Override
protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
    ourSurfaceView.pause();

}

@Override
protected void onResume() {
    // TODO Auto-generated method stub
    super.onResume();
    ourSurfaceView.resume();
}

@Override
public boolean onTouch(View arg0, MotionEvent event) {
    // TODO Auto-generated method stub

    try {
        Thread.sleep(20);
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    x = event.getX();
    y = event.getY();

    switch (event.getAction()) {
    case MotionEvent.ACTION_DOWN:
        sx = event.getX();
        sy = event.getY();
        break;
    case MotionEvent.ACTION_UP:
        fx = event.getX();
        fy = event.getY();

        break;

    }
    return true;
}

public void svBackGround(Canvas canvas, Paint SolidC) {
    canvas.drawRect(0, (cH - 100), cW, cH, SolidC);
    canvas.drawRect(0, 0, cW, 100, SolidC);
}

public boolean ballJump() {
    if (sx != 0 && sy != 0) {
        return true;
    } else {
        return false;
    }
}

public void Jump() {
    if (ballc == 0) {
        ballStartTime = System.currentTimeMillis();
        ballc = 1;
    }
    if (ballc == 1) {
        ballTime = System.currentTimeMillis();
    }

    if (county >= -50 && counter == 0 && ballTime - ballStartTime <= 1000) {
        county -= 10;

    } else if (county <= -50 && ballTime - ballStartTime <= 1000) {
        counter = 1;
    } else if (ballTime - ballStartTime >= 1000 && county < 0) {
        county += 10;
        counter = 1;
    }

    if (ballTime - ballStartTime >= 250 && county == 0 && counter == 1) {
        sx = 0;
        sy = 0;
        ballc = 0;
        counter = 0;
    }
}

public class MKZSurface extends SurfaceView implements Runnable {

    SurfaceHolder ourHolder;
    Thread ourThread = null;
    boolean isRunning = false;

    public MKZSurface(Context context) {
        super(context);
        ourHolder = getHolder();

        // TODO Auto-generated
    }

    public void pause() {
        isRunning = false;
        while (true) {
            try {
                ourThread.join();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            break;
        }
        ourThread = null;
    }

    public void resume() {
        isRunning = true;
        ourThread = new Thread(this);
        ourThread.start();
    }

    @Override
    public void run() {
        // TODO Auto-generated method stub
        while (isRunning) {
            if (!ourHolder.getSurface().isValid())
                continue;

            Canvas canvas = ourHolder.lockCanvas();
            canvas.drawRGB(255, 255, 255);

            Paint SolidC = new Paint();
            SolidC.setColor(Color.LTGRAY);

            Paint BallC = new Paint();
            BallC.setColor(Color.CYAN);

            if (ballJump() == true) {
                Jump();
            }

            cW = canvas.getWidth();
            cH = canvas.getHeight();

            ballx += 10;
            bally = (cH - 115) + county;

            canvas.drawCircle(ballx, bally, 15, BallC);
            svBackGround(canvas, SolidC);
            ourHolder.unlockCanvasAndPost(canvas);
        }

    }
}

アニメーションはスムーズではなく、互いにフェード インしません。フレーム レートを上げようとしましたが、効果がなく、距離を縮めました。アニメーションをスムーズにするにはどうすればよいですか? 別のポイントで円を再作成するだけで、移動は表示されません。ボールが動いているように見せるにはどうすればよいですか?

4

1 に答える 1

1

new Paint()onDraw() メソッドから を削除します。BallC をクラス属性として設定すると、次のように 1 回または遅延して初期化できます。

public class MKZSurface extends SurfaceView implements Runnable {

  SurfaceHolder ourHolder;
  Thread ourThread = null;
  boolean isRunning = false;
  Paint SolidC = new Paint();
  Paint BallC = new Paint();


  public MKZSurface(Context context) {
      super(context);
      ourHolder = getHolder();
      // This must be done only once !
      SolidC.setColor(Color.LTGRAY);
      BallC.setColor(Color.CYAN);
  }

  // ...

  @Override
  public void run() {
      // TODO Auto-generated method stub
      while (isRunning) {
          if (!ourHolder.getSurface().isValid())
              continue;

          Canvas canvas = ourHolder.lockCanvas();
          canvas.drawRGB(255, 255, 255);

          if (ballJump() == true) {
              Jump();
          }

          cW = canvas.getWidth();
          cH = canvas.getHeight();

          ballx += 10;
          bally = (cH - 115) + county;

          canvas.drawCircle(ballx, bally, 15, BallC);
          svBackGround(canvas, SolidC);
          ourHolder.unlockCanvasAndPost(canvas);
      }

}

より一般的には、可能であれば、重要なコード内でオブジェクトを作成しないようにする必要があります。

于 2012-08-28T14:16:57.143 に答える