Advertisement
Guest User

Untitled

a guest
Aug 25th, 2015
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.05 KB | None | 0 0
  1. //To load a bitmap with out causes memory issues, it must be scaled before loaded into memory
  2. public Bitmap createBitmap() {
  3. BitmapFactory.Options options2 = new BitmapFactory.Options();
  4. //inJustDecodeBounds basically loads the spec of the bitmap, such as height and width
  5. options2.inJustDecodeBounds = true;
  6. BitmapFactory.decodeFile(Main.this.getExternalFilesDir(filepath) +"/companylogo.png",options2);
  7. //calculateInSampleSize finds the appropriate size to scale to
  8. options2.inSampleSize = calculateInSampleSize(options2);
  9. options2.inJustDecodeBounds = false;
  10.  
  11. return BitmapFactory.decodeFile(Main.this.getExternalFilesDir(filepath) +"/companylogo.png",options2);
  12. }
  13.  
  14. public int calculateInSampleSize(BitmapFactory.Options options) {
  15. //get the width of device in pixels
  16. DisplayMetrics displayMetrics = Main.this.getResources().getDisplayMetrics();
  17. int reqWidth = displayMetrics.widthPixels;
  18.  
  19. //get the bitmapInHeight/width of incoming bitmap
  20. final int bitmapInHeight = options.outHeight;
  21. final int bitmapInWidth = options.outWidth;
  22.  
  23. //this calculates the maximum height/width that can fit on the screen, keeping the ratio size
  24. double devCal2 = (bitmapInHeight*1000)/bitmapInWidth;
  25. int reqHeight = (int) ((devCal2/1000)*reqWidth);
  26.  
  27. int inSampleSize = 1;
  28.  
  29. if (bitmapInHeight > reqHeight || bitmapInWidth > reqWidth) {
  30.  
  31. final int halfHeight = bitmapInHeight / 2;
  32. final int halfWidth = bitmapInWidth / 2;
  33.  
  34. // Calculate the largest inSampleSize value that is a power of 2 and keeps both
  35. // bitmapInHeight and width larger than the requested bitmapInHeight and width.
  36. while ((halfHeight / inSampleSize) > reqHeight
  37. && (halfWidth / inSampleSize) > reqWidth) {
  38. inSampleSize *= 2;
  39. }
  40. }
  41. return inSampleSize;
  42. // }
  43. //});
  44. //}
  45. //}.start();
  46.  
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement