热门标签 | HotTags
当前位置:  开发笔记 > Android > 正文

Android编程之图片相关代码集锦

这篇文章主要介绍了Android编程之图片相关代码集锦,实例总结了大量Android图片操作相关代码,具有一定参考借鉴价值,需要的朋友可以参考下

本文实例总结了Android编程之图片相关代码。分享给大家供大家参考,具体如下:

1. Bitmap转化为字符串:

/** 
* @param 位图 
* @return 转化成的字符串 
*/ 
public static String bitmapToString(Bitmap bitmap) { 
 // 将Bitmap转换成字符串 
 String string = null; 
 ByteArrayOutputStream bStream = new ByteArrayOutputStream(); 
 bitmap.compress(CompressFormat.PNG, 100, bStream); 
 byte[] bytes = bStream.toByteArray(); 
 string = Base64.encodeToString(bytes, Base64.DEFAULT); 
 return string; 
} 

2.字符串转化为Bitmap:

/** 
* @param string 字符串 
* @return 转化成的位图 
*/ 
public static Bitmap stringToBitmap(String string) { 
  // 将字符串转换成Bitmap类型 
  Bitmap bitmap = null; 
  try { 
   byte[] bitmapArray; 
   bitmapArray = Base64.decode(string, Base64.DEFAULT); 
   bitmap = BitmapFactory.decodeByteArray(bitmapArray, 0, bitmapArray.length); 
  } catch (Exception e) { 
   e.printStackTrace(); 
  } 
  return bitmap; 
} 

3.Bitmap转化为Drawable:

/** 
* @param bitmap Bitmap位图图像 
* @return Drawable 转换后的Drawable对象 
*/ 
public static Drawable bitmapToDrawable(Bitmap bitmap) { 
 if (bitmap == null) 
  return null; 
 if (160 != bitmap.getDensity()) { 
  bitmap.setDensity(160); 
 } 
 return new BitmapDrawable(bitmap); 
} 

根据图片资源ID获取Drawable对象:

/** 
 * @param context 上下文 
 * @param id  图片的资源ID 
 * @return Drawable对象 
 */ 
public static Drawable resourceToDrawable(Context context,int id) { 
 return null == context ? null : bitmapToDrawable(BitmapFactory.decodeResource(context.getResources(), id)); 
} 

byte数组转换Drawble对象:

/** 
 * @param bytes byte数组 
 * @return drawble对象 
 */ 
public static Drawable byteArrayToDrawable(byte[] bytes) { 
 return null == bytes ? null : bitmapToDrawable(BitmapFactory.decodeByteArray(bytes, 0, bytes.length)); 
} 

4.Drawable转化为bitmap:

/** 
* Drawble对象转Bitmap对象 
* @param drawable drawble对象 
* @return bitmap对象 
*/ 
public static Bitmap drawableToBitmap(Drawable drawable) { 
  return null == drawable ? null : ((BitmapDrawable) drawable).getBitmap(); 
} 

5.byte数组转换Bitmap对象:

/** 
* @param bytes byte数组 
* @return bitmap对象 
*/ 
public static Bitmap byteArrayToBitmap(byte[] bytes) { 
  return null == bytes ? null : BitmapFactory.decodeByteArray(bytes, 0, bytes.length); 
} 

6.图片去色,返回灰度图片(老式图片):

/** 
* @param bitmap 传入的bitmap 
* @return 去色后的图片Bitmap对象 
*/ 
public static Bitmap toGrayscale(Bitmap bitmap) { 
  int width,height; 
  height = bitmap.getHeight(); 
  width = bitmap.getWidth(); 
  Bitmap bmpGrayscale = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565); 
  Canvas c = new Canvas(bmpGrayscale); 
  Paint paint = new Paint(); 
  ColorMatrix cm = new ColorMatrix(); 
  cm.setSaturation(0); 
  ColorMatrixColorFilter f = new ColorMatrixColorFilter(cm); 
  paint.setColorFilter(f); 
  c.drawBitmap(bitmap, 0, 0, paint); 
  return bmpGrayscale; 
} 

7.对图片进行缩放:

/** 
* @param url   图片的路径 
* @param requireSize 缩放的尺寸 
* @return 缩放后的图片Bitmap对象 
*/ 
public static Bitmap getScaleImage(String url,int requireSize) { 
  BitmapFactory.Options o = new BitmapFactory.Options(); 
  // 此属性表示图片不加载到内存,只是读取图片的属性,包括图片的高宽 
  o.inJustDecodeBounds = true; 
  BitmapFactory.decodeFile(url, o); 
  int width_tmp = o.outWidth,height_tmp = o.outHeight; 
  int scale = 1; 
  while (true) { 
   if (width_tmp / 2 

8.获得图片的倒影,同时倒影渐变效果:

/** 
* @param bitmap 图片源 
* @return 处理后的图片Bitmap对象 
*/ 
public static Bitmap createMirro(Bitmap bitmap) { 
  int width = bitmap.getWidth(); 
  int height = bitmap.getHeight(); 
  int shadow_height = 15; 
  int[] pixels = new int[width * height]; 
  bitmap.getPixels(pixels, 0, width, 0, 0, width, height); 
  // shadow effect 
  int alpha = 0x00000000; 
  for (int y = 0; y > 16) & 0xff; 
    int g = (pixels[index] >> 8) & 0xff; 
    int b = pixels[index] & 0xff; 
    pixels[index] = alpha | (r <<16) | (g <<8) | b; 
   } 
   if (y >= (height - shadow_height)) { 
    alpha = alpha + 0x1F000000; 
   } 
  } 
  // invert effect 
  Bitmap bm = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); 
  for (int y = 0; y 

9.保存图片到SDCard:

/** 
* @param imagePath 图片保存路径 
* @param bm 被保存的bitmap对象 
*/ 
public static void saveImgToLocal(String imagePath, Bitmap bm) { 
  if (bm == null || imagePath == null || "".equals(imagePath)) { 
   return; 
  } 
  File f = new File(imagePath); 
  if (f.exists()) { 
   return; 
  } else { 
   try { 
    File parentFile = f.getParentFile(); 
    if (!parentFile.exists()) { 
     parentFile.mkdirs(); 
    } 
    f.createNewFile(); 
    FileOutputStream fos; 
    fos = new FileOutputStream(f); 
    bm.compress(Bitmap.CompressFormat.PNG, 100, fos); 
    fos.close(); 
   } catch (FileNotFoundException e) { 
    f.delete(); 
    e.printStackTrace(); 
   } catch (IOException e) { 
    e.printStackTrace(); 
    f.delete(); 
   } 
  } 
}

10.从SDCard中获取图片:

/** 
* @param imagePath 图片在SDCard中保存的路径 
* @return 返回保存的bitmap对象 
*/ 
public static Bitmap getImageFromLocal(String imagePath) { 
  File file = new File(imagePath); 
  if (file.exists()) { 
   Bitmap bitmap = BitmapFactory.decodeFile(imagePath); 
   file.setLastModified(System.currentTimeMillis()); 
   return bitmap; 
  } 
  return null; 
}

11.图片压缩处理:

/** 
* 对图片进行压缩,主要是为了解决控件显示过大图片占用内存造成OOM问题。 
* 一般压缩后的图片大小应该和用来展示它的控件大小相近。 
* @param context 上下文 
* @param resId 图片资源Id 
* @param reqWidth 期望压缩的宽度 
* @param reqHeight 期望压缩的高度 
* @return 压缩后的图片 
*/ 
public static Bitmap compressBitmapFromResourse(Context context, int resId, int reqWidth, int reqHeight) { 
  final BitmapFactory.Options optiOns= new BitmapFactory.Options(); 
  /* 
   * 第一次解析时,inJustDecodeBounds设置为true, 
   * 禁止为bitmap分配内存,虽然bitmap返回值为空,但可以获取图片大小 
   */ 
  options.inJustDecodeBounds = true; 
  BitmapFactory.decodeResource(context.getResources(), resId, options); 
  final int height = options.outHeight; 
  final int width = options.outWidth; 
  int inSampleSize = 1; 
  if (height > reqHeight || width > reqWidth) { 
   final int heightRatio = Math.round((float) height / (float) reqHeight);
   final int widthRatio = Math.round((float) width / (float) reqWidth);
   inSampleSize = heightRatio 

12. 获取可用内存的最大值(App使用内存超出这个值会引起OutOfMemory异常):

private int getMaxMemoryForApp() { 
  int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024); 
  return maxMemory; 
}

13.将图片裁剪成圆圈:

/** 
* 将Bitmap处理为圆形的图片 
* @param bitmap 处理之前的位图 
* @return 处理之后的位图 
*/ 
public static Bitmap circlePic(Bitmap bitmap){ 
  int width = bitmap.getWidth(); 
  int height = bitmap.getHeight(); 
  int r = width  h , 左上坐标((w-h)/2, 0) , 右上坐标((w+h)/2, h) 偏移10 
   */ 
  final Rect rect = new Rect( width 

希望本文所述对大家Android程序设计有所帮助。


推荐阅读
  • 深入理解OAuth认证机制
    本文介绍了OAuth认证协议的核心概念及其工作原理。OAuth是一种开放标准,旨在为第三方应用提供安全的用户资源访问授权,同时确保用户的账户信息(如用户名和密码)不会暴露给第三方。 ... [详细]
  • 2023 ARM嵌入式系统全国技术巡讲旨在分享ARM公司在半导体知识产权(IP)领域的最新进展。作为全球领先的IP提供商,ARM在嵌入式处理器市场占据主导地位,其产品广泛应用于90%以上的嵌入式设备中。此次巡讲将邀请来自ARM、飞思卡尔以及华清远见教育集团的行业专家,共同探讨当前嵌入式系统的前沿技术和应用。 ... [详细]
  • 在计算机技术的学习道路上,51CTO学院以其专业性和专注度给我留下了深刻印象。从2012年接触计算机到2014年开始系统学习网络技术和安全领域,51CTO学院始终是我信赖的学习平台。 ... [详细]
  • CSS 布局:液态三栏混合宽度布局
    本文介绍了如何使用 CSS 实现液态的三栏布局,其中各栏具有不同的宽度设置。通过调整容器和内容区域的属性,可以实现灵活且响应式的网页设计。 ... [详细]
  • Linux 系统启动故障排除指南:MBR 和 GRUB 问题
    本文详细介绍了 Linux 系统启动过程中常见的 MBR 扇区和 GRUB 引导程序故障及其解决方案,涵盖从备份、模拟故障到恢复的具体步骤。 ... [详细]
  • 本文介绍了如何使用jQuery根据元素的类型(如复选框)和标签名(如段落)来获取DOM对象。这有助于更高效地操作网页中的特定元素。 ... [详细]
  • 本文将详细介绍如何使用剪映应用中的镜像功能,帮助用户轻松实现视频的镜像效果。通过简单的步骤,您可以快速掌握这一实用技巧。 ... [详细]
  • 深入理解Cookie与Session会话管理
    本文详细介绍了如何通过HTTP响应和请求处理浏览器的Cookie信息,以及如何创建、设置和管理Cookie。同时探讨了会话跟踪技术中的Session机制,解释其原理及应用场景。 ... [详细]
  • 本文介绍如何在 Xcode 中使用快捷键和菜单命令对多行代码进行缩进,包括右缩进和左缩进的具体操作方法。 ... [详细]
  • 本文介绍了一款用于自动化部署 Linux 服务的 Bash 脚本。该脚本不仅涵盖了基本的文件复制和目录创建,还处理了系统服务的配置和启动,确保在多种 Linux 发行版上都能顺利运行。 ... [详细]
  • 在Linux系统中配置并启动ActiveMQ
    本文详细介绍了如何在Linux环境中安装和配置ActiveMQ,包括端口开放及防火墙设置。通过本文,您可以掌握完整的ActiveMQ部署流程,确保其在网络环境中正常运行。 ... [详细]
  • Android 渐变圆环加载控件实现
    本文介绍了如何在 Android 中创建一个自定义的渐变圆环加载控件,该控件已在多个知名应用中使用。我们将详细探讨其工作原理和实现方法。 ... [详细]
  • 如何在WPS Office for Mac中调整Word文档的文字排列方向
    本文将详细介绍如何使用最新版WPS Office for Mac调整Word文档中的文字排列方向。通过这些步骤,用户可以轻松更改文本的水平或垂直排列方式,以满足不同的排版需求。 ... [详细]
  • 本文总结了在使用Ionic 5进行Android平台APK打包时遇到的问题,特别是针对QRScanner插件的改造。通过详细分析和提供具体的解决方法,帮助开发者顺利打包并优化应用性能。 ... [详细]
  • 理解存储器的层次结构有助于程序员优化程序性能,通过合理安排数据在不同层级的存储位置,提升CPU的数据访问速度。本文详细探讨了静态随机访问存储器(SRAM)和动态随机访问存储器(DRAM)的工作原理及其应用场景,并介绍了存储器模块中的数据存取过程及局部性原理。 ... [详细]
author-avatar
rsidugjig
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有