SHOW:
|
|
- or go back to the newest paste.
1 | ||
2 | ||
3 | - | import ru.donstroy.parusamobile.MainActivity; |
3 | + | |
4 | - | import android.app.ActivityManager; |
4 | + | |
5 | - | import android.content.Context; |
5 | + | |
6 | - | import android.content.res.Resources; |
6 | + | |
7 | - | import android.graphics.Bitmap; |
7 | + | |
8 | - | import android.graphics.BitmapFactory; |
8 | + | |
9 | - | import android.graphics.Matrix; |
9 | + | |
10 | - | import android.view.View; |
10 | + | |
11 | - | import android.view.ViewGroup; |
11 | + | |
12 | - | import android.view.ViewGroup.LayoutParams; |
12 | + | |
13 | - | import android.widget.BaseAdapter; |
13 | + | |
14 | - | import android.widget.Gallery; |
14 | + | |
15 | - | import android.widget.ImageView; |
15 | + | |
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 | } |