01.public class AndroidTest extends Activity {
02. /** Called when the activity is first created. */
03. @Override
04. public void onCreate(Bundle savedInstanceState) {
05. super.onCreate(savedInstanceState);
06.
07. MyView mv = new MyView(this);
08. setContentView(mv);
09. }
10.
11. public class MyView extends View {
12.
13. MyView(Context context) {
14. super(context);
15. }
16.
17. @Override
18. protected void onDraw(Canvas canvas) {
19. // TODO Auto-generated method stub
20. super.onDraw(canvas);
21.
22. // 首先定义一个paint
23. Paint paint = new Paint();
24.
25. // 绘制矩形区域-实心矩形
26. // 设置颜色
27. paint.setColor(Color.BLUE);
28. // 设置样式-填充
29. paint.setStyle(Style.FILL);
30. // 绘制一个矩形
31. canvas.drawRect(new Rect(0, 0, getWidth(), getHeight()), paint);
32.
33. // 绘空心矩形
34. // 设置颜色
35. paint.setColor(Color.RED);
36. // 设置样式-空心矩形
37. paint.setStyle(Style.STROKE);
38. // 绘制一个矩形
39. canvas.drawRect(new Rect(10, 10, 100, 30), paint);
40.
41. // 绘文字
42. // 设置颜色
43. paint.setColor(Color.GREEN);
44. // 绘文字
45. canvas.drawText("Hello", 10, 50, paint);
46.
47. // 绘图
48. // 从资源文件中生成位图
49. Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.icon);
50. // 绘图
51. canvas.drawBitmap(bitmap, 10, 60, paint);
52. }
53.
54. }
55.}