我将尝试使用LunarLander示例作为基础来绘制形状并使其在屏幕上移动。问题在于,它们不是“移动”,而是在新位置重新绘制,旧位置仍然保留。我已经编写了最简短的代码来演示这个问题。
主要活动:
主要活动:
package ybz.test;
import ybz.fireworks.R;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
public class TestActivity extends Activity implements OnClickListener {
private static final String TAG = "CL_FireworksActivity";
MySurfaceView mySurfaceView;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
try {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
findViewById(R.id.btnStart).setOnClickListener(this);
findViewById(R.id.btnStop).setOnClickListener(this);
mySurfaceView = (MySurfaceView) (findViewById(R.id.surfaceView1));
} catch (Exception e) {
Log.d(TAG, "Failed to create; " + e.getMessage());
e.printStackTrace();
}
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnStart:
try {
mySurfaceView.startThread();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
case R.id.btnStop:
mySurfaceView.stopThread();
break;
}
}
}
自定义SurfaceView:
package ybz.test;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.Paint.Cap;
import android.graphics.Paint.Style;
import android.util.AttributeSet;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
public class MySurfaceView extends SurfaceView implements
SurfaceHolder.Callback {
private DrawThread drawThread;
private Paint paint = new Paint();
private Point location;
public MySurfaceView(Context context) {
super(context);
initialize();
}
public MySurfaceView(Context context, AttributeSet attrs) {
super(context, attrs);
initialize();
}
public MySurfaceView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
initialize();
}
private void initialize() {
getHolder().addCallback(this);
setFocusable(true);
paint.setColor(Color.BLUE);
paint.setStrokeWidth(1);
paint.setAntiAlias(true);
paint.setStrokeCap(Cap.SQUARE);
paint.setStyle(Style.FILL);
location = new Point(0, 200);
}
public void startThread() {
drawThread = new DrawThread(getHolder(), this);
drawThread.setRunning(true);
drawThread.start();
}
public void stopThread() {
drawThread.setRunning(false);
drawThread.stop();
}
public void update() {
location.x = location.x + 10;
if(location.x > getWidth()) {
location.x = 0;
}
}
public void onDraw(Canvas canvas) {
canvas.drawCircle(location.x, location.y, 15, paint);
}
class DrawThread extends Thread {
private SurfaceHolder surfaceHolder;
MySurfaceView mySurfaceView;
private boolean run = false;
public DrawThread(SurfaceHolder surfaceHolder,
MySurfaceView mySurfaceView) {
this.surfaceHolder = surfaceHolder;
this.mySurfaceView = mySurfaceView;
run = false;
}
public void setRunning(boolean run) {
this.run = run;
}
@Override
public void run() {
Canvas canvas = null;
while (run) {
try {
canvas = surfaceHolder.lockCanvas(null);
synchronized (surfaceHolder) {
mySurfaceView.onDraw(canvas);
mySurfaceView.update();
}
} finally {
if (canvas != null) {
surfaceHolder.unlockCanvasAndPost(canvas);
}
}
}
}
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
// TODO Auto-generated method stub
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
// TODO Auto-generated method stub
}
}
布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ybz.test.MySurfaceView
android:id="@+id/surfaceView1"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<LinearLayout
android:id="@+id/linearLayout2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center" >
<Button
android:id="@+id/btnStart"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:text="@string/lbl_start" />
<Button
android:id="@+id/btnStop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:text="@string/lbl_stop" />
</LinearLayout>
</LinearLayout>
canvas.drawColor(0, PorterDuff.Mode.CLEAR);
可以在绘制前清除SurfaceView画布的方法。 - Kevin Crain