Advertisement
Guest User

Untitled

a guest
Apr 24th, 2014
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2.  
  3. public class GalleryAdapter extends BaseAdapter{
  4.     private int mGalleryItemBackground;
  5.     private Context mContext;
  6.     private final int[] mImage=VerboseMediaArhitechture.DataImg;
  7.  
  8.     public GalleryAdapter(Context сontext) {
  9.         mContext = сontext;
  10.     }
  11.  
  12.     @Override
  13.     public int getCount() {
  14.         // TODO Auto-generated method stub
  15.         return mImage.length;
  16.     }
  17.  
  18.     @Override
  19.     public Object getItem(int position) {
  20.         // TODO Auto-generated method stub
  21.         return mImage[position];
  22.     }
  23.  
  24.     @Override
  25.     public long getItemId(int position) {
  26.         // TODO Auto-generated method stub
  27.         return mImage[position];
  28.     }
  29.  
  30.     @Override
  31.     public View getView(int position, View convertView, ViewGroup parent) {
  32.         // TODO Auto-generated method stub
  33.         ImageView view = new ImageView(mContext);
  34.          
  35.         view.setImageBitmap(decodeSampledBitmapFromResource(MainActivity.resourcesApp, mImage[position],MainActivity.HightSreenSize/8,MainActivity.HightSreenSize/8););
  36.          
  37.         //view.setImageResource(mImage[position]);
  38.         //view.setPadding(20,0, 20, 0);
  39.         view.setLayoutParams(new Gallery.LayoutParams(MainActivity.WidthSreenSize/3, LayoutParams.WRAP_CONTENT));
  40.         view.setScaleType(ImageView.ScaleType.FIT_XY);
  41.         view.setBackgroundResource(mGalleryItemBackground);
  42.         return view;
  43.     }
  44.      
  45.     public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId,int reqWidth, int reqHeight) {
  46.  
  47.         // First decode with inJustDecodeBounds=true to check dimensions
  48.         final BitmapFactory.Options options = new BitmapFactory.Options();
  49.         options.inJustDecodeBounds = true;
  50.         BitmapFactory.decodeResource(res, resId, options);
  51.  
  52.         // Calculate inSampleSize
  53.         options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
  54.  
  55.         // Decode bitmap with inSampleSize set
  56.         options.inJustDecodeBounds = false;
  57.         return BitmapFactory.decodeResource(res, resId, options);
  58.     }
  59.      
  60.     private static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
  61.     // Raw height and width of image
  62.     final int height = options.outHeight;
  63.     final int width = options.outWidth;
  64.     int inSampleSize = 1;
  65.  
  66.     if (height > reqHeight || width > reqWidth) {
  67.  
  68.         final int halfHeight = height / 2;
  69.         final int halfWidth = width / 2;
  70.  
  71.         // Calculate the largest inSampleSize value that is a power of 2 and keeps both
  72.         // height and width larger than the requested height and width.
  73.         while ((halfHeight / inSampleSize) > reqHeight
  74.                 && (halfWidth / inSampleSize) > reqWidth) {
  75.             inSampleSize *= 2;
  76.         }
  77.     }
  78.  
  79.     return inSampleSize;
  80. }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement