根据别人的例子自己写了,并理解了下
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout android:id="@+id/LinearLayout01"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button android:id="@+id/Button01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button1"
/>
<Button android:id="@+id/Button02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button2"
/>
</LinearLayout>
<SurfaceView android:id="@+id/SurfaceView01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>
package MyTestSurfaceView.code;
import java.util.Timer;
import java.util.TimerTask;
import android.app.Activity;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {
/** Called when the activity is first created. */
private Button btnSimpleDraw;
private Button btnTimerDraw;
private SurfaceView sfv;
private SurfaceHolder sfh;
private Timer mTimer;
private MyTimerTask mTimerTask;
int Y_axis[],centerY,oldX,oldY,currentX;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnSimpleDraw = (Button)findViewById(R.id.Button01);
btnTimerDraw = (Button)findViewById(R.id.Button02);
btnSimpleDraw.setOnClickListener(new ClickEvent());
btnTimerDraw.setOnClickListener(new ClickEvent());
sfv = (SurfaceView)findViewById(R.id.SurfaceView01);
sfh = sfv.getHolder();
mTimer = new Timer();
mTimerTask = new MyTimerTask();
centerY = (getWindowManager().getDefaultDisplay().getHeight() - sfv.getTop())/ 2 ;
Y_axis = new int[getWindowManager().getDefaultDisplay().getWidth()];
oldX = 0;
oldY = 0;
for( int i = 1; i < Y_axis.length; ++ i )
{
Y_axis[i-1] = centerY - (int)(100*Math.sin(i*2*Math.PI/180));
}
}
class ClickEvent implements View.OnClickListener
{
@Override
public void onClick(View v )
{
if( v == btnSimpleDraw )
{
SimpleDraw(Y_axis.length - 1 );
}
else if( v == btnTimerDraw )
{
oldY = centerY;
mTimer.schedule(mTimerTask,0,50);
}
}
}
class MyTimerTask extends TimerTask
{
@Override
public void run()
{
SimpleDraw(currentX);
++currentX;
if( currentX == Y_axis.length - 1 )
{
ClearDraw();
currentX = 0;
oldY = centerY;
}
}
}
void SimpleDraw( int length )
{
if( length == 0 )
{
oldX = 0;
}
Canvas canvas = sfh.lockCanvas( new Rect(oldX,0,oldX + length , getWindowManager().getDefaultDisplay().getHeight()));
Paint paint = new Paint();
paint.setColor(Color.GREEN);
paint.setStrokeWidth(2);
int y;
for( int i = oldX + 1; i < length; ++ i )
{
y = Y_axis[i-1];
canvas.drawLine(oldX,oldY,i,y,paint);
Log.i("Rollrock" , "Line:" + oldX + " " + oldY + " " + i + " " + y );
oldX = i;
oldY = y;
}
sfh.unlockCanvasAndPost(canvas);
}
private void ClearDraw()
{
Canvas canvas = sfh.lockCanvas(null);
canvas.drawColor(Color.BLACK);
sfh.unlockCanvasAndPost(canvas);
}
}
附: 虽然在 xml里面我们可以看到 SurfaceView01 android:layout_height="fill_parent" ,但是通过实际的数据发现,canvas的y坐标的0点再button的下方。