Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2014
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.14 KB | None | 0 0
  1. package com.thanhtung.tungnguyen;
  2.  
  3. import android.content.Context;
  4. import android.graphics.Bitmap;
  5. import android.graphics.BitmapFactory;
  6. import android.view.View;
  7. import android.view.ViewGroup;
  8. import android.widget.BaseAdapter;
  9. import android.widget.GridView;
  10. import android.widget.ImageView;
  11.  
  12. import java.util.ArrayList;
  13.  
  14. /*public class ImageAdapter extends BaseAdapter {
  15. private Context mContext;
  16.  
  17. // Keep all Images in array
  18. public Integer[] mThumbIds = {
  19. R.drawable.pic_1, R.drawable.pic_2,
  20. R.drawable.pic_3, R.drawable.pic_4,
  21. R.drawable.pic_5, R.drawable.pic_6,
  22. R.drawable.pic_7, R.drawable.pic_8,
  23. R.drawable.pic_9, R.drawable.pic_10,
  24. R.drawable.pic_10, R.drawable.pic_10,
  25. R.drawable.pic_10, R.drawable.pic_10,
  26. R.drawable.pic_10
  27. };
  28.  
  29. // Constructor
  30. public ImageAdapter(Context c){
  31. mContext = c;
  32. }
  33.  
  34. @Override
  35. public int getCount() {
  36. return mThumbIds.length;
  37. }
  38.  
  39. @Override
  40. public Object getItem(int position) {
  41. return mThumbIds[position];
  42. }
  43.  
  44. @Override
  45. public long getItemId(int position) {
  46. return 0;
  47. }
  48.  
  49. @Override
  50. public View getView(int position, View convertView, ViewGroup parent) {
  51. ImageView imageView = new ImageView(mContext);
  52. imageView.setImageResource(mThumbIds[position]);
  53. imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
  54. imageView.setLayoutParams(new GridView.LayoutParams(70, 70));
  55. return imageView;
  56. }
  57.  
  58. }*/
  59. public class ImageAdapter extends BaseAdapter {
  60.  
  61. private Context mContext;
  62. ArrayList<String> itemList = new ArrayList<String>();
  63.  
  64. public ImageAdapter(Context c) {
  65. mContext = c;
  66. }
  67.  
  68. void add(String path) {
  69. itemList.add(path);
  70. }
  71.  
  72. @Override
  73. public int getCount() {
  74. return itemList.size();
  75. }
  76.  
  77. @Override
  78. public Object getItem(int arg0) {
  79. // TODO Auto-generated method stub
  80. return null;
  81. }
  82.  
  83. @Override
  84. public long getItemId(int position) {
  85. // TODO Auto-generated method stub
  86. return 0;
  87. }
  88.  
  89. @Override
  90. public View getView(int position, View convertView, ViewGroup parent) {
  91. ImageView imageView;
  92. if (convertView == null) { // if it's not recycled, initialize some attributes
  93. imageView = new ImageView(mContext);
  94. imageView.setLayoutParams(new GridView.LayoutParams(220, 220));
  95. imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
  96. imageView.setPadding(8, 8, 8, 8);
  97. } else {
  98. imageView = (ImageView) convertView;
  99. }
  100.  
  101. Bitmap bm = decodeSampledBitmapFromUri(itemList.get(position), 220, 220);
  102.  
  103. imageView.setImageBitmap(bm);
  104. return imageView;
  105. }
  106.  
  107. public Bitmap decodeSampledBitmapFromUri(String path, int reqWidth, int reqHeight) {
  108.  
  109. Bitmap bm = null;
  110. // First decode with inJustDecodeBounds=true to check dimensions
  111. final BitmapFactory.Options options = new BitmapFactory.Options();
  112. options.inJustDecodeBounds = true;
  113. BitmapFactory.decodeFile(path, options);
  114.  
  115. // Calculate inSampleSize
  116. options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
  117.  
  118. // Decode bitmap with inSampleSize set
  119. options.inJustDecodeBounds = false;
  120. bm = BitmapFactory.decodeFile(path, options);
  121.  
  122. return bm;
  123. }
  124.  
  125. public int calculateInSampleSize(
  126.  
  127. BitmapFactory.Options options, int reqWidth, int reqHeight) {
  128. // Raw height and width of image
  129. final int height = options.outHeight;
  130. final int width = options.outWidth;
  131. int inSampleSize = 1;
  132.  
  133. if (height > reqHeight || width > reqWidth) {
  134. if (width > height) {
  135. inSampleSize = Math.round((float) height / (float) reqHeight);
  136. } else {
  137. inSampleSize = Math.round((float) width / (float) reqWidth);
  138. }
  139. }
  140.  
  141. return inSampleSize;
  142. }
  143.  
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement