android图片处理方法2

Java代码  收藏代码
  1. //Android Matrix类实现镜像方法  
  2. public void drawRegion(Image image_src,  
  3.   
  4. int x_src, int y_src,  
  5.   
  6. int width, int height,  
  7.   
  8. int transform,  
  9.   
  10. int x_dest, int y_dest,  
  11.   
  12. int anchor){  
  13.   
  14. if((anchor&VCENTER) != 0){  
  15.   
  16. y_dest -= height/2 
  17.   
  18. }else if((anchor&BOTTOM) != 0){  
  19.   
  20. y_dest -= height;  
  21.   
  22.  
  23.   
  24. if((anchor&RIGHT) != 0){  
  25.   
  26. x_dest -= width;  
  27.   
  28. }else if((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 new Matrix();  
  37.   
  38. Matrix temp new Matrix();  
  39.   
  40. Matrix temp2 new Matrix();  
  41.   
  42. float[] mirrorY  
  43.   
  44. -100 
  45. 010 
  46. 001  
  47.   
  48. };  
  49.   
  50. temp.setValues(mirrorY);  
  51.   
  52. switch(transform){  
  53.   
  54. case Sprite.TRANS_NONE:  
  55.   
  56. break 
  57.   
  58. case Sprite.TRANS_ROT90:  
  59.   
  60. mMatrix.setRotate(90,width/2height/2);  
  61.   
  62. break 
  63.   
  64. case Sprite.TRANS_ROT180:  
  65.   
  66. mMatrix.setRotate(180,width/2height/2);  
  67.   
  68. break 
  69.   
  70. case Sprite.TRANS_ROT270:  
  71.   
  72. mMatrix.setRotate(270,width/2height/2);  
  73.   
  74. break 
  75.   
  76. case Sprite.TRANS_MIRROR:  
  77.   
  78. mMatrix.postConcat(temp);  
  79.   
  80. break 
  81.   
  82. case Sprite.TRANS_MIRROR_ROT90:  
  83.   
  84. mMatrix.postConcat(temp);  
  85.   
  86. mMatrix.setRotate(90,width/2height/2);  
  87.   
  88. break 
  89.   
  90. case Sprite.TRANS_MIRROR_ROT180:  
  91.   
  92. mMatrix.postConcat(temp);  
  93.   
  94. mMatrix.setRotate(180,width/2height/2);  
  95.   
  96. break 
  97.   
  98. case Sprite.TRANS_MIRROR_ROT270:  
  99.   
  100. mMatrix.postConcat(temp);  
  101.   
  102. mMatrix.setRotate(270,width/2height/2);  
  103.   
  104. break 
  105.   
  106.  
  107.   
  108. mMatrix.setTranslate(x_dest, y_dest);  
  109.   
  110. canvas.drawBitmap(newMap, mMatrix, mPaint);  
  111.   
  112.  


Java代码    收藏代码
  1. //图片Url保存为位图并进行缩放操作  
  2. //通过传入图片url获取位图方法  
  3. public Bitmap returnBitMap(String url)  
  4.         URL myFileUrl null 
  5.         Bitmap bitmap null 
  6.         try  
  7.             myFileUrl new URL(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.         return bitmap;  
  25.      
  26. //通过传入位图,新的宽.高比进行位图的缩放操作  
  27. public static Drawable resizeImage(Bitmap bitmap, int w, int h)  
  28.   
  29.         // load the origial Bitmap  
  30.         Bitmap BitmapOrg bitmap;  
  31.   
  32.         int width BitmapOrg.getWidth();  
  33.         int height BitmapOrg.getHeight();  
  34.         int newWidth w;  
  35.         int newHeight 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.         float scaleWidth ((floatnewWidth) width;  
  45.         float scaleHeight ((floatnewHeight) height;  
  46.   
  47.         // create matrix for the manipulation  
  48.         Matrix matrix new Matrix();  
  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, 00width,  
  56.                 height, matrix, true);  
  57.   
  58.         // make Drawable from Bitmap to allow to set the Bitmap  
  59.         // to the ImageView, ImageButton or what ever  
  60.         return new BitmapDrawable(resizedBitmap);  
  61.   
  62.      


Java代码    收藏代码
  1. 1.图片加载方法,方便用户加载图片  
  2.   
  3. public final Bitmap CreatImage(Context context, int bitAdress)  
  4. Bitmap bitmaptemp null 
  5. bitmaptemp BitmapFactory.decodeResource(context.getResources(),  
  6. bitAdress);  
  7. return bitmaptemp;  
  8.  
  9. 2.图片平均分割方法,将大图平均分割为N行N列,方便用户使用  
  10.   
  11. public final void cuteImage(Canvas g, Paint paint, Bitmap imgBit, int x,  
  12. int y, int w, int h, int line, int row)  
  13. g.clipRect(x, y, w, y);  
  14. g.drawBitmap(imgBit, – line w, – row h, paint);  
  15. g.restore();  
  16.  
  17. 3.图片缩放,对当前图片进行缩放处理  
  18.   
  19. public Bitmap zoomImage(Bitmap bgimage, int newWidth, int newHeight)  
  20. // 获取这个图片的宽和高  
  21. int width bgimage.getWidth();  
  22. int height bgimage.getHeight();  
  23. // 创建操作图片用的matrix对象  
  24. Matrix matrix new Matrix();  
  25. // 计算缩放率,新尺寸除原始尺寸  
  26. float scaleWidth ((floatnewWidth) width;  
  27. float scaleHeight ((floatnewHeight) height;  
  28. // 缩放图片动作  
  29. matrix.postScale(scaleWidth, scaleHeight);  
  30. Bitmap bitmap Bitmap.createBitmap(bgimage, 00width, height,  
  31. matrix, true);  
  32. return bitmap;  
  33.  
  34. 4.绘制带有边框的文字,一般在游戏中起文字的美化作用  
  35.   
  36. public void drawText(String strMsg, Canvas g, Paint paint, int setx,  
  37. int sety, int fg, int bg)  
  38. paint.setColor(bg);  
  39. g.drawText(strMsg, setx 1sety, paint);  
  40. g.drawText(strMsg, setx, sety – 1paint);  
  41. g.drawText(strMsg, setx, sety 1paint);  
  42. g.drawText(strMsg, setx – 1sety, paint);  
  43. paint.setColor(fg);  
  44. g.drawText(strMsg, setx, sety, paint);  
  45. g.restore();  
  46.  
  47. 5.Android 图片透明度处理代码  
  48.   
  49. public static Bitmap setAlpha(Bitmap sourceImg, int number)  
  50. int[] argb new int[sourceImg.getWidth() sourceImg.getHeight()];  
  51. sourceImg.getPixels(argb, 0sourceImg.getWidth(), 00,sourceImg.getWidth(), sourceImg.getHeight());// 获得图片的ARGB值  
  52. number number 255 100 
  53. for (int 0argb.length; i++)  
  54. argb (number << 24(argb 0×00FFFFFF);// 修改最高2位的值  
  55.  
  56. sourceImg Bitmap.createBitmap(argb, sourceImg.getWidth(), sourceImg.getHeight(), Config.ARGB_8888);  
  57. return sourceImg;  
  58.  
  59. 6.图片翻转  
  60. Resources res this.getContext().getResources();  
  61. img BitmapFactory.decodeResource(res, R.drawable.slogo);  
  62. Matrix matrix new Matrix();  
  63. matrix.postRotate(90);          
  64. int width img.getWidth();  
  65. int height img.getHeight();  
  66. r_img Bitmap.createBitmap(img, 00width, height, matrix, true);  

Java代码    收藏代码
  1. import android.graphics.Bitmap;  
  2. import android.graphics.Canvas;  
  3. import android.graphics.LinearGradient;  
  4. import android.graphics.Matrix;  
  5. import android.graphics.Paint;  
  6. import android.graphics.PixelFormat;  
  7. import android.graphics.PorterDuffXfermode;  
  8. import android.graphics.Rect;  
  9. import android.graphics.RectF;  
  10. import android.graphics.Bitmap.Config;  
  11. import android.graphics.PorterDuff.Mode;  
  12. import android.graphics.Shader.TileMode;  
  13. import android.graphics.drawable.Drawable;  
  14.   
  15. public class ImageUtil  
  16.   
  17.   
  18. public static Bitmap zoomBitmap(Bitmap bitmap, int w, int h)  
  19.    int width bitmap.getWidth();  
  20.    int height bitmap.getHeight();  
  21.    Matrix matrix new Matrix();  
  22.    float scaleWidht ((floatwidth);  
  23.    float scaleHeight ((floatheight);  
  24.    matrix.postScale(scaleWidht, scaleHeight);  
  25.    Bitmap newbmp Bitmap.createBitmap(bitmap, 00width, height, matrix, true);  
  26.    return newbmp;  
  27.  
  28.   
  29.   
  30. public static Bitmap drawableToBitmap(Drawable drawable)  
  31.    int width drawable.getIntrinsicWidth();  
  32.    int height drawable.getIntrinsicHeight();  
  33.    Bitmap bitmap Bitmap.createBitmap(width, height, drawable.getOpacity() != PixelFormat.OPAQUE Bitmap.Config.ARGB_8888 Bitmap.Config.RGB_565);  
  34.    Canvas canvas new Canvas(bitmap);  
  35.    drawable.setBounds(00width, height);  
  36.    drawable.draw(canvas);  
  37.    return bitmap;  
  38.   
  39.  
  40.   
  41.   
  42. public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, float roundPx)  
  43.   
  44.    Bitmap output Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888);  
  45.    Canvas canvas new Canvas(output);  
  46.   
  47.    final int color 0xff424242 
  48.    final Paint paint new Paint();  
  49.    final Rect rect new Rect(00bitmap.getWidth(), bitmap.getHeight());  
  50.    final RectF rectF new RectF(rect);  
  51.   
  52.    paint.setAntiAlias(true);  
  53.    canvas.drawARGB(0000);  
  54.    paint.setColor(color);  
  55.    canvas.drawRoundRect(rectF, roundPx, roundPx, paint);  
  56.   
  57.    paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));  
  58.    canvas.drawBitmap(bitmap, rect, rect, paint);  
  59.   
  60.    return output;  
  61.  
  62.   
  63.   
  64. public static Bitmap createReflectionImageWithOrigin(Bitmap bitmap)  
  65.    final int reflectionGap 4 
  66.    int width bitmap.getWidth();  
  67.    int height bitmap.getHeight();  
  68.   
  69.    Matrix matrix new Matrix();  
  70.    matrix.preScale(1-1);  
  71.   
  72.    Bitmap reflectionImage Bitmap.createBitmap(bitmap, 0height 2width, height 2matrix, false);  
  73.   
  74.    Bitmap bitmapWithReflection Bitmap.createBitmap(width, (height height 2), Config.ARGB_8888);  
  75.   
  76.    Canvas canvas new Canvas(bitmapWithReflection);  
  77.    canvas.drawBitmap(bitmap, 00null);  
  78.    Paint deafalutPaint new Paint();  
  79.    canvas.drawRect(0height, width, height reflectionGap, deafalutPaint);  
  80.   
  81.    canvas.drawBitmap(reflectionImage, 0height reflectionGap, null);  
  82.   
  83.    Paint paint new Paint();  
  84.    LinearGradient shader new LinearGradient(0bitmap.getHeight(), 0bitmapWithReflection.getHeight() reflectionGap, 0x70ffffff0x00ffffffTileMode.CLAMP);  
  85.    paint.setShader(shader);  
  86.    // Set the Transfer mode to be porter duff and destination in  
  87.    paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));  
  88.    // Draw rectangle using the paint with our linear gradient  
  89.    canvas.drawRect(0height, width, bitmapWithReflection.getHeight() reflectionGap, paint);  
  90.    return bitmapWithReflection;  
  91.  
  92.  

Java代码 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值