文章目录更新时间:2022-01-21
1. 先来看个效果
1) 效果图2) MainActivity.java3) MyClipRectView4) activity_main.xml 2. 方法分析
1) Rect的构造方法2) canvas.clipRect(Rect rect)3) canvas.clipRect(Rect rect, Region.Op op)4) Op枚举变量说明 3. 效果演示
1) Op为 INTERSECT2) Op为 DIFFERENCE3) 全部显示4) 源码地址 4. 参考文档
1. 先来看个效果 1) 效果图 2) MainActivity.javaimport androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.view.Window; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); supportRequestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_main); } }3) MyClipRectView
import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Rect; import android.graphics.Region; import android.util.AttributeSet; import android.view.View; import androidx.annotation.Nullable; public class MyClipRectView extends View { public MyClipRectView(Context context) { super(context); } public MyClipRectView(Context context, @Nullable AttributeSet attrs) { super(context, attrs); } public MyClipRectView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); // ****** 先给当前的屏幕设置一个矩形画面 ********** int measuredHeight = getMeasuredHeight(); int measuredWidth = getMeasuredWidth(); Paint paint = new Paint(); paint.setColor(Color.GRAY); canvas.drawRect(0, 0, measuredWidth, measuredHeight, paint); // ***************************** 演示1 ***************************** Rect rect = new Rect(0, 0, measuredWidth/2, measuredHeight/3); canvas.clipRect(rect); canvas.drawColor(Color.BLUE); } }4) activity_main.xml
2. 方法分析 1) Rect的构造方法
根据4个参数,来决定矩形区域的位置。
public Rect(int left, int top, int right, int bottom) { this.left = left; this.top = top; this.right = right; this.bottom = bottom; }2) canvas.clipRect(Rect rect)
裁剪矩形区域,这个矩形的区域,由 Rect来指定。
从下方代码可以看出,会调用nClipRect方法,其中最后一个参数,传入的INTERSECT,默认传入的,咱们没有手动去指定,也可以调用另外一个clipRect方法,来进行手动指定它。
public boolean clipRect(@NonNull Rect rect) { return nClipRect(mNativeCanvasWrapper, rect.left, rect.top, rect.right, rect.bottom, Region.Op.INTERSECT.nativeInt); }3) canvas.clipRect(Rect rect, Region.Op op)
public boolean clipRect(@NonNull Rect rect, @NonNull Region.Op op) { checkValidClipOp(op); return nClipRect(mNativeCanvasWrapper, rect.left, rect.top, rect.right, rect.bottom, op.nativeInt); } private static void checkValidClipOp(@NonNull Region.Op op) { if (sCompatiblityVersion >= Build.VERSION_CODES.P && op != Region.Op.INTERSECT && op != Region.Op.DIFFERENCE) { throw new IllegalArgumentException( "Invalid Region.Op - only INTERSECT and DIFFERENCE are allowed"); } } public enum Op { DIFFERENCE(0), INTERSECT(1), UNIOn(2), XOR(3), REVERSE_DIFFERENCE(4), REPLACE(5); Op(int nativeInt) { this.nativeInt = nativeInt; } @UnsupportedAppUsage public final int nativeInt; }4) Op枚举变量说明
下方截图,AB为第一个canvas,BC为第二个canvas:
注意:28以上,只能使用INTERSECT 、DIFFERENCE ,否则会抛出异常!
3. 效果演示 1) Op为 INTERSECTimport android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Rect; import android.graphics.Region; import android.util.AttributeSet; import android.view.View; import androidx.annotation.Nullable; public class MyClipRectView extends View { public MyClipRectView(Context context) { super(context); } public MyClipRectView(Context context, @Nullable AttributeSet attrs) { super(context, attrs); } public MyClipRectView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); // ****** 先给当前的屏幕设置一个矩形画面 ********** int measuredHeight = getMeasuredHeight(); int measuredWidth = getMeasuredWidth(); Paint paint = new Paint(); paint.setColor(Color.GRAY); canvas.drawRect(0, 0, measuredWidth, measuredHeight, paint); // ***************************** 演示2 ***************************** // 第一次 Rect rect = new Rect(0, 0, 500, 500); canvas.clipRect(rect); // 第二次 rect = new Rect(300, 300, 800, 800); canvas.clipRect(rect, Region.Op.INTERSECT); canvas.drawColor(Color.RED); } }2) Op为 DIFFERENCE
import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Rect; import android.graphics.Region; import android.util.AttributeSet; import android.view.View; import androidx.annotation.Nullable; public class MyClipRectView extends View { public MyClipRectView(Context context) { super(context); } public MyClipRectView(Context context, @Nullable AttributeSet attrs) { super(context, attrs); } public MyClipRectView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); // ****** 先给当前的屏幕设置一个矩形画面 ********** int measuredHeight = getMeasuredHeight(); int measuredWidth = getMeasuredWidth(); Paint paint = new Paint(); paint.setColor(Color.GRAY); canvas.drawRect(0, 0, measuredWidth, measuredHeight, paint); // ***************************** 演示2 ***************************** // 第一次 Rect rect = new Rect(0, 0, 500, 500); canvas.clipRect(rect); // 第二次 rect = new Rect(300, 300, 800, 800); canvas.clipRect(rect, Region.Op.DIFFERENCE); canvas.drawColor(Color.RED); } }3) 全部显示
import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Rect; import android.graphics.Region; import android.util.AttributeSet; import android.view.View; import androidx.annotation.Nullable; public class MyClipRectView extends View { public MyClipRectView(Context context) { super(context); } public MyClipRectView(Context context, @Nullable AttributeSet attrs) { super(context, attrs); } public MyClipRectView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); // ****** 先给当前的屏幕设置一个矩形画面 ********** int measuredHeight = getMeasuredHeight(); int measuredWidth = getMeasuredWidth(); Paint paint = new Paint(); paint.setColor(Color.GRAY); canvas.drawRect(0, 0, measuredWidth, measuredHeight, paint); // ***************************** 演示3 ***************************** canvas.save(); // 第一次 Rect rect = new Rect(0, 0, 500, 500); canvas.clipRect(rect); canvas.drawColor(Color.BLUE); canvas.restore(); // 第二次 rect = new Rect(300, 300, 800, 800); canvas.clipRect(rect, Region.Op.INTERSECT); canvas.drawColor(Color.RED); } }4) 源码地址
地址:ClipRect
4. 参考文档Android Developers 中描述的 Canvas.clipRect方法,参考文档: 地址
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)