Can anyone help me with this simple code? Why doesn't the circle move smoothly? What's wrong with it?
package chaseme;
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.Window;
public class ChaseMe extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(new SampleView(this));
}
private class SampleView extends SurfaceView implements SurfaceHolder.Callback, Runnable {
private Point point;
private Thread thread;
private SurfaceHolder holder;
private Paint _rect;
private Paint _circle;
private boolean running;
private int WIDTH;
private int HEIGHT;
private float radius = 20;
public SampleView(Context context) {
super(context);
point = new Point(20, 20);
_rect = new Paint();
_rect.setColor(Color.BLACK);
_circle = new Paint();
_circle.setColor(Color.BLUE);
holder = this.getHolder();
holder.addCallback(this);
thread = new Thread(this);
}
private void updateModel() {
if(point.x > WIDTH - radius) {
point.x = 20;
}
if(point.y > HEIGHT - radius) {
point.y = 20;
}
point.x++;
point.y++;
开发者_运维技巧 }
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {}
@Override
public void surfaceCreated(SurfaceHolder holder) {
WIDTH = getWidth();
HEIGHT = getHeight();
this.setRunning(true);
thread.start();
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
boolean retry = true;
this.setRunning(false);
while (retry) {
try {
thread.join();
retry = false;
}
catch (InterruptedException e) {}
}
}
private void setRunning(boolean b) {
running = b;
}
@Override
public void run() {
while(running) {
Canvas c = null;
updateModel();
try {
c = holder.lockCanvas(null);
// synchronized (holder) {
render(c);
// }
}
catch(Exception e) {
Log.e("main", e.getMessage());
}
finally {
if(c!=null) {
holder.unlockCanvasAndPost(c);
}
}
}
}
private void render(Canvas c) {
c.drawRect(0, 0, WIDTH, HEIGHT, _rect);
c.drawCircle(point.x, point.y, radius , _circle);
//c.save();
//c.restore();
}
}
}
You can't just call point.x++ as you currently do. You need to calculate movement relative to elapsed time and screen size.
Step 1: At every frame, calculate how much time has passed since last frame by doing
long now = System.currentTimeMillis();
elapsed = (now - mLastTime);
totalTimeElapsed += elapsed;
and then at the end of your main loop you do
mLastTime = now;
Step 2. Get screen ratio:
screenWidth = MyClass.this.getWidth();
screenHeight = MyClass.this.getHeight();
float a = screenWidth;
float b = screenHeight;
screenRatioX = a/WIDTH_OF_YOUR_PHONE;
screenRatioY = b/HEIGTH_OF_YOUR_PHONE;
Step 3. Now you can start doing animations, for instance, if you want to move your circle from right to left:
spriteX1 = (int) ((spriteX1 + (VELOCITY*screenRatioX*elapsed))+0.5);
spriteX2 = spriteX1 + spriteWidth;
Start with a velocity of 2.0 or something and tweak from there.
Good luck!
I think for smooth moving of object you should use gesture listener functionality of on touch event .
Below link may help you.
1.http://mobile.tutsplus.com/tutorials/android/android-gesture/ 2.http://stackoverflow.com/questions/937313/android-basic-gesture-detection
Have tested the code on my desire and it runs very smooth.
I suggest to test this kind of projects on real devices, the emulator can be very unpredictable and slow depending also on your cpu.
Not a big deal but you can clear your canvas with
canvas.drawColor(Color.BLACK);
instead of drawing a rectangle.
精彩评论