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