`

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

阅读更多

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

 

 一、对称变换

 

   

 

1. 对称于Y轴

    其变换矩阵:

        

   

    其变换为:

    

 

2. 对称于X轴:

      

 

3. 对称于原点O:

   

 

4. 对称于直线y=x: 

   

 

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

    

 

二、drawRegion方法的实现

 

public void drawRegion(Image image_src, 
            			   int x_src, int y_src,
            			   int width, int height, 
            			   int transform,
            			   int x_dest, int y_dest,
            			   int anchor){
		
		if((anchor&VCENTER) != 0){
			y_dest -= height/2;
		}else if((anchor&BOTTOM) != 0){
			y_dest -= height;
		}
		if((anchor&RIGHT) != 0){
			x_dest -= width;
		}else if((anchor&HCENTER) != 0){
			x_dest -= width/2;
		}
		
		Bitmap newMap = Bitmap.createBitmap(image_src.getBitmap(), x_src, y_src, width, height);
				
		Matrix mMatrix = new Matrix();
		Matrix temp = new Matrix();
		Matrix temp2 = new Matrix();
		
		float[] mirrorY = {
				-1, 0, 0,
				0, 1, 0,
				0, 0, 1
		};
		temp.setValues(mirrorY);
		
		switch(transform){
		case Sprite.TRANS_NONE:
			
			break;

		case Sprite.TRANS_ROT90:
			mMatrix.setRotate(90,width/2, height/2);
			break;

		case Sprite.TRANS_ROT180:
			mMatrix.setRotate(180,width/2, height/2);
			break;

		case Sprite.TRANS_ROT270:
			mMatrix.setRotate(270,width/2, height/2);
			break;

		case Sprite.TRANS_MIRROR:
			mMatrix.postConcat(temp);
			break;

		case Sprite.TRANS_MIRROR_ROT90:
			mMatrix.postConcat(temp);
			mMatrix.setRotate(90,width/2, height/2);
			break;

		case Sprite.TRANS_MIRROR_ROT180:
			mMatrix.postConcat(temp);
			mMatrix.setRotate(180,width/2, height/2);
			break;

		case Sprite.TRANS_MIRROR_ROT270:
			mMatrix.postConcat(temp);
			mMatrix.setRotate(270,width/2, height/2);
			break;

		}
					
		mMatrix.setTranslate(x_dest, y_dest);
		
		canvas.drawBitmap(newMap, mMatrix, mPaint);
		
		
	}

    

   

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

 

 

 

 

3
0
分享到:
评论
4 楼 kingDroid 2012-08-21  
代码倒数第2行:mMatrix.setTranslate(x_dest, y_dest);
改为:mMatrix.postTranslate(x_dest, y_dest);
3 楼 shuiquan8830 2011-07-28  
来看看----
2 楼 hydrogen2008 2010-08-26  
这个不对吧,楼主能不能放一个正确的
1 楼 seven060601 2010-08-23  
试了一下,没有起作用,改了一下,应该用修正的mMatrix创建newMap,再画就ok了。

相关推荐

Global site tag (gtag.js) - Google Analytics