天天看点

Android 图片处理方法大全

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

  1. Java代码  
  2.    /**       
  3.      * 使头像变灰       
  4.      * @param drawable       
  5.      */        
  6.     public static void porBecomeGrey(ImageView p_w_picpathView, Drawable drawable) {        
  7.         drawable.mutate();            
  8.         ColorMatrix cm = new ColorMatrix();            
  9.         cm.setSaturation(0);            
  10.         ColorMatrixColorFilter cf = new ColorMatrixColorFilter(cm);            
  11.         drawable.setColorFilter(cf);         
  12.         p_w_picpathView.setImageDrawable(drawable);        
  13.     }    
  1. Java 代码复制内容到剪贴板  
  2. Drawable drawable = new FastBitmapDrawable(bitmap);      
  1. Java 代码复制内容到剪贴板  
  2. public byte[] getBitmapByte(Bitmap bitmap){          
  3.     ByteArrayOutputStream out = new ByteArrayOutputStream();          
  4.     bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);          
  5.     try {          
  6.         out.flush();          
  7.         out.close();          
  8.     } catch (IOException e) {          
  9.         e.printStackTrace();          
  10.     }          
  11.     return out.toByteArray();          
  12. }          
  13. public Bitmap getBitmapFromByte(byte[] temp){          
  14.     if(temp != null){          
  15.         Bitmap bitmap = BitmapFactory.decodeByteArray(temp, 0, temp.length);          
  16.         return bitmap;          
  17.     }else{          
  18.         return null;          
  19.     }          
  20. }      
  1. Java 代码复制内容到剪贴板  
  2. /**       
  3.      * 将Drawable转化为Bitmap       
  4.      * @param drawable       
  5.      * @return       
  6.      */        
  7.     public static Bitmap drawableToBitmap(Drawable drawable) {        
  8.         int width = drawable.getIntrinsicWidth();        
  9.         int height = drawable.getIntrinsicHeight();        
  10.         Bitmap bitmap = Bitmap.createBitmap(width, height, drawable        
  11.                 .getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888        
  12.                 : Bitmap.Config.RGB_565);        
  13.         Canvas canvas = new Canvas(bitmap);        
  14.         drawable.setBounds(0, 0, width, height);        
  15.         drawable.draw(canvas);        
  16.         return bitmap;        
  17.     }    
  1. Java 代码复制内容到剪贴板  
  2. /**       
  3.     * 获取图片的倒影       
  4.     * @param bitmap       
  5.     * @return       
  6.     */        
  7.     public static Bitmap createReflectionImageWithOrigin(Bitmap bitmap) {        
  8.         final int reflectionGap = 4;        
  9.         int width = bitmap.getWidth();        
  10.         int height = bitmap.getHeight();        
  11.         Matrix matrix = new Matrix();        
  12.         matrix.preScale(1, -1);        
  13.         Bitmap reflectionImage = Bitmap.createBitmap(bitmap, 0, height / 2,        
  14.                 width, height / 2, matrix, false);        
  15.         Bitmap bitmapWithReflection = Bitmap.createBitmap(width,        
  16.                 (height + height / 2), Config.ARGB_8888);        
  17.         Canvas canvas = new Canvas(bitmapWithReflection);        
  18.         canvas.drawBitmap(bitmap, 0, 0, null);        
  19.         Paint deafalutPaint = new Paint();        
  20.         canvas.drawRect(0, height, width, height + reflectionGap, deafalutPaint);        
  21.         canvas.drawBitmap(reflectionImage, 0, height + reflectionGap, null);        
  22.         Paint paint = new Paint();        
  23.         LinearGradient shader = new LinearGradient(0, bitmap.getHeight(), 0,        
  24.                 bitmapWithReflection.getHeight() + reflectionGap, 0x70ffffff,        
  25.                 0x00ffffff, TileMode.CLAMP);        
  26.         paint.setShader(shader);        
  27.         paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));        
  28.         canvas.drawRect(0, height, width, bitmapWithReflection.getHeight()        
  29.                 + reflectionGap, paint);        
  30.         return bitmapWithReflection;        
  31.     }    
  1. Java 代码复制内容到剪贴板  
  2. /**       
  3.     * 把图片变成圆角         
  4.     * @param bitmap 需要修改的图片         
  5.     * @param pixels 圆角的弧度         
  6.     * @return 圆角图片         
  7.     */            
  8.    public static Bitmap toRoundCorner(Bitmap bitmap, int pixels) {            
  9.        Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888);            
  10.        Canvas canvas = new Canvas(output);            
  11.        final int color = 0xff424242;            
  12.        final Paint paint = new Paint();            
  13.        final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());            
  14.        final RectF rectF = new RectF(rect);            
  15.        final float roundPx = pixels;            
  16.        paint.setAntiAlias(true);            
  17.        canvas.drawARGB(0, 0, 0, 0);            
  18.        paint.setColor(color);            
  19.        canvas.drawRoundRect(rectF, roundPx, roundPx, paint);            
  20.        paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));            
  21.        canvas.drawBitmap(bitmap, rect, rect, paint);            
  22.        return output;            
  23.    }      
  1. Java 代码复制内容到剪贴板  
  2. /**       
  3.      * 缩放图片       
  4.      * @param bmp       
  5.      * @param width       
  6.      * @param height       
  7.      * @return       
  8.      */        
  9.     public static Bitmap PicZoom(Bitmap bmp, int width, int height) {        
  10.         int bmpWidth = bmp.getWidth();        
  11.         int bmpHeght = bmp.getHeight();        
  12.         Matrix matrix = new Matrix();        
  13.         matrix.postScale((float) width / bmpWidth, (float) height / bmpHeght);        
  14.         return Bitmap.createBitmap(bmp, 0, 0, bmpWidth, bmpHeght, matrix, true);        
  15.     }    
  1. Java 代码复制内容到剪贴板  
  2. /**       
  3.      * @param photoPath --原图路经       
  4.      * @param aFile     --保存缩图       
  5.      * @param newWidth  --缩图宽度       
  6.      * @param newHeight --缩图高度       
  7.      */        
  8.     public static boolean bitmapToFile(String photoPath, File aFile, int newWidth, int newHeight) {        
  9.         BitmapFactory.Options options = new BitmapFactory.Options();        
  10.         options.inJustDecodeBounds = true;        
  11.         // 获取这个图片的宽和高        
  12.         Bitmap bitmap = BitmapFactory.decodeFile(photoPath, options);        
  13.         options.inJustDecodeBounds = false;        
  14.         //计算缩放比        
  15.         options.inSampleSize = reckonThumbnail(options.outWidth, options.outHeight, newWidth, newHeight);        
  16.         bitmap = BitmapFactory.decodeFile(photoPath, options);        
  17.         try {        
  18.             ByteArrayOutputStream baos = new ByteArrayOutputStream();        
  19.             bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);        
  20.             byte[] photoBytes = baos.toByteArray();        
  21.             if (aFile.exists()) {        
  22.                 aFile.delete();        
  23.             }        
  24.             aFile.createNewFile();        
  25.             FileOutputStream fos = new FileOutputStream(aFile);        
  26.             fos.write(photoBytes);        
  27.             fos.flush();        
  28.             fos.close();        
  29.             return true;        
  30.         } catch (Exception e1) {        
  31.             e1.printStackTrace();        
  32.             if (aFile.exists()) {        
  33.                 aFile.delete();        
  34.             }        
  35.             Log.e("Bitmap To File Fail", e1.toString());        
  36.             return false;        
  37.         }        
  38.     }    
  1. Java 代码复制内容到剪贴板  
  2. /**       
  3.      * 计算缩放比       
  4.      * @param oldWidth       
  5.      * @param oldHeight       
  6.      * @param newWidth       
  7.      * @param newHeight       
  8.      * @return       
  9.      */        
  10.     public static int reckonThumbnail(int oldWidth, int oldHeight, int newWidth, int newHeight) {        
  11.         if ((oldHeight > newHeight && oldWidth > newWidth)        
  12.                 || (oldHeight <= newHeight && oldWidth > newWidth)) {        
  13.             int be = (int) (oldWidth / (float) newWidth);        
  14.             if (be <= 1)        
  15.                 be = 1;        
  16.             return be;        
  17.         } else if (oldHeight > newHeight && oldWidth <= newWidth) {        
  18.             int be = (int) (oldHeight / (float) newHeight);        
  19.             if (be <= 1)        
  20.                 be = 1;        
  21.             return be;        
  22.         }        
  23.         return 1;        
  24.     }    

Android边框圆角

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

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

而corners则是表示圆角,注意的是这里bottomRightRadius是左下角而不是右下角,bottomLeftRadius右下角。

当然上面的效果也可以像下面一样设置,如下: <corners android:radius="5dp" />  

下载文件

点击这里下载文件

继续阅读