SDK: 0.9-r1
Usually the images are stored in the application's resources. In some cases, images that are stored at different locations in the system need to be accessed. We'll be using Gallery to view the images stored in SD card.
<Gallery xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gallery"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:gravity="center_vertical"
android:spacing="7px" />
- Gallery is the only object needed in the XML file.- android:spacing controls the distance between center and left/right images. Images.java :
public class Images extends Activity
{
private Uri[] mUrls;
String[] mFiles=null;
public void onCreate(Bundle icicle)
{
super.onCreate(icicle);
setContentView(R.layout.main);
class ImageFilter implements FilenameFilter
{
public boolean accept(File dir, String name)
{
return (name.endsWith(".jpg"));
}
}
File images = new File("/sdcard/");
File[] imagelist = images.listFiles(new ImageFilter());
mFiles = new String[imagelist.length];
for(int i= 0 ; i< imagelist.length; i++)
{
mFiles[i] = imagelist[i].getAbsolutePath();
}
mUrls = new Uri[mFiles.length];
for(int i=0; i < mFiles.length; i++)
{
mUrls[i] = Uri.parse(mFiles[i]);
}
-
mUrls array stores the path of the images that are parsed from mFiles array.
Gallery g = (Gallery) findViewById(R.id.gallery);
g.setAdapter(new ImageAdapter(this));
//g.setSelectorSkin(getResources().getDrawable(R.drawable.gallery_background_1));
- The ImageAdapter object returns a list of images.
public class ImageAdapter extends BaseAdapter
{
public ImageAdapter(Context c)
{
mContext = c;
}
public int getCount()
{
return mUrls.length;
}
public Object getItem(int position)
{
return position;
}
public long getItemId(int position)
{
return position;
}
public View getView(int position, View convertView, ViewGroup parent)
{
ImageView i = new ImageView(mContext);
i.setImageURI(mUrls[position]);
i.setScaleType(ImageView.ScaleType.FIT_XY);
i.setLayoutParams(new Gallery.LayoutParams(260, 250));
return i;
}
- ImageAdapter is a custom class derived from BaseAdapter to setup the Gallery view. It provides the Gallery values like no. of items in the gallery, their position etc.
- getView method sets up the image currently at the center. - A new ImageView is created which sets the address to the path stored earlier in mUrls array. - The size of the image is decided by setScaleType. When it is set to FIT_XY, it takes the X and Y values from the LayoutParams defined below it (260x250 in this case).
public float getAlpha(boolean focused, int offset)
{
return Math.max(1.0f, 1.0f - (0.2f * Math.abs(offset)));
}
public float getScale(boolean focused, int offset)
{
return Math.max(0.5f, 1.0f - (0.25f * Math.abs(offset)));
}
- getAlpha method controls the translucency at the corner of the left and right images (highlighted in red below).
- getScale method controls the size of the left and right images.