life02

  C++博客 :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  197 随笔 :: 3 文章 :: 37 评论 :: 0 Trackbacks

http://06peng.com/read.php/52.htm

整理了一下目前Android开发中图片的各种处理方法:

Java代码
        
  1.    /** 
  2.     
  3.      * 使头像变灰 
  4.     
  5.      * @param drawable 
  6.     
  7.      */  
  8.     
  9.     public static void porBecomeGrey(ImageView imageView, Drawable drawable) {  
  10.     
  11.         drawable.mutate();      
  12.     
  13.         ColorMatrix cm = new ColorMatrix();      
  14.     
  15.         cm.setSaturation(0);      
  16.     
  17.         ColorMatrixColorFilter cf = new ColorMatrixColorFilter(cm);      
  18.     
  19.         drawable.setColorFilter(cf);   
  20.     
  21.         imageView.setImageDrawable(drawable);  
  22.     
  23.     }  

 

Java 代码复制内容到剪贴板
        
  1. Drawable drawable = new FastBitmapDrawable(bitmap);    
Java 代码复制内容到剪贴板
        
  1. public byte[] getBitmapByte(Bitmap bitmap){    
  2.     
  3.     ByteArrayOutputStream out = new ByteArrayOutputStream();    
  4.     
  5.     bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);    
  6.     
  7.     try {    
  8.     
  9.         out.flush();    
  10.     
  11.         out.close();    
  12.     
  13.     } catch (IOException e) {    
  14.     
  15.         e.printStackTrace();    
  16.     
  17.     }    
  18.     
  19.     return out.toByteArray();    
  20.     
  21. }    
  22.     
  23.     
  24.     
  25.     
  26.     
  27. public Bitmap getBitmapFromByte(byte[] temp){    
  28.     
  29.     if(temp != null){    
  30.     
  31.         Bitmap bitmap = BitmapFactory.decodeByteArray(temp, 0, temp.length);    
  32.     
  33.         return bitmap;    
  34.     
  35.     }else{    
  36.     
  37.         return null;    
  38.     
  39.     }    
  40.     
  41. }    
Java 代码复制内容到剪贴板
        
  1. /**
  2.     
  3.      * 将Drawable转化为Bitmap
  4.     
  5.      * @param drawable
  6.     
  7.      * @return
  8.     
  9.      */  
  10.     
  11.     public static Bitmap drawableToBitmap(Drawable drawable) {  
  12.     
  13.         int width = drawable.getIntrinsicWidth();  
  14.     
  15.         int height = drawable.getIntrinsicHeight();  
  16.     
  17.         Bitmap bitmap = Bitmap.createBitmap(width, height, drawable  
  18.     
  19.                 .getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888  
  20.     
  21.                 : Bitmap.Config.RGB_565);  
  22.     
  23.         Canvas canvas = new Canvas(bitmap);  
  24.     
  25.         drawable.setBounds(0, 0, width, height);  
  26.     
  27.         drawable.draw(canvas);  
  28.     
  29.         return bitmap;  
  30.     
  31.     }  
Java 代码复制内容到剪贴板
        
  1. /**
  2.     
  3.     * 获取图片的倒影
  4.     
  5.     * @param bitmap
  6.     
  7.     * @return
  8.     
  9.     */  
  10.     
  11.     public static Bitmap createReflectionImageWithOrigin(Bitmap bitmap) {  
  12.     
  13.         final int reflectionGap = 4;  
  14.     
  15.         int width = bitmap.getWidth();  
  16.     
  17.         int height = bitmap.getHeight();  
  18.     
  19.   
  20.     
  21.         Matrix matrix = new Matrix();  
  22.     
  23.         matrix.preScale(1, -1);  
  24.     
  25.   
  26.     
  27.         Bitmap reflectionImage = Bitmap.createBitmap(bitmap, 0, height / 2,  
  28.     
  29.                 width, height / 2, matrix, false);  
  30.     
  31.   
  32.     
  33.         Bitmap bitmapWithReflection = Bitmap.createBitmap(width,  
  34.     
  35.                 (height + height / 2), Config.ARGB_8888);  
  36.     
  37.   
  38.     
  39.         Canvas canvas = new Canvas(bitmapWithReflection);  
  40.     
  41.         canvas.drawBitmap(bitmap, 0, 0, null);  
  42.     
  43.         Paint deafalutPaint = new Paint();  
  44.     
  45.         canvas.drawRect(0, height, width, height + reflectionGap, deafalutPaint);  
  46.     
  47.   
  48.     
  49.         canvas.drawBitmap(reflectionImage, 0, height + reflectionGap, null);  
  50.     
  51.   
  52.     
  53.         Paint paint = new Paint();  
  54.     
  55.         LinearGradient shader = new LinearGradient(0, bitmap.getHeight(), 0,  
  56.     
  57.                 bitmapWithReflection.getHeight() + reflectionGap, 0x70ffffff,  
  58.     
  59.                 0x00ffffff, TileMode.CLAMP);  
  60.     
  61.         paint.setShader(shader);  
  62.     
  63.         paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));  
  64.     
  65.         canvas.drawRect(0, height, width, bitmapWithReflection.getHeight()  
  66.     
  67.                 + reflectionGap, paint);  
  68.     
  69.         return bitmapWithReflection;  
  70.     
  71.     }  
Java 代码复制内容到剪贴板
        
  1. /**
  2.     
  3.     * 把图片变成圆角  
  4.     
  5.     * @param bitmap 需要修改的图片  
  6.     
  7.     * @param pixels 圆角的弧度  
  8.     
  9.     * @return 圆角图片  
  10.     
  11.     */      
  12.     
  13.    public static Bitmap toRoundCorner(Bitmap bitmap, int pixels) {      
  14.     
  15.       
  16.     
  17.        Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888);      
  18.     
  19.        Canvas canvas = new Canvas(output);      
  20.     
  21.       
  22.     
  23.        final int color = 0xff424242;      
  24.     
  25.        final Paint paint = new Paint();      
  26.     
  27.        final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());      
  28.     
  29.        final RectF rectF = new RectF(rect);      
  30.     
  31.        final float roundPx = pixels;      
  32.     
  33.       
  34.     
  35.        paint.setAntiAlias(true);      
  36.     
  37.        canvas.drawARGB(0, 0, 0, 0);      
  38.     
  39.        paint.setColor(color);      
  40.     
  41.        canvas.drawRoundRect(rectF, roundPx, roundPx, paint);      
  42.     
  43.       
  44.     
  45.        paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));      
  46.     
  47.        canvas.drawBitmap(bitmap, rect, rect, paint);      
  48.     
  49.       
  50.     
  51.        return output;      
  52.     
  53.    }    
Java 代码复制内容到剪贴板
        
  1. /**
  2.     
  3.      * 缩放图片
  4.     
  5.      * @param bmp
  6.     
  7.      * @param width
  8.     
  9.      * @param height
  10.     
  11.      * @return
  12.     
  13.      */  
  14.     
  15.     public static Bitmap PicZoom(Bitmap bmp, int width, int height) {  
  16.     
  17.         int bmpWidth = bmp.getWidth();  
  18.     
  19.         int bmpHeght = bmp.getHeight();  
  20.     
  21.         Matrix matrix = new Matrix();  
  22.     
  23.         matrix.postScale((float) width / bmpWidth, (float) height / bmpHeght);  
  24.     
  25.   
  26.     
  27.         return Bitmap.createBitmap(bmp, 0, 0, bmpWidth, bmpHeght, matrix, true);  
  28.     
  29.     }  
Java 代码复制内容到剪贴板
        
  1. /**
  2.     
  3.      * @param photoPath --原图路经
  4.     
  5.      * @param aFile     --保存缩图
  6.     
  7.      * @param newWidth  --缩图宽度
  8.     
  9.      * @param newHeight --缩图高度
  10.     
  11.      */  
  12.     
  13.     public static boolean bitmapToFile(String photoPath, File aFile, int newWidth, int newHeight) {  
  14.     
  15.         BitmapFactory.Options options = new BitmapFactory.Options();  
  16.     
  17.         options.inJustDecodeBounds = true;  
  18.     
  19.         // 获取这个图片的宽和高  
  20.     
  21.         Bitmap bitmap = BitmapFactory.decodeFile(photoPath, options);  
  22.     
  23.         options.inJustDecodeBounds = false;  
  24.     
  25.   
  26.     
  27.         //计算缩放比  
  28.     
  29.         options.inSampleSize = reckonThumbnail(options.outWidth, options.outHeight, newWidth, newHeight);  
  30.     
  31.   
  32.     
  33.         bitmap = BitmapFactory.decodeFile(photoPath, options);  
  34.     
  35.   
  36.     
  37.         try {  
  38.     
  39.             ByteArrayOutputStream baos = new ByteArrayOutputStream();  
  40.     
  41.             bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);  
  42.     
  43.             byte[] photoBytes = baos.toByteArray();  
  44.     
  45.   
  46.     
  47.             if (aFile.exists()) {  
  48.     
  49.                 aFile.delete();  
  50.     
  51.             }  
  52.     
  53.             aFile.createNewFile();  
  54.     
  55.   
  56.     
  57.             FileOutputStream fos = new FileOutputStream(aFile);  
  58.     
  59.             fos.write(photoBytes);  
  60.     
  61.             fos.flush();  
  62.     
  63.             fos.close();  
  64.     
  65.   
  66.     
  67.             return true;  
  68.     
  69.         } catch (Exception e1) {  
  70.     
  71.             e1.printStackTrace();  
  72.     
  73.             if (aFile.exists()) {  
  74.     
  75.                 aFile.delete();  
  76.     
  77.             }  
  78.     
  79.             Log.e("Bitmap To File Fail", e1.toString());  
  80.     
  81.             return false;  
  82.     
  83.         }  
  84.     
  85.     }  
Java 代码复制内容到剪贴板
        
  1. /**
  2.     
  3.      * 计算缩放比
  4.     
  5.      * @param oldWidth
  6.     
  7.      * @param oldHeight
  8.     
  9.      * @param newWidth
  10.     
  11.      * @param newHeight
  12.     
  13.      * @return
  14.     
  15.      */  
  16.     
  17.     public static int reckonThumbnail(int oldWidth, int oldHeight, int newWidth, int newHeight) {  
  18.     
  19.         if ((oldHeight > newHeight && oldWidth > newWidth)  
  20.     
  21.                 || (oldHeight <= newHeight && oldWidth > newWidth)) {  
  22.     
  23.             int be = (int) (oldWidth / (float) newWidth);  
  24.     
  25.             if (be <= 1)  
  26.     
  27.                 be = 1;  
  28.     
  29.             return be;  
  30.     
  31.         } else if (oldHeight > newHeight && oldWidth <= newWidth) {  
  32.     
  33.             int be = (int) (oldHeight / (float) newHeight);  
  34.     
  35.             if (be <= 1)  
  36.     
  37.                 be = 1;  
  38.     
  39.             return be;  
  40.     
  41.         }  
  42.     
  43.   
  44.     
  45.         return 1;  
  46.     
  47.     }  

Android边框圆角

XML/HTML 代码复制内容到剪贴板
        
  1. <?xml version="1.0" encoding="utf-8"?>      
  2.     
  3. <shape xmlns:android="http://schema...android">        
  4.     
  5.     <solid android:color="#000000" />        
  6.     
  7.     <corners android:topLeftRadius="10dp"      
  8.     
  9.                     android:topRightRadius="10dp"        
  10.     
  11.                 android:bottomRightRadius="10dp"      
  12.     
  13.                 android:bottomLeftRadius="10dp"/>        
  14.     
  15. </shape>    

解释:solid的表示填充颜色,为了简单,这里用的是黑色。

而corners则是表示圆角,注意的是这里bottomRightRadius是左下角而不是右下角,bottomLeftRadius右下角。
当然上面的效果也可以像下面一样设置,如下: <corners android:radius="5dp" />  

 

源码下载:

 

如果有更多的功能和方法,请大家评论指出。

posted on 2012-03-15 13:50 life02 阅读(532) 评论(0)  编辑 收藏 引用 所属分类: Android开发

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理