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(00, 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, 00null);       
  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(00, bitmap.getWidth(), bitmap.getHeight());           
  16.        final RectF rectF = new RectF(rect);           
  17.        final float roundPx = pixels;           
  18.            
  19.        paint.setAntiAlias(true);           
  20.        canvas.drawARGB(0000);           
  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, 00, 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" />  

 

原文来自:http://06peng.com/read.php/52.htm

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值