首页 > 系统 > Android > 正文

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

2019-10-24 20:34:31
字体:
来源:转载
供稿:网友

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

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

1. Bitmap转化为字符串:

 

 
  1. /**  
  2. * @param 位图  
  3. * @return 转化成的字符串  
  4. */ 
  5. public static String bitmapToString(Bitmap bitmap) {  
  6. // 将Bitmap转换成字符串  
  7. String string = null;  
  8. ByteArrayOutputStream bStream = new ByteArrayOutputStream();  
  9. bitmap.compress(CompressFormat.PNG, 100, bStream);  
  10. byte[] bytes = bStream.toByteArray();  
  11. string = Base64.encodeToString(bytes, Base64.DEFAULT);  
  12. return string;  
  13. }  

2.字符串转化为Bitmap:

 

 
  1. /**  
  2. * @param string 字符串  
  3. * @return 转化成的位图  
  4. */ 
  5. public static Bitmap stringToBitmap(String string) {  
  6. // 将字符串转换成Bitmap类型  
  7. Bitmap bitmap = null;  
  8. try {  
  9. byte[] bitmapArray;  
  10. bitmapArray = Base64.decode(string, Base64.DEFAULT);  
  11. bitmap = BitmapFactory.decodeByteArray(bitmapArray, 0, bitmapArray.length);  
  12. catch (Exception e) {  
  13. e.printStackTrace();  
  14. }  
  15. return bitmap;  
  16. }  

3.Bitmap转化为Drawable:

 

 
  1. /**  
  2. * @param bitmap Bitmap位图图像  
  3. * @return Drawable 转换后的Drawable对象  
  4. */ 
  5. public static Drawable bitmapToDrawable(Bitmap bitmap) {  
  6. if (bitmap == null)  
  7. return null;  
  8. if (160 != bitmap.getDensity()) {  
  9. bitmap.setDensity(160);  
  10. }  
  11. return new BitmapDrawable(bitmap);  
  12. }  

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

 

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

byte数组转换Drawble对象:

 

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

4.Drawable转化为bitmap:

 

 
  1. /**  
  2. * Drawble对象转Bitmap对象  
  3. * @param drawable drawble对象  
  4. * @return bitmap对象  
  5. */ 
  6. public static Bitmap drawableToBitmap(Drawable drawable) {  
  7. return null == drawable ? null : ((BitmapDrawable) drawable).getBitmap();  
  8. }  

5.byte数组转换Bitmap对象:

 

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

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

 

 
  1. /**  
  2. * @param bitmap 传入的bitmap  
  3. * @return 去色后的图片Bitmap对象  
  4. */ 
  5. public static Bitmap toGrayscale(Bitmap bitmap) {  
  6. int width,height;  
  7. height = bitmap.getHeight();  
  8. width = bitmap.getWidth();  
  9. Bitmap bmpGrayscale = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);  
  10. Canvas c = new Canvas(bmpGrayscale);  
  11. Paint paint = new Paint();  
  12. ColorMatrix cm = new ColorMatrix();  
  13. cm.setSaturation(0);  
  14. ColorMatrixColorFilter f = new ColorMatrixColorFilter(cm);  
  15. paint.setColorFilter(f);  
  16. c.drawBitmap(bitmap, 0, 0, paint);  
  17. return bmpGrayscale;  
  18. }  

7.对图片进行缩放:

 

 
  1. /**  
  2. * @param url 图片的路径  
  3. * @param requireSize 缩放的尺寸  
  4. * @return 缩放后的图片Bitmap对象  
  5. */ 
  6. public static Bitmap getScaleImage(String url,int requireSize) {  
  7. BitmapFactory.Options o = new BitmapFactory.Options();  
  8. // 此属性表示图片不加载到内存,只是读取图片的属性,包括图片的高宽  
  9. o.inJustDecodeBounds = true;  
  10. BitmapFactory.decodeFile(url, o);  
  11. int width_tmp = o.outWidth,height_tmp = o.outHeight;  
  12. int scale = 1;  
  13. while (true) {  
  14. if (width_tmp / 2 < requireSize || height_tmp / 2 < requireSize)  
  15. break;  
  16. width_tmp /= 2;  
  17. height_tmp /= 2;  
  18. scale *= 2;  
  19. }  
  20. BitmapFactory.Options o2 = new BitmapFactory.Options();  
  21. o2.inSampleSize = scale;  
  22. Bitmap bmp = BitmapFactory.decodeFile(url, o2);  
  23. return bmp;  
  24. }  

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

 

 
  1. /**  
  2. * @param bitmap 图片源  
  3. * @return 处理后的图片Bitmap对象  
  4. */ 
  5. public static Bitmap createMirro(Bitmap bitmap) {  
  6. int width = bitmap.getWidth();  
  7. int height = bitmap.getHeight();  
  8. int shadow_height = 15;  
  9. int[] pixels = new int[width * height];  
  10. bitmap.getPixels(pixels, 0, width, 0, 0, width, height);  
  11. // shadow effect  
  12. int alpha = 0x00000000;  
  13. for (int y = 0; y < height; y++) {  
  14. for (int x = 0; x < width; x++) {  
  15. int index = y * width + x;  
  16. int r = (pixels[index] >> 16) & 0xff;  
  17. int g = (pixels[index] >> 8) & 0xff;  
  18. int b = pixels[index] & 0xff;  
  19. pixels[index] = alpha | (r << 16) | (g << 8) | b;  
  20. }  
  21. if (y >= (height - shadow_height)) {  
  22. alpha = alpha + 0x1F000000;  
  23. }  
  24. }  
  25. // invert effect  
  26. Bitmap bm = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);  
  27. for (int y = 0; y < height; y++) {  
  28. bm.setPixels(pixels, y * width, width, 0, height - y - 1, width, 1);  
  29. }  
  30. return Bitmap.createBitmap(bm, 0, 0, width, shadow_height);  
  31. }  

9.保存图片到SDCard:

 

 
  1. /**  
  2. * @param imagePath 图片保存路径  
  3. * @param bm 被保存的bitmap对象  
  4. */ 
  5. public static void saveImgToLocal(String imagePath, Bitmap bm) {  
  6. if (bm == null || imagePath == null || "".equals(imagePath)) {  
  7. return;  
  8. }  
  9. File f = new File(imagePath);  
  10. if (f.exists()) {  
  11. return;  
  12. else {  
  13. try {  
  14. File parentFile = f.getParentFile();  
  15. if (!parentFile.exists()) {  
  16. parentFile.mkdirs();  
  17. }  
  18. f.createNewFile();  
  19. FileOutputStream fos;  
  20. fos = new FileOutputStream(f);  
  21. bm.compress(Bitmap.CompressFormat.PNG, 100, fos);  
  22. fos.close();  
  23. catch (FileNotFoundException e) {  
  24. f.delete();  
  25. e.printStackTrace();  
  26. catch (IOException e) {  
  27. e.printStackTrace();  
  28. f.delete();  
  29. }  
  30. }  

10.从SDCard中获取图片:

 

 
  1. /**  
  2. * @param imagePath 图片在SDCard中保存的路径  
  3. * @return 返回保存的bitmap对象  
  4. */ 
  5. public static Bitmap getImageFromLocal(String imagePath) {  
  6. File file = new File(imagePath);  
  7. if (file.exists()) {  
  8. Bitmap bitmap = BitmapFactory.decodeFile(imagePath);  
  9. file.setLastModified(System.currentTimeMillis());  
  10. return bitmap;  
  11. }  
  12. return null;  

11.图片压缩处理:

 

 
  1. /**  
  2. * 对图片进行压缩,主要是为了解决控件显示过大图片占用内存造成OOM问题。  
  3. * 一般压缩后的图片大小应该和用来展示它的控件大小相近。  
  4. * @param context 上下文  
  5. * @param resId 图片资源Id  
  6. * @param reqWidth 期望压缩的宽度  
  7. * @param reqHeight 期望压缩的高度  
  8. * @return 压缩后的图片  
  9. */ 
  10. public static Bitmap compressBitmapFromResourse(Context context, int resId, int reqWidth, int reqHeight) {  
  11. final BitmapFactory.Options options = new BitmapFactory.Options();  
  12. /*  
  13. * 第一次解析时,inJustDecodeBounds设置为true,  
  14. * 禁止为bitmap分配内存,虽然bitmap返回值为空,但可以获取图片大小  
  15. */ 
  16. options.inJustDecodeBounds = true;  
  17. BitmapFactory.decodeResource(context.getResources(), resId, options);  
  18. final int height = options.outHeight;  
  19. final int width = options.outWidth;  
  20. int inSampleSize = 1;  
  21. if (height > reqHeight || width > reqWidth) {  
  22. final int heightRatio = Math.round((float) height / (float) reqHeight); 
  23. final int widthRatio = Math.round((float) width / (float) reqWidth); 
  24. inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio; 
  25. }  
  26. options.inSampleSize = inSampleSize;  
  27. //使用计算得到的inSampleSize值再次解析图片  
  28. options.inJustDecodeBounds = false;  
  29. return BitmapFactory.decodeResource(context.getResources(), resId, options);  

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

 

 
  1. private int getMaxMemoryForApp() {  
  2. int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);  
  3. return maxMemory;  

13.将图片裁剪成圆圈:

 

 
  1. /**  
  2. * 将Bitmap处理为圆形的图片  
  3. * @param bitmap 处理之前的位图  
  4. * @return 处理之后的位图  
  5. */ 
  6. public static Bitmap circlePic(Bitmap bitmap){  
  7. int width = bitmap.getWidth();  
  8. int height = bitmap.getHeight();  
  9. int r = width < height ? width/2:height/2;//圆的半径,取宽和高中较小的,以便于显示没有空白  
  10. Bitmap outBitmap = Bitmap.createBitmap(r*2, r*2, Bitmap.Config.ARGB_8888);//创建一个刚好2r大小的Bitmap  
  11. Canvas canvas = new Canvas(outBitmap);  
  12. final int color =0xff424242;  
  13. final Paint paint = new Paint();  
  14. /**  
  15. * 截取图像的中心的一个正方形,用于在原图中截取  
  16. * 坐标如下:  
  17. * 1.如果 w < h , 左上坐标(0, (h-w)/2) , 右上坐标(w, (h+w)/2) 偏移10  
  18. * 2.如果 w > h , 左上坐标((w-h)/2, 0) , 右上坐标((w+h)/2, h) 偏移10  
  19. */ 
  20. final Rect rect = new Rect( width < height ? 0 : (width-height)/2, width < height ? (height-width)/2 - 10 : -10,  
  21. width < height ? width : (width+height)/2, (width < height ? (height+width)/2 - 10: height - 10));  
  22. //创建一个直径大小的正方形,用于设置canvas的显示与设置画布截取  
  23. final Rect rect2 = new Rect( 0, 0, r*2, r*2);  
  24. //提高精度,用于消除锯齿  
  25. final RectF rectF = new RectF(rect2);  
  26. //下面是设置画笔和canvas  
  27. paint.setAntiAlias(true);  
  28. canvas.drawARGB(0,0,0,0);  
  29. paint.setColor(color);  
  30. //设置圆角,半径都为r,大小为rect2  
  31. canvas.drawRoundRect(rectF, r, r, paint);  
  32. //设置图像重叠时的显示方式  
  33. paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));  
  34. //绘制图像到canvas  
  35. canvas.drawBitmap(bitmap, rect, rect2, paint);  
  36. return outBitmap;  
  37. }  

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


注:相关教程知识阅读请移步到Android开发频道。
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表