Android Matrix类实现J2ME的drawRegion的镜像方法

先说说drawRegion方法:

Java代码   收藏代码
  1. public void drawRegion(Image src,//图像文件  
  2.                        int x_src,//图像的起始点X坐标  
  3.                        int y_src,//图像的起始点Y坐标  
  4.                        int width,//要画图像的宽度  
  5.                        int height,//要画图像的高度  
  6.                        int transform,//旋转参数  
  7.                        int x_dest,//目标X坐标(也就是你要把图像画到那个Canvas上的哪个位置)  
  8.                        int y_dest,//目标Y坐标  
  9.                        int anchor)//锚点  

 简单的说就是从原图截取一块内容并且可以旋转画在指定的位置。

从网上找了一个方法用matrix实现了,具体代码如下:

http://novembre.iteye.com/blog/636175 写道
     在用Android实现J2ME的drawRegion方法时,发现网上介绍的镜像翻转都是用像素数组行变列实现的,其实这还是j2me式的实现方法,Android中有Matrix类,如果学过计算机图形学,只要按其原理,进行矩阵变换即可。

 

 一、对称变换

 

   

 

1. 对称于Y轴

    其变换矩阵:

        

   

    其变换为:

    

 

2. 对称于X轴:

      

 

3. 对称于原点O:

    

 

4. 对称于直线y=x: 

    

 

5. 对称于直线y=-x:

    

 

二、drawRegion方法的实现

 

Ruby代码   收藏代码
  1. public void drawRegion(Image image_src,   
  2.                            int x_src, int y_src,  
  3.                            int width, int height,   
  4.                            int transform,  
  5.                            int x_dest, int y_dest,  
  6.                            int anchor){  
  7.           
  8.         if((anchor&VCENTER) != 0){  
  9.             y_dest -= height/2;  
  10.         }else if((anchor&BOTTOM) != 0){  
  11.             y_dest -= height;  
  12.         }  
  13.         if((anchor&RIGHT) != 0){  
  14.             x_dest -= width;  
  15.         }else if((anchor&HCENTER) != 0){  
  16.             x_dest -= width/2;  
  17.         }  
  18.           
  19.         Bitmap newMap = Bitmap.createBitmap(image_src.getBitmap(), x_src, y_src, width, height);  
  20.                   
  21.         Matrix mMatrix = new Matrix();  
  22.         Matrix temp = new Matrix();  
  23.         Matrix temp2 = new Matrix();  
  24.           
  25.         float[] mirrorY = {  
  26.                 -1, 0, 0,  
  27.                 0, 1, 0,  
  28.                 0, 0, 1  
  29.         };  
  30.         temp.setValues(mirrorY);  
  31.           
  32.         switch(transform){  
  33.         case Sprite.TRANS_NONE:  
  34.               
  35.             break;  
  36.   
  37.         case Sprite.TRANS_ROT90:  
  38.             mMatrix.setRotate(90,width/2, height/2);  
  39.             break;  
  40.   
  41.         case Sprite.TRANS_ROT180:  
  42.             mMatrix.setRotate(180,width/2, height/2);  
  43.             break;  
  44.   
  45.         case Sprite.TRANS_ROT270:  
  46.             mMatrix.setRotate(270,width/2, height/2);  
  47.             break;  
  48.   
  49.         case Sprite.TRANS_MIRROR:  
  50.             mMatrix.postConcat(temp);  
  51.             break;  
  52.   
  53.         case Sprite.TRANS_MIRROR_ROT90:  
  54.             mMatrix.postConcat(temp);  
  55.             mMatrix.setRotate(90,width/2, height/2);  
  56.             break;  
  57.   
  58.         case Sprite.TRANS_MIRROR_ROT180:  
  59.             mMatrix.postConcat(temp);  
  60.             mMatrix.setRotate(180,width/2, height/2);  
  61.             break;  
  62.   
  63.         case Sprite.TRANS_MIRROR_ROT270:  
  64.             mMatrix.postConcat(temp);  
  65.             mMatrix.setRotate(270,width/2, height/2);  
  66.             break;  
  67.   
  68.         }  
  69.                       
  70.         mMatrix.setTranslate(x_dest, y_dest);  
  71.           
  72.         canvas.drawBitmap(newMap, mMatrix, mPaint);  
  73.           
  74.           
  75.     }  

    

   

    利用Matrix类,不止可实现对称变换,还可以实现其它的几何变换,包括组合变换。附件中是从网上找到的关于图形变换原理的ppt,希望对大家有帮助。

 

 然而事情到这里并没有结束,我发现对于我来说这个实现方法有一个问题。在往下说之前我先说一下我为什么要在android上实现drawRegion方法,我在做一个动画使用到了动画编辑器spritex,所以必须使用这个编辑器提供的类SpriteX.java,这个类只使用了j2me中的canvas里的基本绘图函数(当然也包括了drawRegion),所以只要你实现了j2me的canvas方法就可以用SpriteX.java,SpriteX.java的paint方法中使用了drawRegion所以我们也必须要实现它。

 

现在在说说我发现的问题那就是我上面找到的实现方法是使用“Bitmap newMap = Bitmap.createBitmap(image_src.getBitmap(), x_src, y_src, width, height);”来截取一块图像。它create了一个张图片。在android中每次创建图片系统都会GC一次!

 

现在问题出来了 我的程序移植在刷新--》SpriteX.java的paint方法每次都调用drawRegion--》drawRegion方法每次都createBitmap--》系统每次都GC

 

这样下去程序肯定会完蛋的,所以我修改了这个类,使用另一种方式来实现了它,没有create。

 说明写在程序中了

 

Java代码   收藏代码
  1. public void drawRegion(Image image_src,   
  2.            int x_src, int y_src,  
  3.            int width, int height,   
  4.            int transform,  
  5.            int x_dest, int y_dest,  
  6.            int anchor) {  
  7.   
  8.     if ((anchor & VCENTER) != 0) {  
  9.         y_dest -= height / 2;  
  10.     } else if ((anchor & BOTTOM) != 0) {  
  11.         y_dest -= height;  
  12.     }  
  13.     if ((anchor & RIGHT) != 0) {  
  14.         x_dest -= width;  
  15.     } else if ((anchor & HCENTER) != 0) {  
  16.         x_dest -= width / 2;  
  17.     }  
  18.   
  19.     //Bitmap newMap = Bitmap.createBitmap(image_src.getImage(), x_src, y_src,width, height);  
  20.     /* 
  21.      * 这里没有使用create 而是用canvas的clipRect方法来实现扣图的效果 
  22.      * 使用了save为后面恢复剪裁区用 
  23.      */  
  24.     g.save();  
  25.     g.clipRect(x_dest, y_dest, x_dest+width, y_dest+height);  
  26.     Matrix mMatrix = new Matrix();  
  27.     Matrix temp = new Matrix();  
  28.   
  29.     float[] mirrorY = { -100010001 };  
  30.     temp.setValues(mirrorY);  
  31.   
  32.     switch (transform) {  
  33.     case SpriteX.TRANS_NONE:  
  34.   
  35.         break;  
  36.   
  37.     case SpriteX.TRANS_ROT90:  
  38.         mMatrix.setRotate(90, width / 2, height / 2);  
  39.         break;  
  40.   
  41.     case SpriteX.TRANS_ROT180:  
  42.         mMatrix.setRotate(180, width / 2, height / 2);  
  43.         break;  
  44.   
  45.     case SpriteX.TRANS_ROT270:  
  46.         mMatrix.setRotate(270, width / 2, height / 2);  
  47.         break;  
  48.   
  49.     case SpriteX.TRANS_MIRROR:  
  50.         mMatrix.postConcat(temp);  
  51.         break;  
  52.   
  53.     case SpriteX.TRANS_MIRROR_ROT90:  
  54.         mMatrix.postConcat(temp);  
  55.         mMatrix.setRotate(90, width / 2, height / 2);  
  56.         break;  
  57.   
  58.     case SpriteX.TRANS_MIRROR_ROT180:  
  59.         mMatrix.postConcat(temp);  
  60.         mMatrix.setRotate(180, width / 2, height / 2);  
  61.         break;  
  62.   
  63.     case SpriteX.TRANS_MIRROR_ROT270:  
  64.         mMatrix.postConcat(temp);  
  65.         mMatrix.setRotate(270, width / 2, height / 2);  
  66.         break;  
  67.     }  
  68.       
  69.     /* 
  70.      * 这里的偏移要注意减去x_src和y_src 
  71.      * 并且调用restore()恢复剪裁区 
  72.      */  
  73.     mMatrix.setTranslate(x_dest-x_src, y_dest-y_src);  
  74.     g.drawBitmap(image_src.getImage(), mMatrix, p);  
  75.     g.restore();  
  76. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值