Android ApiDemos示例解析(58):Graphics->Clipping

本例介紹Canvas的裁剪功能。

Canvas提供瞭ClipPath, ClipRect, ClipRegion 等方法來裁剪,通過Path, Rect ,Region 的不同組合,Android幾乎可以支持任意現狀的裁剪區域。

android.graphics包中定義瞭Point, Rect, Path, Region 這幾種幾何形狀,Path可以為有圓弧,橢圓,二次曲線,三次曲線,線段,矩形等基本幾何圖形或是由這些基本幾何圖形組合而成,Path可以為開放或是閉合曲線。Rect提供瞭定義矩形的簡潔方法。Region則支持通過區域的“加”,“減”,“並”,“異或”等邏輯運算由多個Region組合而成。Region.Op定義瞭Region支持的區域間運算種類。

Clipping 介紹有Region運算來為Canvs定義剪裁區域後,同一幅圖最後顯示的效果。canvas.save();和canvas.restore();用於保存和恢復Canvas的狀態屬性。

drawScene定義瞭繪圖的內容:一條紅線,一個綠圓,和“Clipping”文字

[java] 
private void drawScene(Canvas canvas) { 
 canvas.clipRect(0, 0, 100, 100); 
  
 canvas.drawColor(Color.WHITE); 
  
 mPaint.setColor(Color.RED); 
 canvas.drawLine(0, 0, 100, 100, mPaint); 
  
 mPaint.setColor(Color.GREEN); 
 canvas.drawCircle(30, 70, 30, mPaint); 
  
 mPaint.setColor(Color.BLUE); 
 canvas.drawText("Clipping", 100, 30, mPaint); 

private void drawScene(Canvas canvas) {
 canvas.clipRect(0, 0, 100, 100);
 
 canvas.drawColor(Color.WHITE);
 
 mPaint.setColor(Color.RED);
 canvas.drawLine(0, 0, 100, 100, mPaint);
 
 mPaint.setColor(Color.GREEN);
 canvas.drawCircle(30, 70, 30, mPaint);
 
 mPaint.setColor(Color.BLUE);
 canvas.drawText("Clipping", 100, 30, mPaint);
}剪裁區域為兩個矩形相減:

[java] 
canvas.save(); 
canvas.translate(160, 10); 
canvas.clipRect(10, 10, 90, 90); 
canvas.clipRect(30, 30, 70, 70, Region.Op.DIFFERENCE); 
drawScene(canvas); 
canvas.restore(); 

canvas.save();
canvas.translate(160, 10);
canvas.clipRect(10, 10, 90, 90);
canvas.clipRect(30, 30, 70, 70, Region.Op.DIFFERENCE);
drawScene(canvas);
canvas.restore();剪裁區域為一個圓

[java] 
canvas.save(); 
canvas.translate(10, 160); 
mPath.reset(); 
canvas.clipPath(mPath); // makes the clip empty  
mPath.addCircle(50, 50, 50, Path.Direction.CCW); 
canvas.clipPath(mPath, Region.Op.REPLACE); 
drawScene(canvas); 
canvas.restore(); 

canvas.save();
canvas.translate(10, 160);
mPath.reset();
canvas.clipPath(mPath); // makes the clip empty
mPath.addCircle(50, 50, 50, Path.Direction.CCW);
canvas.clipPath(mPath, Region.Op.REPLACE);
drawScene(canvas);
canvas.restore();剪裁區域為兩個矩形的並集:

[java]
canvas.save(); 
canvas.translate(160, 160); 
canvas.clipRect(0, 0, 60, 60); 
canvas.clipRect(40, 40, 100, 100, Region.Op.UNION); 
drawScene(canvas); 
canvas.restore(); 

canvas.save();
canvas.translate(160, 160);
canvas.clipRect(0, 0, 60, 60);
canvas.clipRect(40, 40, 100, 100, Region.Op.UNION);
drawScene(canvas);
canvas.restore();剪裁區域為兩個矩形的異或集:

[java] 
canvas.save(); 
canvas.translate(10, 310); 
canvas.clipRect(0, 0, 60, 60); 
canvas.clipRect(40, 40, 100, 100, Region.Op.XOR); 
drawScene(canvas); 
canvas.restore(); 

canvas.save();
canvas.translate(10, 310);
canvas.clipRect(0, 0, 60, 60);
canvas.clipRect(40, 40, 100, 100, Region.Op.XOR);
drawScene(canvas);
canvas.restore();最後一個為兩個矩形的逆向差集:

[java] 
canvas.save(); 
canvas.translate(160, 310); 
canvas.clipRect(0, 0, 60, 60); 
canvas.clipRect(40, 40, 100, 100, 
 Region.Op.REVERSE_DIFFERENCE); 
drawScene(canvas); 
canvas.restore(); 

canvas.save();
canvas.translate(160, 310);
canvas.clipRect(0, 0, 60, 60);
canvas.clipRect(40, 40, 100, 100,
 Region.Op.REVERSE_DIFFERENCE);
drawScene(canvas);
canvas.restore();


作者:mapdigit

 

發佈留言