A view that shows items in a center-locked, horizontally scrolling list.
The default values for the Gallery assume you will be using
Theme_galleryItemBackground as the background for each View given to the Gallery from the Adapter. If you are not doing this, you may need to adjust some Gallery properties, such as the spacing.Views given to the Gallery should use
Gallery.LayoutParams as their layout parameters type.In this tutorial, you'll create a gallery of photos and then display a toast message each time a gallery item is selected.
- Start a new project named HelloGallery.
- Find some photos you'd like to use, or use these sample images. Save the images into the project's
res/drawable/directory. - Open the
res/layout/main.xmlfile and insert the following:<?xml version="1.0" encoding="utf-8"?> <Gallery xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/gallery" android:layout_width="fill_parent" android:layout_height="wrap_content" />
- Open the
HelloGallery.javafile and insert the following code for theonCreate()method:@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Gallery g = (Gallery) findViewById(R.id.gallery); g.setAdapter(new ImageAdapter(this)); g.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView parent, View v, int position, long id) { Toast.makeText(HelloGallery.this, "" + position, Toast.LENGTH_SHORT).show(); } }); }
This starts by setting themain.xmllayout as the content view and then capturing theGalleryfrom the layout withfindViewById(int). A customBaseAdaptercalledImageAdapteris instantiated and applied to theGallerywithsetAdapter(). (TheImageAdapterclass is defined next.) Then an anonymousAdapterView.OnItemClickListeneris instantiated. TheonItemClick(AdapterView, View, int, long)callback method receives theAdapterViewwhere the click occurred, the specificViewthat received the click, the position of theViewclicked (zero-based), and the row ID of the item clicked (if applicable). In this example, all that's needed is the position of the click to show aToastmessage that says the position of the item, usingmakeText(Context, CharSequence, int)andshow()(in a real world scenario, this ID could be used to get the full sized image for some other task). - Create a new XML file in the
res/values/directory namedattrs.xml. Insert the following:<?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="HelloGallery"> <attr name="android:galleryItemBackground" /> </declare-styleable> </resources>
This is a custom styleable resource that can be applied to a layout. In this case, it will be applied to the individual items placed into theGallerywidget. The<attr>element defines a specific attribute for the styleable, and in this case, it refers to an existing platform attribute,galleryItemBackground, which defines a border styling for gallery items. In the next step, you'll see how this attribute is referenced and then later applied to each item in the gallery. - Go back to the
HelloGallery.javafile. After theonCreate(Bundle)method, define the customImageAdapterclass:public class ImageAdapter extends BaseAdapter { int mGalleryItemBackground; private Context mContext; private Integer[] mImageIds = { R.drawable.sample_1, R.drawable.sample_2, R.drawable.sample_3, R.drawable.sample_4, R.drawable.sample_5, R.drawable.sample_6, R.drawable.sample_7 }; public ImageAdapter(Context c) { mContext = c; TypedArray a = obtainStyledAttributes(R.styleable.HelloGallery); mGalleryItemBackground = a.getResourceId( R.styleable.HelloGallery_android_galleryItemBackground, 0); a.recycle(); } public int getCount() { return mImageIds.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.setImageResource(mImageIds[position]); i.setLayoutParams(new Gallery.LayoutParams(150, 100)); i.setScaleType(ImageView.ScaleType.FIT_XY); i.setBackgroundResource(mGalleryItemBackground); return i; } }
First, there are a few member variables, including an array of IDs that reference the images saved in the drawable resources directory (res/drawable/).Next is the class constructor, where theContextfor anImageAdapterinstance is defined and the styleable resource defined in the last step is acquired and saved to a local field. At the end of the constructor,recycle()is called on theTypedArrayso it can be re-used by the system.The methodsgetCount(),getItem(int), andgetItemId(int)are methods that must be implemented for simple queries on theAdapter. Themethod does the work to apply an image to an {@link android.widget.ImageViewthat will be embedded in theGallery. In this method, the memberContextis used to create a newImageView. TheImageViewis prepared by applying an image from the local array of drawable resources, setting theGallery.LayoutParamsheight and width for the image, setting the scale to fit theImageViewdimensions, and then finally setting the background to use the styleable attribute acquired in the constructor.SeeImageView.ScaleTypefor other image scaling options. - Run the application.
No comments:
Post a Comment