1. Gallery allows you to drag an image to achieve a dazzling effect, as shown in the following figure. You can drag the image to the left and right:
2. layout code
<?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"></Gallery>
3. Use a class inherited from baseadapter to act as a container
Public class imageadapter extends baseadapter {private context mcontext; private integer [] mimageids = {R. drawable. img1, R. drawable. img2, R. drawable. img3, R. drawable. img4, R. drawable. img5, R. drawable. img6, R. drawable. img7, R. drawable. img8}; Public imageadapter (context c) {This. mcontext = C ;}@ override public int getcount () {return mimageids. length; // obtain the number of images} @ override public object getitem (INT position) {return position; // obtain the position of the image in the library} @ override public long getitemid (INT position) {return position; // obtain the position of the image in the library} @ override public view getview (INT position, view convertview, viewgroup parent) {log. I ("tag", Position + ""); imageview = new imageview (mcontext); imageview. setimageresource (mimageids [position]); imageview. setlayoutparams (new gallery. layoutparams (100,100); // sets the image size to imageview. setscaletype (imageview. scaletype. fit_center); // set the display ratio type return imageview ;}}
4. Java code used:Public class test_gallery extends activity {/** called when the activity is first created. * // @ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); gallery G = (Gallery) This. findviewbyid (R. id. gallery); G. setadapter (New imageadapter (this); G. setbackgroundresource (R. drawable. bg0); G. setonitemclicklistener (New onitemclickliste NER () {@ override public void onitemclick (adapterview <?> Arg0, view arg1, int arg2, long arg3) {toast. maketext (test_gallery.this, "selected" + (arg2 + 1) + "image", toast. length_short ). show ();}});}}