canvas.clipRect

clipRect(float left, float top, float right, float bottom)
Intersect the current clip with the specified rectangle, which is expressed in local coordinates.

只有在这个区域内的才显示,其他所有内容好像被裁剪掉了一样,不显示。

clipRect2

 

  1. public void onDraw(Canvas canvas)  
  2. {  
  3. super.onDraw(canvas);  
  4. /* 设置画布的颜色 */  
  5. canvas.drawColor(Color.BLACK);  
  6. /* 设置取消锯齿效果 */  
  7. mPaint.setAntiAlias(true);  
  8. //mPaint.setStyle(Paint.Style.STROKE);  
  9. mPaint.setColor(Color.RED);  
  10. //canvas.save();  
  11. // 设置裁剪区域  
  12. //canvas.drawRect(new Rect(1, 1, 320, 400),mPaint);  
  13. canvas.clipRect(4040280260);  
  14. canvas.drawColor(Color.GRAY);  
  15. /* 线锁定画布 */  
  16. canvas.save();  
  17. /* 旋转画布,顺时针旋转“画布”,以左上角为圆心 */  
  18. canvas.rotate(45.0f);  
  19. //canvas.drawRect(new Rect(40, 40, 280, 260),mPaint);  
  20. /* 设置颜色及绘制矩形 */  
  21. mPaint.setColor(Color.RED);  
  22. canvas.drawRect(new Rect(45,45,140,100), mPaint);  
  23. /* 解除画布的锁定 */  
  24. canvas.restore();  
  25. /* 设置颜色及绘制另一个矩形 */  
  26. mPaint.setColor(Color.GREEN);  
  27. canvas.drawRect(new Rect(150,75,260,120), mPaint);  
  28. }  


clipRect1

 

 

  1. public void onDraw(Canvas canvas)  
  2. {  
  3.     super.onDraw(canvas);  
  4.   
  5.     /* 设置画布的颜色 */  
  6.     canvas.drawColor(Color.BLACK);  
  7.   
  8.     /* 设置取消锯齿效果 */  
  9.     mPaint.setAntiAlias(true);  
  10.     mPaint.setStyle(Paint.Style.STROKE);//空心,不填充  
  11.     mPaint.setColor(Color.RED);  
  12.     /* 线锁定画布 */  
  13.     canvas.save();  
  14.     //整个屏幕加个边框  
  15.     canvas.drawRect(new Rect(11320400),mPaint);  
  16.     //这里不用clipRect,而改为drawRect,仅模拟裁剪的区域,而不真正裁剪  
  17.     canvas.drawRect(new Rect(4040280260),mPaint);  
  18.   
  19.     /* 旋转画布 */  
  20.     canvas.rotate(45.0f);  
  21.     //旋转后的屏幕边框  
  22.     canvas.drawRect(new Rect(11320400),mPaint);  
  23.     //裁剪框旋转后的weizhi  
  24.     canvas.drawRect(new Rect(4040280260),mPaint);  
  25.   
  26.     /* 设置颜色及绘制矩形 */  
  27.     mPaint.setColor(Color.RED);  
  28.     canvas.drawRect(new Rect(45,45,140,100), mPaint);  
  29.   
  30.     /* 解除画布的锁定 */  
  31.     canvas.restore();  
  32.   
  33.     /* 设置颜色及绘制另一个矩形 */  
  34.     mPaint.setColor(Color.GREEN);  
  35.     canvas.drawRect(new Rect(150,75,260,120), mPaint);  
  36. }  
posted @ 2012-07-19 21:07  carbs  阅读(836)  评论(0编辑  收藏  举报