Advertisement
Guest User

Untitled

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