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

2019-12-10 19:53:55王振洲

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

 

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

13.将图片裁剪成圆圈:

 

 
  1. /**   * 将Bitmap处理为圆形的图片  
  2. * @param bitmap 处理之前的位图   * @return 处理之后的位图  
  3. */  public static Bitmap circlePic(Bitmap bitmap){  
  4. int width = bitmap.getWidth();   int height = bitmap.getHeight();  
  5. int r = width < height ? width/2:height/2;//圆的半径,取宽和高中较小的,以便于显示没有空白   Bitmap outBitmap = Bitmap.createBitmap(r*2, r*2, Bitmap.Config.ARGB_8888);//创建一个刚好2r大小的Bitmap  
  6. Canvas canvas = new Canvas(outBitmap);   final int color =0xff424242;  
  7. final Paint paint = new Paint();   /**  
  8. * 截取图像的中心的一个正方形,用于在原图中截取   * 坐标如下:  
  9. * 1.如果 w < h , 左上坐标(0, (h-w)/2) , 右上坐标(w, (h+w)/2) 偏移10   * 2.如果 w > h , 左上坐标((w-h)/2, 0) , 右上坐标((w+h)/2, h) 偏移10  
  10. */  final Rect rect = new Rect( width < height ? 0 : (width-height)/2, width < height ? (height-width)/2 - 10 : -10,