---
title: "Android 图片处理方法大全"
author: "w1985chun"
date: "2012-07-02T15:41:43Z"
tags: []
categories: ["移动开发"]
slug: "android"
draft: false
lastmod: "2012-07-02T15:41:43Z"
id: 1574665
url: "https://w1985chun.iteye.com/blog/1574665"
---

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

Java代码

01. /\**
02. * 使头像变灰
03. * @param drawable
04. \*/
05. public static void porBecomeGrey(ImageView imageView, Drawable drawable) {
06. drawable.mutate();
07. ColorMatrix cm = new ColorMatrix();
08. cm.setSaturation(0);
09. ColorMatrixColorFilter cf = new ColorMatrixColorFilter(cm);
10. drawable.setColorFilter(cf);
11. imageView.setImageDrawable(drawable);
12. }

 

Java 代码复制内容到剪贴板

1. Drawable drawable = new FastBitmapDrawable(bitmap);

Java 代码复制内容到剪贴板

01. public byte\[] getBitmapByte(Bitmap bitmap){
02. ByteArrayOutputStream out = new ByteArrayOutputStream();
03. bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
04. try {
05. out.flush();
06. out.close();
07. } catch (IOException e) {
08. e.printStackTrace();
09. }
10. return out.toByteArray();
11. }
12. public Bitmap getBitmapFromByte(byte\[] temp){
13. if(temp != null){
14. Bitmap bitmap = BitmapFactory.decodeByteArray(temp, 0, temp.length);
15. return bitmap;
16. }else{
17. return null;
18. }
19. }

Java 代码复制内容到剪贴板

01. /\**
02. \* 将Drawable转化为Bitmap
03. \* @param drawable
04. \* @return
05. \*/
06. public static Bitmap drawableToBitmap(Drawable drawable) {
07. int width = drawable.getIntrinsicWidth();
08. int height = drawable.getIntrinsicHeight();
09. Bitmap bitmap = Bitmap.createBitmap(width, height, drawable
10. .getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB\_8888
11. : Bitmap.Config.RGB\_565);
12. Canvas canvas = new Canvas(bitmap);
13. drawable.setBounds(0, 0, width, height);
14. drawable.draw(canvas);
15. return bitmap;
16. }

Java 代码复制内容到剪贴板

01. /\**
02. \* 获取图片的倒影
03. \* @param bitmap
04. \* @return
05. \*/
06. public static Bitmap createReflectionImageWithOrigin(Bitmap bitmap) {
07. final int reflectionGap = 4;
08. int width = bitmap.getWidth();
09. int height = bitmap.getHeight();
10. Matrix matrix = new Matrix();
11. matrix.preScale(1, -1);
12. Bitmap reflectionImage = Bitmap.createBitmap(bitmap, 0, height / 2,
13. width, height / 2, matrix, false);
14. Bitmap bitmapWithReflection = Bitmap.createBitmap(width,
15. (height + height / 2), Config.ARGB\_8888);
16. Canvas canvas = new Canvas(bitmapWithReflection);
17. canvas.drawBitmap(bitmap, 0, 0, null);
18. Paint deafalutPaint = new Paint();
19. canvas.drawRect(0, height, width, height + reflectionGap, deafalutPaint);
20. canvas.drawBitmap(reflectionImage, 0, height + reflectionGap, null);
21. Paint paint = new Paint();
22. LinearGradient shader = new LinearGradient(0, bitmap.getHeight(), 0,
23. bitmapWithReflection.getHeight() + reflectionGap, 0x70ffffff,
24. 0x00ffffff, TileMode.CLAMP);
25. paint.setShader(shader);
26. paint.setXfermode(new PorterDuffXfermode(Mode.DST\_IN));
27. canvas.drawRect(0, height, width, bitmapWithReflection.getHeight()
28. \+ reflectionGap, paint);
29. return bitmapWithReflection;
30. }

Java 代码复制内容到剪贴板

01. /\**
02. \* 把图片变成圆角
03. \* @param bitmap 需要修改的图片
04. \* @param pixels 圆角的弧度
05. \* @return 圆角图片
06. \*/
07. public static Bitmap toRoundCorner(Bitmap bitmap, int pixels) {
08. Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB\_8888);
09. Canvas canvas = new Canvas(output);
10. final int color = 0xff424242;
11. final Paint paint = new Paint();
12. final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
13. final RectF rectF = new RectF(rect);
14. final float roundPx = pixels;
15. paint.setAntiAlias(true);
16. canvas.drawARGB(0, 0, 0, 0);
17. paint.setColor(color);
18. canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
19. paint.setXfermode(new PorterDuffXfermode(Mode.SRC\_IN));
20. canvas.drawBitmap(bitmap, rect, rect, paint);
21. return output;
22. }

Java 代码复制内容到剪贴板

01. /\**
02. \* 缩放图片
03. \* @param bmp
04. \* @param width
05. \* @param height
06. \* @return
07. \*/
08. public static Bitmap PicZoom(Bitmap bmp, int width, int height) {
09. int bmpWidth = bmp.getWidth();
10. int bmpHeght = bmp.getHeight();
11. Matrix matrix = new Matrix();
12. matrix.postScale((float) width / bmpWidth, (float) height / bmpHeght);
13. return Bitmap.createBitmap(bmp, 0, 0, bmpWidth, bmpHeght, matrix, true);
14. }

Java 代码复制内容到剪贴板

01. /\**
02. \* @param photoPath --原图路经
03. \* @param aFile     --保存缩图
04. \* @param newWidth  --缩图宽度
05. \* @param newHeight --缩图高度
06. \*/
07. public static boolean bitmapToFile(String photoPath, File aFile, int newWidth, int newHeight) {
08. BitmapFactory.Options options = new BitmapFactory.Options();
09. options.inJustDecodeBounds = true;
10. // 获取这个图片的宽和高
11. Bitmap bitmap = BitmapFactory.decodeFile(photoPath, options);
12. options.inJustDecodeBounds = false;
13. //计算缩放比
14. options.inSampleSize = reckonThumbnail(options.outWidth, options.outHeight, newWidth, newHeight);
15. bitmap = BitmapFactory.decodeFile(photoPath, options);
16. try {
17. ByteArrayOutputStream baos = new ByteArrayOutputStream();
18. bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
19. byte\[] photoBytes = baos.toByteArray();
20. if (aFile.exists()) {
21. aFile.delete();
22. }
23. aFile.createNewFile();
24. FileOutputStream fos = new FileOutputStream(aFile);
25. fos.write(photoBytes);
26. fos.flush();
27. fos.close();
28. return true;
29. } catch (Exception e1) {
30. e1.printStackTrace();
31. if (aFile.exists()) {
32. aFile.delete();
33. }
34. Log.e("Bitmap To File Fail", e1.toString());
35. return false;
36. }
37. }

Java 代码复制内容到剪贴板

01. /\**
02. \* 计算缩放比
03. \* @param oldWidth
04. \* @param oldHeight
05. \* @param newWidth
06. \* @param newHeight
07. \* @return
08. \*/
09. public static int reckonThumbnail(int oldWidth, int oldHeight, int newWidth, int newHeight) {
10. if ((oldHeight &gt; newHeight && oldWidth &gt; newWidth)
11. || (oldHeight &lt;= newHeight && oldWidth &gt; newWidth)) {
12. int be = (int) (oldWidth / (float) newWidth);
13. if (be &lt;= 1)
14. be = 1;
15. return be;
16. } else if (oldHeight &gt; newHeight && oldWidth &lt;= newWidth) {
17. int be = (int) (oldHeight / (float) newHeight);
18. if (be &lt;= 1)
19. be = 1;
20. return be;
21. }
22. return 1;
23. }

Android边框圆角

XML/HTML 代码复制内容到剪贴板

1. &lt;?xml version="1.0" encoding="utf-8"?&gt;
2. &lt;shape xmlns:android="[http://schema...android"](http://schemas.android.com/apk/res/android%22)&gt;
3. &lt;solid android:color="#000000" /&gt;
4. &lt;corners android:topLeftRadius="10dp"
5. android:topRightRadius="10dp"
6. android:bottomRightRadius="10dp"
7. android:bottomLeftRadius="10dp"/&gt;
8. &lt;/shape&gt;

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

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

 

原文来自：[http://06peng.com/read.php/52.htm](http://06peng.com/read.php/52.htm)