`

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

 
阅读更多

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

一、对称变换

1. 对称于Y轴

其变换矩阵:

其变换为:

2. 对称于X轴:

3. 对称于原点O:

4.对称于直线y=x:

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

二、drawRegion方法的实现

Ruby代码收藏代码
  1. publicvoiddrawRegion(Imageimage_src,
  2. intx_src,inty_src,
  3. intwidth,intheight,
  4. inttransform,
  5. intx_dest,inty_dest,
  6. intanchor){
  7. if((anchor&VCENTER)!=0){
  8. y_dest-=height/2;
  9. }elseif((anchor&BOTTOM)!=0){
  10. y_dest-=height;
  11. }
  12. if((anchor&RIGHT)!=0){
  13. x_dest-=width;
  14. }elseif((anchor&HCENTER)!=0){
  15. x_dest-=width/2;
  16. }
  17. BitmapnewMap=Bitmap.createBitmap(image_src.getBitmap(),x_src,y_src,width,height);
  18. MatrixmMatrix=newMatrix();
  19. Matrixtemp=newMatrix();
  20. Matrixtemp2=newMatrix();
  21. float[]mirrorY={
  22. -1,0,0,
  23. 0,1,0,
  24. 0,0,1
  25. };
  26. temp.setValues(mirrorY);
  27. switch(transform){
  28. caseSprite.TRANS_NONE:
  29. break;
  30. caseSprite.TRANS_ROT90:
  31. mMatrix.setRotate(90,width/2,height/2);
  32. break;
  33. caseSprite.TRANS_ROT180:
  34. mMatrix.setRotate(180,width/2,height/2);
  35. break;
  36. caseSprite.TRANS_ROT270:
  37. mMatrix.setRotate(270,width/2,height/2);
  38. break;
  39. caseSprite.TRANS_MIRROR:
  40. mMatrix.postConcat(temp);
  41. break;
  42. caseSprite.TRANS_MIRROR_ROT90:
  43. mMatrix.postConcat(temp);
  44. mMatrix.setRotate(90,width/2,height/2);
  45. break;
  46. caseSprite.TRANS_MIRROR_ROT180:
  47. mMatrix.postConcat(temp);
  48. mMatrix.setRotate(180,width/2,height/2);
  49. break;
  50. caseSprite.TRANS_MIRROR_ROT270:
  51. mMatrix.postConcat(temp);
  52. mMatrix.setRotate(270,width/2,height/2);
  53. break;
  54. }
  55. mMatrix.setTranslate(x_dest,y_dest);
  56. canvas.drawBitmap(newMap,mMatrix,mPaint);
  57. }

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

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics