1. //Android Matrix类实现镜像方法
2. publicvoiddrawRegion(Image image_src,
3.
4. intx_src, inty_src,
5.
6. intwidth, intheight,
7.
8. inttransform,
9.
10. intx_dest, inty_dest,
11.
12. intanchor){
13.
14. if((anchor&VCENTER) != 0){
15.
16. y_dest -= height/2;
17.
18. }elseif((anchor&BOTTOM) != 0){
19.
20. y_dest -= height;
21.
22. }
23.
24. if((anchor&RIGHT) != 0){
25.
26. x_dest -= width;
27.
28. }elseif((anchor&HCENTER) != 0){
29.
30. x_dest -= width/2;
31.
32. }
33.
34. Bitmap newMap = Bitmap.createBitmap(image_src.getBitmap(), x_src, y_src, width, height);
35.
36. Matrix mMatrix = newMatrix();
37.
38. Matrix temp = newMatrix();
39.
40. Matrix temp2 = newMatrix();
41.
42. float[] mirrorY = {
43.
44. -1, 0, 0,
45. 0, 1, 0,
46. 0, 0, 1
47.
48. };
49.
50. temp.setValues(mirrorY);
51.
52. switch(transform){
53.
54. caseSprite.TRANS_NONE:
55.
56. break;
57.
58. caseSprite.TRANS_ROT90:
59.
60. mMatrix.setRotate(90,width/2, height/2);
61.
62. break;
63.
64. caseSprite.TRANS_ROT180:
65.
66. mMatrix.setRotate(180,width/2, height/2);
67.
68. break;
69.
70. caseSprite.TRANS_ROT270:
71.
72. mMatrix.setRotate(270,width/2, height/2);
73.
74. break;
75.
76. caseSprite.TRANS_MIRROR:
77.
78. mMatrix.postConcat(temp);
79.
80. break;
81.
82. caseSprite.TRANS_MIRROR_ROT90:
83.
84. mMatrix.postConcat(temp);
85.
86. mMatrix.setRotate(90,width/2, height/2);
87.
88. break;
89.
90. caseSprite.TRANS_MIRROR_ROT180:
91.
92. mMatrix.postConcat(temp);
93.
94. mMatrix.setRotate(180,width/2, height/2);
95.
96. break;
97.
98. caseSprite.TRANS_MIRROR_ROT270:
99.
100. mMatrix.postConcat(temp);
101.
102. mMatrix.setRotate(270,width/2, height/2);
103.
104. break;
105.
106. }
107.
108. mMatrix.setTranslate(x_dest, y_dest);
109.
110. canvas.drawBitmap(newMap, mMatrix, mPaint);
111.
112. }
1. //图片Url保存为位图并进行缩放操作
2. //通过传入图片url获取位图方法
3. publicBitmap returnBitMap(String url) {
4. URL myFileUrl = null;
5. Bitmap bitmap = null;
6. try{
7. myFileUrl = newURL(url);
8. } catch(MalformedURLException e) {
9. e.printStackTrace();
10. }
11. try{
12. HttpURLConnection conn = (HttpURLConnection) myFileUrl
13. .openConnection();
14. conn.setDoInput(true);
15. conn.connect();
16. InputStream is = conn.getInputStream();
17. bitmap = BitmapFactory.decodeStream(is);
18. is.close();
19. } catch(IOException e) {
20. e.printStackTrace();
21. }
22. Log.v(tag, bitmap.toString());
23.
24. returnbitmap;
25. }
26. //通过传入位图,新的宽.高比进行位图的缩放操作
27. publicstaticDrawable resizeImage(Bitmap bitmap, intw, inth) {
28.
29. // load the origial Bitmap
30. Bitmap BitmapOrg = bitmap;
31.
32. intwidth = BitmapOrg.getWidth();
33. intheight = BitmapOrg.getHeight();
34. intnewWidth = w;
35. intnewHeight = h;
36.
37. Log.v(tag, String.valueOf(width));
38. Log.v(tag, String.valueOf(height));
39.
40. Log.v(tag, String.valueOf(newWidth));
41. Log.v(tag, String.valueOf(newHeight));
42.
43. // calculate the scale
44. floatscaleWidth = ((float) newWidth) / width;
45. floatscaleHeight = ((float) newHeight) / height;
46.
47. // create a matrix for the manipulation
48. Matrix matrix = newMatrix();
49. // resize the Bitmap
50. matrix.postScale(scaleWidth, scaleHeight);
51. // if you want to rotate the Bitmap
52. // matrix.postRotate(45);
53.
54. // recreate the new Bitmap
55. Bitmap resizedBitmap = Bitmap.createBitmap(BitmapOrg, 0, 0, width,
56. height, matrix, true);
57.
58. // make a Drawable from Bitmap to allow to set the Bitmap
59. // to the ImageView, ImageButton or what ever
60. returnnewBitmapDrawable(resizedBitmap);
61.
62. }
1. 1.图片加载方法,方便用户加载图片
2.
3. publicfinalBitmap CreatImage(Context context, intbitAdress) {
4. Bitmap bitmaptemp = null;
5. bitmaptemp = BitmapFactory.decodeResource(context.getResources(),
6. bitAdress);
7. returnbitmaptemp;
8. }
9. 2.图片平均分割方法,将大图平均分割为N行N列,方便用户使用
10.
11. publicfinalvoidcuteImage(Canvas g, Paint paint, Bitmap imgBit, intx,
12. inty, intw, inth, intline, introw) {
13. g.clipRect(x, y, x + w, h + y);
14. g.drawBitmap(imgBit, x – line * w, y – row * h, paint);
15. g.restore();
16. }
17. 3.图片缩放,对当前图片进行缩放处理
18.
19. publicBitmap zoomImage(Bitmap bgimage, intnewWidth, intnewHeight) {
20. // 获取这个图片的宽和高
21. intwidth = bgimage.getWidth();
22. intheight = bgimage.getHeight();
23. // 创建操作图片用的matrix对象
24. Matrix matrix = newMatrix();
25. // 计算缩放率,新尺寸除原始尺寸
26. floatscaleWidth = ((float) newWidth) / width;
27. floatscaleHeight = ((float) newHeight) / height;
28. // 缩放图片动作
29. matrix.postScale(scaleWidth, scaleHeight);
30. Bitmap bitmap = Bitmap.createBitmap(bgimage, 0, 0, width, height,
31. matrix, true);
32. returnbitmap;
33. }
34. 4.绘制带有边框的文字,一般在游戏中起文字的美化作用
35.
36. publicvoiddrawText(String strMsg, Canvas g, Paint paint, intsetx,
37. intsety, intfg, intbg) {
38. paint.setColor(bg);
39. g.drawText(strMsg, setx + 1, sety, paint);
40. g.drawText(strMsg, setx, sety – 1, paint);
41. g.drawText(strMsg, setx, sety + 1, paint);
42. g.drawText(strMsg, setx – 1, sety, paint);
43. paint.setColor(fg);
44. g.drawText(strMsg, setx, sety, paint);
45. g.restore();
46. }
47. 5.Android 图片透明度处理代码
48.
49. publicstaticBitmap setAlpha(Bitmap sourceImg, intnumber) {
50. int[] argb = newint[sourceImg.getWidth() * sourceImg.getHeight()];
51. sourceImg.getPixels(argb, 0, sourceImg.getWidth(), 0, 0,sourceImg.getWidth(), sourceImg.getHeight());// 获得图片的ARGB值
52. number = number * 255/ 100;
53. for(inti = 0; i < argb.length; i++) {
54. argb = (number << 24) | (argb & 0×00FFFFFF);// 修改最高2位的值
55. }
56. sourceImg = Bitmap.createBitmap(argb, sourceImg.getWidth(), sourceImg.getHeight(), Config.ARGB_8888);
57. returnsourceImg;
58. }
59. 6.图片翻转
60. Resources res = this.getContext().getResources();
61. img = BitmapFactory.decodeResource(res, R.drawable.slogo);
62. Matrix matrix = newMatrix();
63. matrix.postRotate(90);
64. intwidth = img.getWidth();
65. intheight = img.getHeight();
66. r_img = Bitmap.createBitmap(img, 0, 0, width, height, matrix, true);
1. importandroid.graphics.Bitmap;
2. importandroid.graphics.Canvas;
3. importandroid.graphics.LinearGradient;
4. importandroid.graphics.Matrix;
5. importandroid.graphics.Paint;
6. importandroid.graphics.PixelFormat;
7. importandroid.graphics.PorterDuffXfermode;
8. importandroid.graphics.Rect;
9. importandroid.graphics.RectF;
10. importandroid.graphics.Bitmap.Config;
11. importandroid.graphics.PorterDuff.Mode;
12. importandroid.graphics.Shader.TileMode;
13. importandroid.graphics.drawable.Drawable;
14.
15. publicclassImageUtil {
16.
17.
18. publicstaticBitmap zoomBitmap(Bitmap bitmap, intw, inth) {
19. intwidth = bitmap.getWidth();
20. intheight = bitmap.getHeight();
21. Matrix matrix = newMatrix();
22. floatscaleWidht = ((float) w / width);
23. floatscaleHeight = ((float) h / height);
24. matrix.postScale(scaleWidht, scaleHeight);
25. Bitmap newbmp = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);
26. returnnewbmp;
27. }
28.
29.
30. publicstaticBitmap drawableToBitmap(Drawable drawable) {
31. intwidth = drawable.getIntrinsicWidth();
32. intheight = drawable.getIntrinsicHeight();
33. Bitmap bitmap = Bitmap.createBitmap(width, height, drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565);
34. Canvas canvas = newCanvas(bitmap);
35. drawable.setBounds(0, 0, width, height);
36. drawable.draw(canvas);
37. returnbitmap;
38.
39. }
40.
41.
42. publicstaticBitmap getRoundedCornerBitmap(Bitmap bitmap, floatroundPx) {
43.
44. Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888);
45. Canvas canvas = newCanvas(output);
46.
47. finalintcolor = 0xff424242;
48. finalPaint paint = newPaint();
49. finalRect rect = newRect(0, 0, bitmap.getWidth(), bitmap.getHeight());
50. finalRectF rectF = newRectF(rect);
51.
52. paint.setAntiAlias(true);
53. canvas.drawARGB(0, 0, 0, 0);
54. paint.setColor(color);
55. canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
56.
57. paint.setXfermode(newPorterDuffXfermode(Mode.SRC_IN));
58. canvas.drawBitmap(bitmap, rect, rect, paint);
59.
60. returnoutput;
61. }
62.
63.
64. publicstaticBitmap createReflectionImageWithOrigin(Bitmap bitmap) {
65. finalintreflectionGap = 4;
66. intwidth = bitmap.getWidth();
67. intheight = bitmap.getHeight();
68.
69. Matrix matrix = newMatrix();
70. matrix.preScale(1, -1);
71.
72. Bitmap reflectionImage = Bitmap.createBitmap(bitmap, 0, height / 2, width, height / 2, matrix, false);
73.
74. Bitmap bitmapWithReflection = Bitmap.createBitmap(width, (height + height / 2), Config.ARGB_8888);
75.
76. Canvas canvas = newCanvas(bitmapWithReflection);
77. canvas.drawBitmap(bitmap, 0, 0, null);
78. Paint deafalutPaint = newPaint();
79. canvas.drawRect(0, height, width, height + reflectionGap, deafalutPaint);
80.
81. canvas.drawBitmap(reflectionImage, 0, height + reflectionGap, null);
82.
83. Paint paint = newPaint();
84. LinearGradient shader = newLinearGradient(0, bitmap.getHeight(), 0, bitmapWithReflection.getHeight() + reflectionGap, 0x70ffffff, 0x00ffffff, TileMode.CLAMP);
85. paint.setShader(shader);
86. // Set the Transfer mode to be porter duff and destination in
87. paint.setXfermode(newPorterDuffXfermode(Mode.DST_IN));
88. // Draw a rectangle using the paint with our linear gradient
89. canvas.drawRect(0, height, width, bitmapWithReflection.getHeight() + reflectionGap, paint);
90. returnbitmapWithReflection;
91. }
92. }