zoukankan      html  css  js  c++  java
  • Android 2D Graphics学习 Region和Canvas裁剪

    1、首先介绍Region类

    Region,中文意思即区域的意思,它表示的是canvas图层上的某一块封闭的区域。

    1. /**构造方法*/  
    2.  public Region()  //创建一个空的区域   
    3.  public Region(Region region) //拷贝一个region的范围   
    4.  public Region(Rect r)  //创建一个矩形的区域   
    5.  public Region(int left, int top, int right, int bottom) //创建一个矩形的区域   
    6.   
    7. /**一系列set方法,这些set方法,和上面构造方法形式差不多*/  
    8.  public void setEmpty() {  
    9.  public boolean set(Region region)   
    10.  public boolean set(Rect r)   
    11.  public boolean set(int left, int top, int right, int bottom)   
    12.  /*往一个Region中添加一个Path只有这种方法,参数clip代表这个整个Region的区域,在在里面裁剪出path范围的区域*/  
    13.  public boolean setPath(Path path, Region clip) //用指定的Path和裁剪范围构建一个区域   
    14.   
    15. /**几个判断方法*/  
    16. public native boolean isEmpty();//判断该区域是否为空   
    17. public native boolean isRect(); //是否是一个矩阵   
    18. public native boolean isComplex();//是否是多个矩阵组合   
    19.   
    20.   
    21. /**一系列的getBound方法,返回一个Region的边界*/  
    22. public Rect getBounds()   
    23. public boolean getBounds(Rect r)   
    24. public Path getBoundaryPath()   
    25. public boolean getBoundaryPath(Path path)   
    26.   
    27.   
    28. /**一系列的判断是否包含某点 和是否相交*/  
    29. public native boolean contains(int x, int y);//是否包含某点   
    30. public boolean quickContains(Rect r)   //是否包含某矩阵   
    31. public native boolean quickContains(int left, int top, int right,  
    32.                                         int bottom) //是否没有包含某矩阵   
    33.  public boolean quickReject(Rect r) //是否没和该矩阵相交   
    34.  public native boolean quickReject(int left, int top, int right, int bottom); //是否没和该矩阵相交   
    35.  public native boolean quickReject(Region rgn);  //是否没和该矩阵相交   
    36.   
    37. /**几个平移变换的方法*/  
    38. public void translate(int dx, int dy)   
    39. public native void translate(int dx, int dy, Region dst);  
    40. public void scale(float scale) //hide   
    41. public native void scale(float scale, Region dst);//hide   
    42.   
    43.   
    44. /**一系列组合的方法*/  
    45. public final boolean union(Rect r)   
    46. public boolean op(Rect r, Op op) {  
    47. public boolean op(int left, int top, int right, int bottom, Op op)   
    48. public boolean op(Region region, Op op)   
    49. public boolean op(Rect rect, Region region, Op op)   
    /**构造方法*/
     public Region()  //创建一个空的区域
     public Region(Region region) //拷贝一个region的范围
     public Region(Rect r)  //创建一个矩形的区域
     public Region(int left, int top, int right, int bottom) //创建一个矩形的区域
    
    /**一系列set方法,这些set方法,和上面构造方法形式差不多*/
     public void setEmpty() {
     public boolean set(Region region) 
     public boolean set(Rect r) 
     public boolean set(int left, int top, int right, int bottom) 
     /*往一个Region中添加一个Path只有这种方法,参数clip代表这个整个Region的区域,在在里面裁剪出path范围的区域*/
     public boolean setPath(Path path, Region clip) //用指定的Path和裁剪范围构建一个区域
    
    /**几个判断方法*/
    public native boolean isEmpty();//判断该区域是否为空
    public native boolean isRect(); //是否是一个矩阵
    public native boolean isComplex();//是否是多个矩阵组合
    
    
    /**一系列的getBound方法,返回一个Region的边界*/
    public Rect getBounds() 
    public boolean getBounds(Rect r) 
    public Path getBoundaryPath() 
    public boolean getBoundaryPath(Path path) 
    
    
    /**一系列的判断是否包含某点 和是否相交*/
    public native boolean contains(int x, int y);//是否包含某点
    public boolean quickContains(Rect r)   //是否包含某矩阵
    public native boolean quickContains(int left, int top, int right,
                                            int bottom) //是否没有包含某矩阵
     public boolean quickReject(Rect r) //是否没和该矩阵相交
     public native boolean quickReject(int left, int top, int right, int bottom); //是否没和该矩阵相交
     public native boolean quickReject(Region rgn);  //是否没和该矩阵相交
    
    /**几个平移变换的方法*/
    public void translate(int dx, int dy) 
    public native void translate(int dx, int dy, Region dst);
    public void scale(float scale) //hide
    public native void scale(float scale, Region dst);//hide
    
    
    /**一系列组合的方法*/
    public final boolean union(Rect r) 
    public boolean op(Rect r, Op op) {
    public boolean op(int left, int top, int right, int bottom, Op op) 
    public boolean op(Region region, Op op) 
    public boolean op(Rect rect, Region region, Op op) 
    


    上面几乎是Region的所有API,很好理解,主要说明一下最后的一组关于Region组合的方式。组合即当前的Region和另外的一个Region组合,可以用不同的Op方式来进行组合。

    Op是一个枚举,定义在Region类中。

    1. 假设用region1  去组合region2   
    2. public enum Op {  
    3.         DIFFERENCE(0), //最终区域为region1 与 region2不同的区域   
    4.         INTERSECT(1), // 最终区域为region1 与 region2相交的区域   
    5.         UNION(2),      //最终区域为region1 与 region2组合一起的区域   
    6.         XOR(3),        //最终区域为region1 与 region2相交之外的区域   
    7.         REVERSE_DIFFERENCE(4), //最终区域为region2 与 region1不同的区域   
    8.         REPLACE(5); //最终区域为为region2的区域   
    9.  }  
    假设用region1  去组合region2 
    public enum Op {
            DIFFERENCE(0), //最终区域为region1 与 region2不同的区域
            INTERSECT(1), // 最终区域为region1 与 region2相交的区域
            UNION(2),      //最终区域为region1 与 region2组合一起的区域
            XOR(3),        //最终区域为region1 与 region2相交之外的区域
            REVERSE_DIFFERENCE(4), //最终区域为region2 与 region1不同的区域
            REPLACE(5); //最终区域为为region2的区域
     }

    ApiDemo中已经提供了一个关于组合的例子,在最后面给出。

    Android还提供了一个RegionIterator来对Region中的所有矩阵进行迭代,可以使用该类,获得某个Region的所有矩阵。比较简单。

    2、什么是裁剪

    裁剪Clip,即裁剪Canvas图层,我们绘制的东西,只能在裁剪区域的范围能才能显示出来。

    1. @Override  
    2.         protected void onDraw(Canvas canvas) {  
    3.               Paint paint=new Paint();   
    4.               canvas.save();   
    5.               canvas.clipRect(new Rect(100,100,300,300));  
    6.               canvas.drawColor(Color.BLUE);//裁剪区域的rect变为蓝色    
    7.               canvas.drawRect(new Rect(0,0,100,100), paint);//在裁剪的区域之外,不能显示    
    8.               canvas.drawCircle(150,150, 50, paint);//在裁剪区域之内,能显示   
    9.               canvas.restore();  
    10.         }  
    @Override
            protected void onDraw(Canvas canvas) {
                  Paint paint=new Paint(); 
                  canvas.save(); 
                  canvas.clipRect(new Rect(100,100,300,300));
                  canvas.drawColor(Color.BLUE);//裁剪区域的rect变为蓝色 
                  canvas.drawRect(new Rect(0,0,100,100), paint);//在裁剪的区域之外,不能显示 
                  canvas.drawCircle(150,150, 50, paint);//在裁剪区域之内,能显示
                  canvas.restore();
            }
    


    裁剪并不像Matrix变换,它相对于mutable bitmap的坐标是不会改变的。所以超出裁剪区域的绘制不会被显示

    3、裁剪的保存和回滚

    在之前已经提到了,canvas.save()和canvas.restore()不仅对matrix有效,同样对clip有类似的效果。

    4、裁剪的方式

    Canvas提供了三种裁剪的方式:

    1、最基本的clipRect,裁剪一个矩形

    2、clipPath,裁剪Path包括的范围,Path所包括的范围不是空的才有效。

    3、clipRegion。

    Region在前面已经介绍过了,其实Region就是一个对区域组合的一个封装。但是它和clipRect和clipPath的最大区别在于下面:

    1.  Note that unlike  
    2. clipRect() and clipPath() which transform their arguments by the  
    3. current matrix, clipRegion() assumes its argument is already in the  
    4. coordinate system of the current layer's bitmap, and so not  
    5. transformation is performed.  
      Note that unlike
     clipRect() and clipPath() which transform their arguments by the
     current matrix, clipRegion() assumes its argument is already in the
     coordinate system of the current layer's bitmap, and so not
     transformation is performed.

    与clipRect和clipPath要使用当前的matrix进行变换不同。clipRegion不会进行转换。也就是说canvas的matrix对clipRegion没有影响。

    1. Paint paint=new Paint();  
    2.  canvas.scale(0.5f, 0.5f);  
    3.  canvas.save();  
    4.  canvas.clipRect(new Rect(100,100,200,200));//裁剪区域实际大小为50*50   
    5.  canvas.drawColor(Color.RED);  
    6.  canvas.restore();  
    7.    
    8.  canvas.drawRect(new Rect(0,0,100,100), paint);//矩形实际大小为50*50   
    9.    
    10.  canvas.clipRegion(new Region(new Rect(300,300,400,400)));//裁剪区域实际大小为100*100   
    11.  canvas.drawColor(Color.BLACK);  
               Paint paint=new Paint();
                canvas.scale(0.5f, 0.5f);
                canvas.save();
                canvas.clipRect(new Rect(100,100,200,200));//裁剪区域实际大小为50*50
                canvas.drawColor(Color.RED);
                canvas.restore();
                
                canvas.drawRect(new Rect(0,0,100,100), paint);//矩形实际大小为50*50
                
                canvas.clipRegion(new Region(new Rect(300,300,400,400)));//裁剪区域实际大小为100*100
                canvas.drawColor(Color.BLACK);



    可以看到,Canvas的变换 对clipRegion没有作用。

    ApiDemo中关于组合的例子:

    1. public class Clipping extends Activity {  
    2.   
    3.     @Override  
    4.     protected void onCreate(Bundle savedInstanceState) {  
    5.         super.onCreate(savedInstanceState);  
    6.         setContentView(new SampleView(this));  
    7.     }  
    8.   
    9.     private static class SampleView extends View {  
    10.         private Paint mPaint;  
    11.         private Path mPath;  
    12.   
    13.         public SampleView(Context context) {  
    14.             super(context);  
    15.             setFocusable(true);  
    16.   
    17.             mPaint = new Paint();  
    18.             mPaint.setAntiAlias(true);  
    19.             mPaint.setStrokeWidth(6);  
    20.             mPaint.setTextSize(16);  
    21.             mPaint.setTextAlign(Paint.Align.RIGHT);  
    22.   
    23.             mPath = new Path();  
    24.         }  
    25.   
    26.         private void drawScene(Canvas canvas) {  
    27.             canvas.clipRect(0, 0, 100, 100);  
    28.   
    29.             canvas.drawColor(Color.WHITE);  
    30.   
    31.             mPaint.setColor(Color.RED);  
    32.             canvas.drawLine(0, 0, 100, 100, mPaint);  
    33.   
    34.             mPaint.setColor(Color.GREEN);  
    35.             canvas.drawCircle(30, 70, 30, mPaint);  
    36.   
    37.             mPaint.setColor(Color.BLUE);  
    38.             canvas.drawText("Clipping", 100, 30, mPaint);  
    39.         }  
    40.   
    41.         @Override  
    42.         protected void onDraw(Canvas canvas) {  
    43.             canvas.drawColor(Color.GRAY);  
    44.   
    45.             canvas.save();  
    46.             canvas.translate(10, 10);  
    47.             drawScene(canvas);  
    48.             canvas.restore();  
    49.   
    50.             canvas.save();  
    51.             canvas.translate(160, 10);  
    52.             canvas.clipRect(10, 10, 90, 90);  
    53.             canvas.clipRect(30, 30, 70, 70, Region.Op.DIFFERENCE);  
    54.             drawScene(canvas);  
    55.             canvas.restore();  
    56.   
    57.             canvas.save();  
    58.             canvas.translate(10, 160);  
    59.             mPath.reset();  
    60.             canvas.clipPath(mPath); // makes the clip empty   
    61.             mPath.addCircle(50, 50, 50, Path.Direction.CCW);  
    62.             canvas.clipPath(mPath, Region.Op.REPLACE);  
    63.             drawScene(canvas);  
    64.             canvas.restore();  
    65.   
    66.             canvas.save();  
    67.             canvas.translate(160, 160);  
    68.             canvas.clipRect(0, 0, 60, 60);  
    69.             canvas.clipRect(40, 40, 100, 100, Region.Op.UNION);  
    70.             drawScene(canvas);  
    71.             canvas.restore();  
    72.   
    73.             canvas.save();  
    74.             canvas.translate(10, 310);  
    75.             canvas.clipRect(0, 0, 60, 60);  
    76.             canvas.clipRect(40, 40, 100, 100, Region.Op.XOR);  
    77.             drawScene(canvas);  
    78.             canvas.restore();  
    79.   
    80.             canvas.save();  
    81.             canvas.translate(160, 310);  
    82.             canvas.clipRect(0, 0, 60, 60);  
    83.             canvas.clipRect(40, 40, 100, 100, Region.Op.REVERSE_DIFFERENCE);  
    84.             drawScene(canvas);  
    85.             canvas.restore();  
    86.         }  
    87.           
    88.     }  
    89. }  
    public class Clipping extends Activity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(new SampleView(this));
        }
    
        private static class SampleView extends View {
            private Paint mPaint;
            private Path mPath;
    
            public SampleView(Context context) {
                super(context);
                setFocusable(true);
    
                mPaint = new Paint();
                mPaint.setAntiAlias(true);
                mPaint.setStrokeWidth(6);
                mPaint.setTextSize(16);
                mPaint.setTextAlign(Paint.Align.RIGHT);
    
                mPath = new Path();
            }
    
            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);
            }
    
            @Override
            protected void onDraw(Canvas canvas) {
                canvas.drawColor(Color.GRAY);
    
                canvas.save();
                canvas.translate(10, 10);
                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();
    
                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(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(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(160, 310);
                canvas.clipRect(0, 0, 60, 60);
                canvas.clipRect(40, 40, 100, 100, Region.Op.REVERSE_DIFFERENCE);
                drawScene(canvas);
                canvas.restore();
            }
            
        }
    }
    


    效果图:

    5、裁剪的一个小用处

    1. public class ClippingRegion extends Activity {  
    2.     @Override  
    3.     protected void onCreate(Bundle savedInstanceState) {  
    4.         super.onCreate(savedInstanceState);  
    5.         setContentView(new SampleView(this));  
    6.     }  
    7.   
    8.     private class SampleView extends View {  
    9.   
    10.         private Bitmap mBitmap;  
    11.         private int limitLength = 0;  
    12.         private int width;  
    13.         private int heigth;  
    14.         private static final int CLIP_HEIGHT = 30;  
    15.   
    16.         private boolean status = HIDE;//显示还是隐藏的状态,最开始为HIDE   
    17.         private static final boolean SHOW = true;//显示图片    
    18.         private static final boolean HIDE = false;//隐藏图片   
    19.   
    20.         public SampleView(Context context) {  
    21.             super(context);  
    22.             mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.image1);  
    23.             limitLength = width = mBitmap.getWidth();  
    24.             heigth = mBitmap.getHeight();  
    25.         }  
    26.   
    27.         @Override  
    28.         protected void onDraw(Canvas canvas) {  
    29.             Region region = new Region();  
    30.             int i = 0;  
    31.             while (i * CLIP_HEIGHT <= heigth) {//计算clip的区域   
    32.                 if (i % 2 == 0) {  
    33.                     region.union(new Rect(0, i * CLIP_HEIGHT, limitLength, (i + 1) * CLIP_HEIGHT));  
    34.                 } else {  
    35.                     region.union(new Rect(width - limitLength, i * CLIP_HEIGHT, width, (i + 1)  
    36.                             * CLIP_HEIGHT));  
    37.                 }  
    38.                 i++;  
    39.             }  
    40.   
    41.             canvas.clipRegion(region);  
    42.             canvas.drawBitmap(mBitmap, 0, 0, new Paint());  
    43.             if (status == HIDE) {//如果此时是隐藏   
    44.                 limitLength -= 5;  
    45.                 if(limitLength<=0)  
    46.                     status=SHOW;  
    47.             } else {//如果此时是显示   
    48.                 limitLength += 5;  
    49.                 if(limitLength>=width)  
    50.                     status=HIDE;  
    51.             }  
    52.   
    53.             invalidate();  
    54.         }  
    55.     }  
    56. }  
    public class ClippingRegion extends Activity {
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(new SampleView(this));
        }
    
        private class SampleView extends View {
    
            private Bitmap mBitmap;
            private int limitLength = 0;
            private int width;
            private int heigth;
            private static final int CLIP_HEIGHT = 30;
    
            private boolean status = HIDE;//显示还是隐藏的状态,最开始为HIDE
            private static final boolean SHOW = true;//显示图片 
            private static final boolean HIDE = false;//隐藏图片
    
            public SampleView(Context context) {
                super(context);
                mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.image1);
                limitLength = width = mBitmap.getWidth();
                heigth = mBitmap.getHeight();
            }
    
            @Override
            protected void onDraw(Canvas canvas) {
                Region region = new Region();
                int i = 0;
                while (i * CLIP_HEIGHT <= heigth) {//计算clip的区域
                    if (i % 2 == 0) {
                        region.union(new Rect(0, i * CLIP_HEIGHT, limitLength, (i + 1) * CLIP_HEIGHT));
                    } else {
                        region.union(new Rect(width - limitLength, i * CLIP_HEIGHT, width, (i + 1)
                                * CLIP_HEIGHT));
                    }
                    i++;
                }
    
                canvas.clipRegion(region);
                canvas.drawBitmap(mBitmap, 0, 0, new Paint());
                if (status == HIDE) {//如果此时是隐藏
                    limitLength -= 5;
                    if(limitLength<=0)
                        status=SHOW;
                } else {//如果此时是显示
                    limitLength += 5;
                    if(limitLength>=width)
                        status=HIDE;
                }
    
                invalidate();
            }
        }
    }


    效果就是一直这样交叉的隐藏和显示图片

  • 相关阅读:
    基础数据结构-线性表-顺序表的合并操作
    基础数据结构-线性表-顺序表
    后向引用(转)
    PHP正则表达式的快速学习方法
    c语言结构体中的冒号的用法
    C语言中可变参数的用法
    C语言snprintf函数
    C语言宏与单井号(#)和双井号(##)
    gcc 的visibility 使用
    __attribute__机制介绍 (转)
  • 原文地址:https://www.cnblogs.com/zhongle/p/4196123.html
Copyright © 2011-2022 走看看