现在的位置: 首页 > 综合 > 正文

Android 图像处理(类型转换,比例缩放,倒影,圆角)

2013年03月12日 ⁄ 综合 ⁄ 共 3308字 ⁄ 字号 评论关闭

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

抱歉!评论已关闭.