Advertisement
Guest User

Untitled

a guest
Apr 19th, 2014
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.25 KB | None | 0 0
  1. BitmapFactory.Options options = new BitmapFactory.Options();
  2. options.inMutable = true;
  3. _bitmap = BitmapFactory.decodeFile(_path, options);
  4.  
  5. BitmapFactory.Options options = new BitmapFactory.Options();
  6. options.inJustDecodeBounds = true;
  7. BitmapFactory.decodeResource(getResources(), R.id.myimage, options);
  8. int imageHeight = options.outHeight;
  9. int imageWidth = options.outWidth;
  10.  
  11. public static int calculateInSampleSize(
  12. BitmapFactory.Options options, int reqWidth, int reqHeight) {
  13. // Raw height and width of image
  14. final int height = options.outHeight;
  15. final int width = options.outWidth;
  16. int inSampleSize = 1;
  17.  
  18. if (height > reqHeight || width > reqWidth) {
  19.  
  20. final int halfHeight = height / 2;
  21. final int halfWidth = width / 2;
  22.  
  23. // Calculate the largest inSampleSize value that is a power of 2 and keeps both
  24. // height and width larger than the requested height and width.
  25. while ((halfHeight / inSampleSize) > reqHeight
  26. && (halfWidth / inSampleSize) > reqWidth) {
  27. inSampleSize *= 2;
  28. }
  29. }
  30.  
  31. return inSampleSize;
  32. }
  33.  
  34. public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId,
  35. int reqWidth, int reqHeight) {
  36.  
  37. // First decode with inJustDecodeBounds=true to check dimensions
  38. final BitmapFactory.Options options = new BitmapFactory.Options();
  39. options.inJustDecodeBounds = true;
  40. BitmapFactory.decodeResource(res, resId, options);
  41.  
  42. // Calculate inSampleSize
  43. options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
  44.  
  45. // Decode bitmap with inSampleSize set
  46. options.inJustDecodeBounds = false;
  47. return BitmapFactory.decodeResource(res, resId, options);
  48. }
  49.  
  50. final BitmapFactory.Options options = new BitmapFactory.Options();
  51. options.inJustDecodeBounds = true;
  52.  
  53. /*
  54. * If set to a value > 1, requests the decoder to
  55. * subsample the original image, returning a smaller
  56. * image to save memory. The sample size is the
  57. * number of pixels in either dimension that
  58. * correspond to a single pixel in the decoded
  59. * bitmap. For example, inSampleSize == 4 returns an
  60. * image that is 1/4 the width/height of the
  61. * original, and 1/16 the number of pixels. Any
  62. * value <= 1 is treated the same as 1. Note: the
  63. * decoder uses a final value based on powers of 2,
  64. * any other value will be rounded down to the
  65. * nearest power of 2.
  66. */
  67. options.inSampleSize = 2;
  68.  
  69. // Decode bitmap with inSampleSize set
  70. options.inJustDecodeBounds = false;
  71.  
  72. bitmap = BitmapFactory.decodeFile(path, options);
  73.  
  74. BitmapFactory.Options options = new BitmapFactory.Options();
  75. options.inJustDecodeBounds = true;
  76. BitmapFactory.decodeResource(getResources(), R.id.myimage, options);
  77. int imageHeight = options.outHeight;
  78. int imageWidth = options.outWidth;
  79. String imageType = options.outMimeType;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement