Android 图片处理方法大全

简介:

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

 

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

 

 
  1. Java 代码复制内容到剪贴板  
  2.       
  3. Drawable drawable = new FastBitmapDrawable(bitmap);      

 

 
  1. Java 代码复制内容到剪贴板  
  2.       
  3. public byte[] getBitmapByte(Bitmap bitmap){          
  4.     ByteArrayOutputStream out = new ByteArrayOutputStream();          
  5.     bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);          
  6.     try {          
  7.         out.flush();          
  8.         out.close();          
  9.     } catch (IOException e) {          
  10.         e.printStackTrace();          
  11.     }          
  12.     return out.toByteArray();          
  13. }          
  14.           
  15.           
  16. public Bitmap getBitmapFromByte(byte[] temp){          
  17.     if(temp != null){          
  18.         Bitmap bitmap = BitmapFactory.decodeByteArray(temp, 0, temp.length);          
  19.         return bitmap;          
  20.     }else{          
  21.         return null;          
  22.     }          
  23. }      

 

 
  1. Java 代码复制内容到剪贴板  
  2.       
  3. /**       
  4.      * 将Drawable转化为Bitmap       
  5.      * @param drawable       
  6.      * @return       
  7.      */        
  8.     public static Bitmap drawableToBitmap(Drawable drawable) {        
  9.         int width = drawable.getIntrinsicWidth();        
  10.         int height = drawable.getIntrinsicHeight();        
  11.         Bitmap bitmap = Bitmap.createBitmap(width, height, drawable        
  12.                 .getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888        
  13.                 : Bitmap.Config.RGB_565);        
  14.         Canvas canvas = new Canvas(bitmap);        
  15.         drawable.setBounds(00, width, height);        
  16.         drawable.draw(canvas);        
  17.         return bitmap;        
  18.     }    

 

 
  1. Java 代码复制内容到剪贴板  
  2.       
  3. /**       
  4.     * 获取图片的倒影       
  5.     * @param bitmap       
  6.     * @return       
  7.     */        
  8.     public static Bitmap createReflectionImageWithOrigin(Bitmap bitmap) {        
  9.         final int reflectionGap = 4;        
  10.         int width = bitmap.getWidth();        
  11.         int height = bitmap.getHeight();        
  12.         
  13.         Matrix matrix = new Matrix();        
  14.         matrix.preScale(1, -1);        
  15.         
  16.         Bitmap reflectionImage = Bitmap.createBitmap(bitmap, 0, height / 2,        
  17.                 width, height / 2, matrix, false);        
  18.         
  19.         Bitmap bitmapWithReflection = Bitmap.createBitmap(width,        
  20.                 (height + height / 2), Config.ARGB_8888);        
  21.         
  22.         Canvas canvas = new Canvas(bitmapWithReflection);        
  23.         canvas.drawBitmap(bitmap, 00null);        
  24.         Paint deafalutPaint = new Paint();        
  25.         canvas.drawRect(0, height, width, height + reflectionGap, deafalutPaint);        
  26.         
  27.         canvas.drawBitmap(reflectionImage, 0, height + reflectionGap, null);        
  28.         
  29.         Paint paint = new Paint();        
  30.         LinearGradient shader = new LinearGradient(0, bitmap.getHeight(), 0,        
  31.                 bitmapWithReflection.getHeight() + reflectionGap, 0x70ffffff,        
  32.                 0x00ffffff, TileMode.CLAMP);        
  33.         paint.setShader(shader);        
  34.         paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));        
  35.         canvas.drawRect(0, height, width, bitmapWithReflection.getHeight()        
  36.                 + reflectionGap, paint);        
  37.         return bitmapWithReflection;        
  38.     }    

 

 

 
  1. Java 代码复制内容到剪贴板  
  2.       
  3. /**       
  4.     * 把图片变成圆角         
  5.     * @param bitmap 需要修改的图片         
  6.     * @param pixels 圆角的弧度         
  7.     * @return 圆角图片         
  8.     */            
  9.    public static Bitmap toRoundCorner(Bitmap bitmap, int pixels) {            
  10.              
  11.        Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888);            
  12.        Canvas canvas = new Canvas(output);            
  13.              
  14.        final int color = 0xff424242;            
  15.        final Paint paint = new Paint();            
  16.        final Rect rect = new Rect(00, bitmap.getWidth(), bitmap.getHeight());            
  17.        final RectF rectF = new RectF(rect);            
  18.        final float roundPx = pixels;            
  19.              
  20.        paint.setAntiAlias(true);            
  21.        canvas.drawARGB(0000);            
  22.        paint.setColor(color);            
  23.        canvas.drawRoundRect(rectF, roundPx, roundPx, paint);            
  24.              
  25.        paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));            
  26.        canvas.drawBitmap(bitmap, rect, rect, paint);            
  27.              
  28.        return output;            
  29.    }      

 

 
  1. Java 代码复制内容到剪贴板  
  2.       
  3. /**       
  4.      * 缩放图片       
  5.      * @param bmp       
  6.      * @param width       
  7.      * @param height       
  8.      * @return       
  9.      */        
  10.     public static Bitmap PicZoom(Bitmap bmp, int width, int height) {        
  11.         int bmpWidth = bmp.getWidth();        
  12.         int bmpHeght = bmp.getHeight();        
  13.         Matrix matrix = new Matrix();        
  14.         matrix.postScale((float) width / bmpWidth, (float) height / bmpHeght);        
  15.         
  16.         return Bitmap.createBitmap(bmp, 00, bmpWidth, bmpHeght, matrix, true);        
  17.     }    

 

 
  1. Java 代码复制内容到剪贴板  
  2.       
  3. /**       
  4.      * @param photoPath --原图路经       
  5.      * @param aFile     --保存缩图       
  6.      * @param newWidth  --缩图宽度       
  7.      * @param newHeight --缩图高度       
  8.      */        
  9.     public static boolean bitmapToFile(String photoPath, File aFile, int newWidth, int newHeight) {        
  10.         BitmapFactory.Options options = new BitmapFactory.Options();        
  11.         options.inJustDecodeBounds = true;        
  12.         // 获取这个图片的宽和高        
  13.         Bitmap bitmap = BitmapFactory.decodeFile(photoPath, options);        
  14.         options.inJustDecodeBounds = false;        
  15.          
  16.         //计算缩放比        
  17.         options.inSampleSize = reckonThumbnail(options.outWidth, options.outHeight, newWidth, newHeight);        
  18.         
  19.         bitmap = BitmapFactory.decodeFile(photoPath, options);        
  20.         
  21.         try {        
  22.             ByteArrayOutputStream baos = new ByteArrayOutputStream();        
  23.             bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);        
  24.             byte[] photoBytes = baos.toByteArray();        
  25.         
  26.             if (aFile.exists()) {        
  27.                 aFile.delete();        
  28.             }        
  29.             aFile.createNewFile();        
  30.         
  31.             FileOutputStream fos = new FileOutputStream(aFile);        
  32.             fos.write(photoBytes);        
  33.             fos.flush();        
  34.             fos.close();        
  35.         
  36.             return true;        
  37.         } catch (Exception e1) {        
  38.             e1.printStackTrace();        
  39.             if (aFile.exists()) {        
  40.                 aFile.delete();        
  41.             }        
  42.             Log.e("Bitmap To File Fail", e1.toString());        
  43.             return false;        
  44.         }        
  45.     }    

 

 
  1. Java 代码复制内容到剪贴板  
  2.       
  3. /**       
  4.      * 计算缩放比       
  5.      * @param oldWidth       
  6.      * @param oldHeight       
  7.      * @param newWidth       
  8.      * @param newHeight       
  9.      * @return       
  10.      */        
  11.     public static int reckonThumbnail(int oldWidth, int oldHeight, int newWidth, int newHeight) {        
  12.         if ((oldHeight > newHeight && oldWidth > newWidth)        
  13.                 || (oldHeight <= newHeight && oldWidth > newWidth)) {        
  14.             int be = (int) (oldWidth / (float) newWidth);        
  15.             if (be <= 1)        
  16.                 be = 1;        
  17.             return be;        
  18.         } else if (oldHeight > newHeight && oldWidth <= newWidth) {        
  19.             int be = (int) (oldHeight / (float) newHeight);        
  20.             if (be <= 1)        
  21.                 be = 1;        
  22.             return be;        
  23.         }        
  24.         
  25.         return 1;        
  26.     }    

Android边框圆角

 

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

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

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

 

源码下载:

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


     本文转自06peng 51CTO博客,原文链接:http://blog.51cto.com/06peng/962505,如需转载请自行联系原作者






相关文章
|
3月前
|
Android开发 Python
Python封装ADB获取Android设备wifi地址的方法
Python封装ADB获取Android设备wifi地址的方法
61 0
|
4月前
|
XML Java Android开发
Android Studio App开发之对图片进行简单加工(包括放缩,旋转等等 附源码)
Android Studio App开发之对图片进行简单加工(包括放缩,旋转等等 附源码)
45 0
|
4月前
|
XML Java Android开发
Android Studio App开发之使用相机拍摄照片和从相册中选取图片(附源码 超详细必看)
Android Studio App开发之使用相机拍摄照片和从相册中选取图片(附源码 超详细必看)
181 0
|
4月前
|
XML JSON Java
Android App开发即时通信中通过SocketIO在客户端与服务端间传输文本和图片的讲解及实战(超详细 附源码)
Android App开发即时通信中通过SocketIO在客户端与服务端间传输文本和图片的讲解及实战(超详细 附源码)
72 0
|
24天前
|
Android开发
Android保存图片到相册(适配android 10以下及以上)
Android保存图片到相册(适配android 10以下及以上)
22 1
|
24天前
|
Android开发
Android调用相机与相册的方法2
Android调用相机与相册的方法
18 0
|
6月前
|
SQL 人工智能 移动开发
Android etc1tool之png图片转换pkm 和 zipalign简介
etc1tool 是一种命令行实用程序,可用于将 PNG 图片编码为 ETC1 压缩标准格式(PKM),并将 ETC1 压缩图片解码回 PNG。
|
3月前
|
安全 Java 数据安全/隐私保护
Android和iOS应用程序加固方法详解:混淆、加壳、数据加密、动态加载和数字签名实现
Android和iOS应用程序加固方法详解:混淆、加壳、数据加密、动态加载和数字签名实现
74 0
|
4月前
|
API Android开发
[Android]图片加载库Glide
[Android]图片加载库Glide
55 0
|
4月前
|
Android开发
[Android]制作9-Patch图片
[Android]制作9-Patch图片
42 0