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.
只有在这个区域内的才显示,其他所有内容好像被裁剪掉了一样,不显示。
- public void onDraw(Canvas canvas)
- {
- super.onDraw(canvas);
- /* 设置画布的颜色 */
- canvas.drawColor(Color.BLACK);
- /* 设置取消锯齿效果 */
- mPaint.setAntiAlias(true);
- //mPaint.setStyle(Paint.Style.STROKE);
- mPaint.setColor(Color.RED);
- //canvas.save();
- // 设置裁剪区域
- //canvas.drawRect(new Rect(1, 1, 320, 400),mPaint);
- canvas.clipRect(40, 40, 280, 260);
- canvas.drawColor(Color.GRAY);
- /* 线锁定画布 */
- canvas.save();
- /* 旋转画布,顺时针旋转“画布”,以左上角为圆心 */
- canvas.rotate(45.0f);
- //canvas.drawRect(new Rect(40, 40, 280, 260),mPaint);
- /* 设置颜色及绘制矩形 */
- mPaint.setColor(Color.RED);
- canvas.drawRect(new Rect(45,45,140,100), mPaint);
- /* 解除画布的锁定 */
- canvas.restore();
- /* 设置颜色及绘制另一个矩形 */
- mPaint.setColor(Color.GREEN);
- canvas.drawRect(new Rect(150,75,260,120), mPaint);
- }
- public void onDraw(Canvas canvas)
- {
- super.onDraw(canvas);
- /* 设置画布的颜色 */
- canvas.drawColor(Color.BLACK);
- /* 设置取消锯齿效果 */
- mPaint.setAntiAlias(true);
- mPaint.setStyle(Paint.Style.STROKE);//空心,不填充
- mPaint.setColor(Color.RED);
- /* 线锁定画布 */
- canvas.save();
- //整个屏幕加个边框
- canvas.drawRect(new Rect(1, 1, 320, 400),mPaint);
- //这里不用clipRect,而改为drawRect,仅模拟裁剪的区域,而不真正裁剪
- canvas.drawRect(new Rect(40, 40, 280, 260),mPaint);
- /* 旋转画布 */
- canvas.rotate(45.0f);
- //旋转后的屏幕边框
- canvas.drawRect(new Rect(1, 1, 320, 400),mPaint);
- //裁剪框旋转后的weizhi
- canvas.drawRect(new Rect(40, 40, 280, 260),mPaint);
- /* 设置颜色及绘制矩形 */
- mPaint.setColor(Color.RED);
- canvas.drawRect(new Rect(45,45,140,100), mPaint);
- /* 解除画布的锁定 */
- canvas.restore();
- /* 设置颜色及绘制另一个矩形 */
- mPaint.setColor(Color.GREEN);
- canvas.drawRect(new Rect(150,75,260,120), mPaint);
- }