ITeye博客

Android 图片处理方法大全

w1985chun · 2012-07-02 15:41 · Android开发 · 移动开发 · Markdown

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

Java代码

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

 

Java 代码复制内容到剪贴板

  1. Drawable drawable = new FastBitmapDrawable(bitmap);

Java 代码复制内容到剪贴板

  1. public byte[] getBitmapByte(Bitmap bitmap){
  2. ByteArrayOutputStream out = new ByteArrayOutputStream();
  3. bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
  4. try {
  5. out.flush();
  6. out.close();
  7. } catch (IOException e) {
  8. e.printStackTrace();
  9. }
  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 代码复制内容到剪贴板

  1. /**
  2. * 将Drawable转化为Bitmap
  3. * @param drawable
  4. * @return
  5. */
  6. public static Bitmap drawableToBitmap(Drawable drawable) {
  7. int width = drawable.getIntrinsicWidth();
  8. int height = drawable.getIntrinsicHeight();
  9. 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 代码复制内容到剪贴板

  1. /**
  2. * 获取图片的倒影
  3. * @param bitmap
  4. * @return
  5. */
  6. public static Bitmap createReflectionImageWithOrigin(Bitmap bitmap) {
  7. final int reflectionGap = 4;
  8. int width = bitmap.getWidth();
  9. 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 代码复制内容到剪贴板

  1. /**
  2. * 把图片变成圆角
  3. * @param bitmap 需要修改的图片
  4. * @param pixels 圆角的弧度
  5. * @return 圆角图片
  6. */
  7. public static Bitmap toRoundCorner(Bitmap bitmap, int pixels) {
  8. Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888);
  9. 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 代码复制内容到剪贴板

  1. /**
  2. * 缩放图片
  3. * @param bmp
  4. * @param width
  5. * @param height
  6. * @return
  7. */
  8. public static Bitmap PicZoom(Bitmap bmp, int width, int height) {
  9. 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 代码复制内容到剪贴板

  1. /**
  2. * @param photoPath --原图路经
  3. * @param aFile     --保存缩图
  4. * @param newWidth  --缩图宽度
  5. * @param newHeight --缩图高度
  6. */
  7. public static boolean bitmapToFile(String photoPath, File aFile, int newWidth, int newHeight) {
  8. BitmapFactory.Options options = new BitmapFactory.Options();
  9. 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 代码复制内容到剪贴板

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

Android边框圆角

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

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <shape xmlns:android="http://schema...android">
  3. <solid android:color="#000000" />
  4. <corners android:topLeftRadius="10dp"
  5. android:topRightRadius="10dp"
  6. android:bottomRightRadius="10dp"
  7. android:bottomLeftRadius="10dp"/>
  8. </shape>

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

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

 

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

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。