(1)首先在layout中添加FrameLayout,在FrameLayout中的wps.location.view.ShowLocationView是我们自己定义的一个类,它继承了surfaceView。
(2)实现类ShowLocationView。它继承了surfaceView,声明如下:
public class ShowLocationView extends SurfaceView implements Callback, Runnable{ }。具体实现看图2代码
(3)编写Activity,显示整个画面。如图3所示。
图1.layout中的XML文件(location_draw.xml)如下:
<?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:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center">
<TextView
android:id="@+id/textview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="@string/showLocation"
android:textSize="32sp"
android:textColor="#00FF00"
android:gravity="center_horizontal"/>
</LinearLayout>
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1" >
<wps.location.view.ShowLocationView
android:id="@+id/ShowLocationView"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</wps.location.view.ShowLocationView>
</FrameLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/sub"
android:id="@+id/button1"/>
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/plus"
android:id="@+id/button2"/>
</LinearLayout>
</LinearLayout>
图2.ShowLocationView类的实现:
public class ShowLocationView extends SurfaceView implements Callback, Runnable{
public static int level =5;
private int mid_x;
private int mid_y;
private Thread thread; //绘画线程
private Canvas canvas;//画布对象
private Paint paint;//画笔对象
private SurfaceHolder holder;
private int loop;
public ShowLocationView(Context context ){
super(context);
holder = getHolder();
holder.addCallback(this);
thread = new Thread(this);
}
public ShowLocationView(Context context, AttributeSet attrs ){
super(context,attrs);
holder = getHolder();
holder.addCallback(this);
thread = new Thread(this);
}
@Override
public void run() {
while(loop>0){
draw();
}
}
public void draw(){
try{
canvas = holder.lockCanvas(); //锁定画布,才可以开始画
canvas.save();
canvas.drawColor(Color.WHITE);
paint.setColor(Color.RED);
canvas.drawCircle(mid_x, mid_y, 5+level*5, paint);
canvas.restore();
}catch(Exception err) {
err.printStackTrace();
} finally {
if(canvas != null) {
holder.unlockCanvasAndPost(canvas);
}
}
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
int[] location = new int[2];
this.getLocationInWindow(location);
Log.d("SCREEN","getLocationInWindow(x) = " + location[0]+"");
Log.d("SCREEN","getLocationInWindow(y)= " + location[1]+"");
this.getLocationOnScreen(location);
mid_x = this.getWidth()/2;
mid_y = this.getHeight()/2;
Log.d("SCREEN","mid_x = " +mid_x+"");
Log.d("SCREEN","mid_y = " + mid_y+"");
Log.d("SCREEN","getLocationOnScreen(x) = " + location[0]+"");
Log.d("SCREEN","getLocationOnScreen(y)= " + location[1]+"");
Log.d("SCREEN","getWidth = " + this.getWidth()+"");
Log.d("SCREEN","getHeight = " + this.getHeight()+"");
Log.d("SCREEN","getLeft = " + this.getLeft()+"");
Log.d("SCREEN","getRight = " + this.getRight()+"");
Log.d("SCREEN","getBottom = " + this.getBottom()+"");
Log.d("SCREEN","ScrollX = " + this.getScrollX()+"");
Log.d("SCREEN","ScrollY = " + this.getScrollY()+"");
paint = new Paint();
thread.start(); //开启线程 画图
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
}
}
图3,LocationActivity
public class LocationActivity extends Activity {
private Button button1, button2;
private TextView text ;
private ShowLocationView locationView = null;
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.location_draw);
locationView = (ShowLocationView)findViewById(R.id.ShowLocationView);
text = (TextView)findViewById(R.id.textview);
button1 = (Button)findViewById(R.id.button1);
button2 = (Button)findViewById(R.id.button2);
button1.setOnClickListener(button_listener);
button2.setOnClickListener(button_listener);
}
private Button.OnClickListener button_listener =new Button.OnClickListener(){
@Override
public void onClick(View v) {
if(v.getId()==R.id.button1){ //- 缩小
if(ShowLocationView.level <=0){
Log.i("LOG","level = 0 ");
}else{
ShowLocationView.level--;
}
}
if(v.getId()==R.id.button2){ //+ 放大
if(ShowLocationView.level >=10){
Log.i("LOG","level = 10 ");
}else{
ShowLocationView.level++;
}
}
}
};}
注:在LocationActivity中,调用了setContentView(R.layout.location_draw),这样就用调用上面的XML文件作为活动窗口。然后程序会自动调用FrameLayout中的wps.location.view.ShowLocationView.class。也就是我们的surfaceView继承类。这里不需要我们手动调用,程序会自动调用,但必须记住一点:ShowLocationView中必须实现构造函数public ShowLocationView(Context context, AttributeSet attrs );因为自动调用的是这个构造函数,而不是public ShowLocationView(Context context )。