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

Android 图片处理方法大全

Android编程 , 评论(3) , 引用(0) , 阅读(1577) 大 | 中 | 小

整理了一下目前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右下角。
当然上面的效果也可以像下面一样设置,如下: "5dp" />  

 

源码下载:

下载文件 点击这里下载文件

 


更多相关文章

  1. Eclipse,到了说再见的时候了——Android(安卓)Studio最全解析
  2. windows环境下 android 源码阅读
  3. Android中的HTTP通信
  4. 【代码】android通过criteria选择合适的地理位置服务
  5. android2.2更新为android2.3
  6. android 调用系统计算器 实例
  7. android 自定义viewR.styleable找不到或者是报错!
  8. 安卓在代码中设置TextView的drawableLeft、drawableRight、drawa
  9. Android注解——Butter Knife的使用

随机推荐

  1. Android(安卓)Parcelable代码自动生成插
  2. (Android学习之路)Android中listView结合
  3. Android(安卓)ConstraintLayout完全解析
  4. 启动Activity时的方法调用(应用层)(MVC模式)
  5. Android:BaseAdapter的优化方案一览
  6. android ndk返回String(字符串)
  7. 【Android】java.lang.IllegalArgumentEx
  8. Android(安卓)Studio 4.0 gradle-6.1.1-a
  9. android把odex转成dex文件
  10. Android(安卓)List Background and item