/[projects]/android/Side9/src/dk/thoerup/side9/ImageAdapter.java
ViewVC logotype

Contents of /android/Side9/src/dk/thoerup/side9/ImageAdapter.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 791 - (show annotations) (download)
Thu Jun 3 09:31:00 2010 UTC (13 years, 11 months ago) by torben
File size: 2203 byte(s)
Cache the scaled image and downscale to a 1/4 of original size
1 package dk.thoerup.side9;
2
3 import java.io.File;
4 import java.util.ArrayList;
5
6 import android.content.Context;
7 import android.graphics.Bitmap;
8 import android.graphics.BitmapFactory;
9 import android.os.Environment;
10 import android.view.View;
11 import android.view.ViewGroup;
12 import android.widget.BaseAdapter;
13 import android.widget.GridView;
14 import android.widget.ImageView;
15
16 public class ImageAdapter extends BaseAdapter {
17 //int mGalleryItemBackground;
18 private Context mContext;
19
20 ArrayList<String> mBitmapPaths = new ArrayList<String>();
21 ArrayList<Bitmap> mBitmaps = new ArrayList<Bitmap>();
22
23 public ImageAdapter(Context c) {
24 mContext = c;
25
26 String path = Environment.getExternalStorageDirectory().getPath() + "/Side9" ;
27
28 File root = new File(path);
29 File files[] = root.listFiles();
30 for (File f : files) {
31 Bitmap bmp = BitmapFactory.decodeFile( f.getPath() );
32 Bitmap scaled = Bitmap.createScaledBitmap(bmp, bmp.getWidth()/4, bmp.getHeight()/4, true);
33
34 mBitmapPaths.add( f.getPath() );
35 mBitmaps.add(scaled);
36 }
37 }
38
39 public int getCount() {
40 return mBitmaps.size();
41 }
42
43 public Object getItem(int position) {
44 return position;
45 }
46
47 public long getItemId(int position) {
48 return position;
49 }
50
51 public String getImagePath(int position) {
52 return mBitmapPaths.get(position);
53 }
54
55 public View getView(int position, View convertView, ViewGroup parent) {
56 ImageView imageView;
57 if (convertView == null) { // if it's not recycled, initialize some attributes
58 imageView = new ImageView(mContext);
59 imageView.setLayoutParams(new GridView.LayoutParams(200, 200));
60 imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
61 imageView.setAdjustViewBounds(true);
62 imageView.setPadding(4, 4, 4, 4);
63 } else {
64 imageView = (ImageView) convertView;
65 }
66
67
68 imageView.setImageBitmap( mBitmaps.get(position) );
69 return imageView;
70
71 }
72 }

  ViewVC Help
Powered by ViewVC 1.1.20