android.widget.Gallery - Example
What you learn: You will learn how to use the Gallery-Widget (android.widget.Gallery) to display a series of images (/res/drawable/s).
Difficulty: 1 of 5

What it will look like:

Description:
0.) Lets start with the XML-File which will result in the Screenshot shown above. It is very simple, just a TextView followed by a Gallery(with id: "gallery"):
Using xml Syntax Highlighting
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="GalleryExample - by anddev.org"
- />
- <Gallery id="@+id/gallery"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:gravity="bottom"
- />
- </LinearLayout>
Parsed in 0.002 seconds, using GeSHi 1.0.8.4
1.) Now to the EntryPoint of all Activities, the onCreate(...) method. It loads the previously defined main.xml and applies an Adapter (ImageAdapter) to the Gallery.
Using java Syntax Highlighting
- public class GalleryExample extends Activity {
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle icicle) {
- super.onCreate(icicle);
- setContentView(R.layout.main);
- /* Find the gallery defined in the main.xml
- * Apply a new (custom) ImageAdapter to it. */
- ((Gallery) findViewById(R.id.gallery))
- .setAdapter(new ImageAdapter(this));
- }
Parsed in 0.030 seconds, using GeSHi 1.0.8.4
2.) The basic setup of the ImageAdapter
Using java Syntax Highlighting
- public class ImageAdapter extends BaseAdapter {
- /** The parent context */
- private Context myContext;
- /** All images to be displayed.
- * Put some images to project-folder:
- * '/res/drawable/uvw.xyz' .*/
- private int[] myImageIds = {
- R.drawable.image_1,
- R.drawable.image_2,
- R.drawable.image_3,
- R.drawable.image_4
- };
- /** Simple Constructor saving the 'parent' context. */
- public ImageAdapter(Context c) { this.myContext = c; }
- /** Returns the amount of images we have defined. */
- public int getCount() { return this.myImageIds.length; }
- /* Use the array-Positions as unique IDs */
- public Object getItem(int position) { return position; }
- public long getItemId(int position) { return position; }
Parsed in 0.033 seconds, using GeSHi 1.0.8.4
3.) The 'important' methods of the ImageAdapter
Using java Syntax Highlighting
- /** Returns a new ImageView to
- * be displayed, depending on
- * the position passed. */
- public View getView(int position, View convertView, ViewGroup parent) {
- ImageView i = new ImageView(this.myContext);
- i.setImageResource(this.myImageIds[position]);
- /* Image should be scaled as width/height are set. */
- i.setScaleType(ImageView.ScaleType.FIT_XY);
- /* Set the Width/Height of the ImageView. */
- i.setLayoutParams(new Gallery.LayoutParams(150, 150));
- return i;
- }
- /** Returns the size (0.0f to 1.0f) of the views
- * depending on the 'offset' to the center. */
- public float getScale(boolean focused, int offset) {
- /* Formula: 1 / (2 ^ offset) */
- return Math.max(0, 1.0f / (float)Math.pow(2, Math.abs(offset)));
- }
- }
- }
Parsed in 0.035 seconds, using GeSHi 1.0.8.4
Thats it 

Regards,
plusminus






