SurfaceView的示例程序
1.package com.ray.test;
2./*
3. * SurfaceView的示例程序
4. * 演示其流程
5. */
6.import android.app.Activity;
7.import android.content.Context;
8.import android.graphics.Canvas;
9.import android.graphics.Color;
10.import android.graphics.Paint;
11.import android.graphics.RectF;
12.import android.os.Bundle;
13.import android.view.SurfaceHolder;
14.import android.view.SurfaceView;
15.
16.public class Test extends Activity {
17. public void onCreate(Bundle savedInstanceState) {
18. super.onCreate(savedInstanceState);
19. setContentView(new MyView(this));
20. }
21.
22. //内部类
23. class MyView extends SurfaceView implements SurfaceHolder.Callback{
24.
25. SurfaceHolder holder;
26. public MyView(Context context) {
27. super(context);
28. holder = this.getHolder();//获取holder
29. holder.addCallback(this);
30. //setFocusable(true);
31.
32. }
33.
34. @Override
35. public void surfaceChanged(SurfaceHolder holder, int format, int width,
36. int height) {
37.
38. }
39.
40. @Override
41. public void surfaceCreated(SurfaceHolder holder) {
42. new Thread(new MyThread()).start();
43. }
44.
45. @Override
46. public void surfaceDestroyed(SurfaceHolder holder) {
47.
48. }
49.
50. //内部类的内部类
51. class MyThread implements Runnable{
52.
53. @Override
54. public void run() {
55. Canvas canvas = holder.lockCanvas(null);//获取画布
56. Paint mPaint = new Paint();
57. mPaint.setColor(Color.BLUE);
58.
59. canvas.drawRect(new RectF(40,60,80,80), mPaint);
60. holder.unlockCanvasAndPost(canvas);//解锁画布,提交画好的图像
61.
62. }
63.
64. }
65.
66. }
北大青鸟鲁广校区 曹老师
------解决方案--------------------