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

谷歌文檔說的很清楚。


注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
粤ICP备14056181号  © 2014-2020 ITdaan.com