android 圖片處理大全

http://06peng.com/read.php/52.htm
原帖地址~~非常好~

Android 圖片處理方法大全 不指定

整理了一下目前Android開發中圖片的各種處理方法:

Java代碼
  1.     
  2.    /**     
  3.      * 使頭像變灰     
  4.      * @param drawable     
  5.      */      
  6.     public static void porBecomeGrey(ImageView imageView, 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.         imageView.setImageDrawable(drawable);      
  13.     }  

 

Java 代碼複製內容到剪貼板
  1.     
  2. Drawable drawable = new FastBitmapDrawable(bitmap);    
Java 代碼複製內容到剪貼板
  1.     
  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.         
  14.         
  15. public Bitmap getBitmapFromByte(byte[] temp){        
  16.     if(temp != null){        
  17.         Bitmap bitmap = BitmapFactory.decodeByteArray(temp, 0, temp.length);        
  18.         return bitmap;        
  19.     }else{        
  20.         return null;        
  21.     }        
  22. }    
Java 代碼複製內容到剪貼板
  1.     
  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.     }  
Java 代碼複製內容到剪貼板
  1.     
  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.       
  12.         Matrix matrix = new Matrix();      
  13.         matrix.preScale(1, -1);      
  14.       
  15.         Bitmap reflectionImage = Bitmap.createBitmap(bitmap, 0, height / 2,      
  16.                 width, height / 2, matrix, false);      
  17.       
  18.         Bitmap bitmapWithReflection = Bitmap.createBitmap(width,      
  19.                 (height + height / 2), Config.ARGB_8888);      
  20.       
  21.         Canvas canvas = new Canvas(bitmapWithReflection);      
  22.         canvas.drawBitmap(bitmap, 0, 0, null);      
  23.         Paint deafalutPaint = new Paint();      
  24.         canvas.drawRect(0, height, width, height + reflectionGap, deafalutPaint);      
  25.       
  26.         canvas.drawBitmap(reflectionImage, 0, height + reflectionGap, null);      
  27.       
  28.         Paint paint = new Paint();      
  29.         LinearGradient shader = new LinearGradient(0, bitmap.getHeight(), 0,      
  30.                 bitmapWithReflection.getHeight() + reflectionGap, 0x70ffffff,      
  31.                 0x00ffffff, TileMode.CLAMP);      
  32.         paint.setShader(shader);      
  33.         paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));      
  34.         canvas.drawRect(0, height, width, bitmapWithReflection.getHeight()      
  35.                 + reflectionGap, paint);      
  36.         return bitmapWithReflection;      
  37.     }  
Java 代碼複製內容到剪貼板
  1.     
  2. /**     
  3.     * 把圖片變成圓角       
  4.     * @param bitmap 需要修改的圖片       
  5.     * @param pixels 圓角的弧度       
  6.     * @return 圓角圖片       
  7.     */          
  8.    public static Bitmap toRoundCorner(Bitmap bitmap, int pixels) {          
  9.            
  10.        Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888);          
  11.        Canvas canvas = new Canvas(output);          
  12.            
  13.        final int color = 0xff424242;          
  14.        final Paint paint = new Paint();          
  15.        final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());          
  16.        final RectF rectF = new RectF(rect);          
  17.        final float roundPx = pixels;          
  18.            
  19.        paint.setAntiAlias(true);          
  20.        canvas.drawARGB(0, 0, 0, 0);          
  21.        paint.setColor(color);          
  22.        canvas.drawRoundRect(rectF, roundPx, roundPx, paint);          
  23.            
  24.        paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));          
  25.        canvas.drawBitmap(bitmap, rect, rect, paint);          
  26.            
  27.        return output;          
  28.    }    
Java 代碼複製內容到剪貼板
  1.     
  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.       
  15.         return Bitmap.createBitmap(bmp, 0, 0, bmpWidth, bmpHeght, matrix, true);      
  16.     }  
Java 代碼複製內容到剪貼板
  1.     
  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.         //計算縮放比      
  16.         options.inSampleSize = reckonThumbnail(options.outWidth, options.outHeight, newWidth, newHeight);      
  17.       
  18.         bitmap = BitmapFactory.decodeFile(photoPath, options);      
  19.       
  20.         try {      
  21.             ByteArrayOutputStream baos = new ByteArrayOutputStream();      
  22.             bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);      
  23.             byte[] photoBytes = baos.toByteArray();      
  24.       
  25.             if (aFile.exists()) {      
  26.                 aFile.delete();      
  27.             }      
  28.             aFile.createNewFile();      
  29.       
  30.             FileOutputStream fos = new FileOutputStream(aFile);      
  31.             fos.write(photoBytes);      
  32.             fos.flush();      
  33.             fos.close();      
  34.       
  35.             return true;      
  36.         } catch (Exception e1) {      
  37.             e1.printStackTrace();      
  38.             if (aFile.exists()) {      
  39.                 aFile.delete();      
  40.             }      
  41.             Log.e("Bitmap To File Fail", e1.toString());      
  42.             return false;      
  43.         }      
  44.     }  
Java 代碼複製內容到剪貼板
  1.     
  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.       
  24.         return 1;      
  25.     }  

Android邊框圓角

XML/HTML 代碼複製內容到剪貼板
  1.     
  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" />  

 

源碼下載:

 


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章