android SurfaceView绘制 重新学习--切图clipRect详解

解释都在代码注释中:

 1 public class SampleView extends View {

 2     private Paint mPaint;

 3     private Path mPath;

 4 

 5     public SampleView(Context context) {

 6         super(context);

 7         setFocusable(true);

 8 

 9         mPaint = new Paint();

10         mPaint.setAntiAlias(true);

11         mPaint.setStrokeWidth(6);

12         mPaint.setTextSize(16);

13         mPaint.setTextAlign(Paint.Align.RIGHT);

14 

15         mPath = new Path();

16     }

17 

18     private void drawScene(Canvas canvas) {

19         canvas.clipRect(0, 0, 100, 100);

20 

21         canvas.drawColor(Color.WHITE);

22 

23         mPaint.setColor(Color.RED);

24         canvas.drawLine(0, 0, 100, 100, mPaint);

25 

26         mPaint.setColor(Color.GREEN);

27         canvas.drawCircle(30, 70, 30, mPaint);

28 

29         mPaint.setColor(Color.BLUE);

30         canvas.drawText("Clipping", 100, 30, mPaint);

31     }

32 

33     @Override

34     protected void onDraw(Canvas canvas) {

35         canvas.drawColor(Color.GRAY);

36         canvas.save();

37         canvas.translate(10, 10);

38         drawScene(canvas);

39         canvas.restore();

40 

41         // Region.Op.DIFFERENCE B-A

42         canvas.save();

43         canvas.translate(160, 10);

44         canvas.clipRect(10, 10, 90, 90); // B

45         canvas.clipRect(30, 30, 70, 70, Region.Op.DIFFERENCE); // A

46         drawScene(canvas);

47         canvas.restore();

48         

49         // 这里因为在3.0以上的硬件上开启了硬件加速,会有问题.

50         // Region.Op.REPLACE 不论A和B的集合状况,B的范围将全部进行显示,如果和A有交集,则将覆盖A的交集范围

51 //        canvas.save();

52 //        canvas.translate(10, 160);

53 //        mPath.reset();

54 //        canvas.clipPath(mPath); // B

55 //        mPath.addCircle(50, 50, 50, Path.Direction.CCW);

56 //        canvas.clipPath(mPath, Region.Op.REPLACE);// A

57 //        drawScene(canvas);

58 //        canvas.restore();

59         

60         // Region.Op.UNION AB并集范围

61         canvas.save();

62         canvas.translate(160, 160);

63         canvas.clipRect(0, 0, 60, 60);// B

64         canvas.clipRect(40, 40, 100, 100, Region.Op.UNION);// A

65         drawScene(canvas);

66         canvas.restore();

67 

68         // Region.Op.XOR 异或,去掉AB相交的部分

69         canvas.save();

70         canvas.translate(10, 310);

71         canvas.clipRect(40, 40, 100, 100); // B

72         canvas.clipRect(0, 0, 60, 60, Region.Op.XOR); // A

73         drawScene(canvas);

74         canvas.restore();

75 

76         // Region.Op.REVERSE_DIFFERENCE A-B

77         canvas.save();

78         canvas.translate(160, 310);

79         canvas.clipRect(0, 0, 60, 60); // B

80         canvas.clipRect(40, 40, 100, 100, Region.Op.REVERSE_DIFFERENCE);// A

81         drawScene(canvas);

82         canvas.restore();

83     }

84 }

这个在游戏中还是很常用的。

关于硬件加速的问题,http://developer.android.com/guide/topics/graphics/hardware-accel.html#controlling

谷歌文档说的很清楚。

你可能感兴趣的:(SurfaceView)